跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

lock.go

48 行 · 1.2 KB历史下载
1package cli
2
3import (
4 "context"
5 "fmt"
6 "os"
7 "path/filepath"
8 "time"
9)
10
11// lockVM takes the lock of name, which serialises what changes that VM
12// between agent-vm runs: two runs in one folder would otherwise clean up,
13// clone and configure the same VM at once. It waits, saying so, until the
14// holder is done or ctx ends. The lock is a file in the state folder's
15// locks/, kept for the next run: a crashed holder releases it with its
16// process.
17func (e *app) lockVM(ctx context.Context, name string) (unlock func(), err error) {
18 dir := e.state.Path("locks")
19 if err := os.MkdirAll(dir, 0o755); err != nil {
20 return nil, err
21 }
22 f, err := os.OpenFile(filepath.Join(dir, name), os.O_CREATE|os.O_RDWR, 0o644)
23 if err != nil {
24 return nil, err
25 }
26 told := false
27 for {
28 ok, err := tryLock(f)
29 if err != nil {
30 f.Close()
31 return nil, err
32 }
33 if ok {
34 // Closing the file releases the lock.
35 return func() { f.Close() }, nil
36 }
37 if !told {
38 fmt.Fprintf(e.io.Stderr, "Waiting for another agent-vm that is working on '%s'...\n", name)
39 told = true
40 }
41 select {
42 case <-ctx.Done():
43 f.Close()
44 return nil, ctx.Err()
45 case <-time.After(200 * time.Millisecond):
46 }
47 }
48}