floatdrop / cacha

Cache on file-system
MIT License
21 stars 0 forks source link

Handle cross-process conflict #1

Open jamestalmage opened 8 years ago

jamestalmage commented 8 years ago

We need this to work across multiple process.

Specifically, how to handle cacha.get when the file is still open for write via cacha.set in a separate process.

I am not sure exactly how Node behaves in this scenario (hopefully it just returns / throws an error). If that's the case, maybe just a delay / retry loop.

Alternatively (but more complicated, and I'm still not 100% sure how it would look); Have it write to a temp file, then rename. I think renames are atomic cross-platform.

Windows: how to do atomic writes in a file

Things UNIX can do atomically

I think we are mostly safe on this since the file name is the hash of the contents. That prevents us from having to decide who "wins" in a race condition for writes (if a race condition for the same filename exists, it doesn't matter who wins - they are both trying to write the same contents).

The issue is mostly how to handle reads while the file is write-locked.

sindresorhus commented 8 years ago

See https://github.com/yeoman/configstore/issues/20 and how it was solved.

jamestalmage commented 8 years ago

TL; DR; https://github.com/iarna/write-file-atomic