Reputation: 1081
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
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:
os.O_APPEND
. This way every write, regardless of any preceding operations, will go to the end of the fileFile.ReadAt
. This lets you specify arbitrary offsets for your readsUsing 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