kyriosli / node-shared-cache

An interprocess shared LRU cache module for Node.JS
149 stars 30 forks source link

现在的锁方案是flock? #8

Closed friskfly closed 7 years ago

friskfly commented 7 years ago
To avoid data crupption, we use a read-write lock to ensure that data modification is exclusive. But when a program is writting data when something bad, for example, a SIGKILL, happens that crashes the program before the write operation is complete and lock is released, other processes may not be able to enter the exclusive region again. I do not use an auto recovery lock such as flock, which will automatically release when process exits, just in case that wrong data is returned when performing a reading operation, or even, causing a segment fault.

我看到描述中,进程退出是不能释放锁的,但看到代码实现中使用的是flock,是这样吗?

kyriosli commented 7 years ago

是的,我本来不想用flock因为进程出错后它自动释放会导致写入不完全,但后来加入了一个dirty位解决了这个问题。当进程获得写锁开始写操作前,先将dirty置为1,写成功释放锁之前,将dirty置为0. 这样如果一个进程获得了写锁,发现dirty为1,说明有一个进程写操作没完成就退出了,当前进程可以选择检查并恢复数据完整性,或者初始化整个内存区域(目前只实现了后者)。

friskfly commented 7 years ago

好的 thx