跳到主要内容

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

浏览文件

pool.go

79 行 · 1.4 KB历史下载
1package sftp
2
3// bufPool provides a pool of byte-slices to be reused in various parts of the package.
4// It is safe to use concurrently through a pointer.
5type bufPool struct {
6 ch chan []byte
7 blen int
8}
9
10func newBufPool(depth, bufLen int) *bufPool {
11 return &bufPool{
12 ch: make(chan []byte, depth),
13 blen: bufLen,
14 }
15}
16
17func (p *bufPool) Get() []byte {
18 if p.blen <= 0 {
19 panic("bufPool: new buffer creation length must be greater than zero")
20 }
21
22 for {
23 select {
24 case b := <-p.ch:
25 if cap(b) < p.blen {
26 // just in case: throw away any buffer with insufficient capacity.
27 continue
28 }
29
30 return b[:p.blen]
31
32 default:
33 return make([]byte, p.blen)
34 }
35 }
36}
37
38func (p *bufPool) Put(b []byte) {
39 if p == nil {
40 // functional default: no reuse.
41 return
42 }
43
44 if cap(b) < p.blen || cap(b) > p.blen*2 {
45 // DO NOT reuse buffers with insufficient capacity.
46 // This could cause panics when resizing to p.blen.
47
48 // DO NOT reuse buffers with excessive capacity.
49 // This could cause memory leaks.
50 return
51 }
52
53 select {
54 case p.ch <- b:
55 default:
56 }
57}
58
59type resChanPool chan chan result
60
61func newResChanPool(depth int) resChanPool {
62 return make(chan chan result, depth)
63}
64
65func (p resChanPool) Get() chan result {
66 select {
67 case ch := <-p:
68 return ch
69 default:
70 return make(chan result, 1)
71 }
72}
73
74func (p resChanPool) Put(ch chan result) {
75 select {
76 case p <- ch:
77 default:
78 }
79}