【发布时间】:2019-05-02 07:35:52
【问题描述】:
我有可以从多个进程更新的文件,因此我想使用锁定并找到 https://github.com/gofrs/flock 这可能会有所帮助
但我认为这个问题有点复杂,例如更新 api 需要 read 之前的 file/ 部分的文件(我们提供 api 来读取 file 或applicationname 的对象)
获取json数据在内存中修改并更新文件。
有两种选择:
- 更新所有文件内容
- 更新文件的部分,即一个 应用程序属性
问题如下:
1. Process A & Process B (can be more…) Reads the object of
application name `node1`
2. Process A update the section (node1) with new data (for example
change the kind property and update)
3. Process B want to do the same , the problem is that between the time
it reads the data and the time it wants to update ,the data is not
valid since other process already update it.
此外,相同的场景对所有文件上下文都有效
Race 条件问题...
这是一些stateless 进程可以在任何给定时间更新的文件的简短示例
ID: demo
version: 0.0.1
applications:
- name: node1
kind: html5
path: node1
expose:
- name: node_api
properties:
url: ${app-url}
- name: node2
kind: nodejs
path: node2
parameters:
quota: 256M
memory: 256M
我们如何克服这个问题,或者简化它以避免竞争条件和串通?
【问题讨论】:
-
进程读取文件时,会一直更新文件内容吗?
-
@AnkitDeshpande - 并不总是...
-
听起来你真的很想使用数据库。
-
@flyx - 抱歉,在这种情况下我不能,我还有其他选择吗?甚至是简化事情的方法?或限制 API...我对想法持开放态度...
-
当进程 B 尝试将数据写入文件并看到它已被进程 A 更新时,进程 B 应该做什么?
标签: go locking race-condition file-locking