let4be
let4be

Reputation: 1081

Golang simultaneous read/write to the file without explicit file lock

I have a situation where I need to concurrently read/write from/to the file, but the scope of operations is limited:

This is a high loaded application and I would like to avoid locking file for each read/write I do

I was going to open 2 files - one for read, another for append only

would doing so create some potential issues/bugs?

what is the recommended practice if I would like to avoid file locking for each read/write I do?

p.s. golang, linux, ext4

Upvotes: 4

Views: 4972

Answers (1)

cnicutar
cnicutar

Reputation: 182794

I'll assume by "random read" you actually mean "arbitrary read".

If I understand your use case correctly, you don't need to seek or lock or do anything manual. UNIX has this covered via O_APPEND. Here is what you can do:

  1. Open the file with os.O_APPEND. This way every write, regardless of any preceding operations, will go to the end of the file
  2. When reading use File.ReadAt. This lets you specify arbitrary offsets for your reads

Using this scheme you can avoid any sort of locking: the OS will do it for you. Because of the buffer cache this scheme is not even inefficient: appends and reads are pretty much independent.

Upvotes: 5

Related Questions