Debian Codegraph @

bench/diffcopy.go

raw symbols SCIP
1 package bench
2
3 import (
4 "context"
5
6 "github.com/pkg/errors"
7 "github.com/tonistiigi/fsutil"
8 fstypes "github.com/tonistiigi/fsutil/types"
9 "golang.org/x/sync/errgroup"
10 )
11
12 func diffCopy(proto bool, src, dest string) error {
13 var s1, s2 fsutil.Stream
14
15 eg, ctx := errgroup.WithContext(context.Background())
16
17 if proto {
18 s1, s2 = sockPairProto(ctx)
19 } else {
20 s1, s2 = sockPair(ctx)
21 }
22
23 eg.Go(func() error {
24 fs, err := fsutil.NewFS(src)
25 if err != nil {
26 panic(err)
27 }
28 return fsutil.Send(ctx, s1, fs, nil)
29 })
30 eg.Go(func() error {
31 return fsutil.Receive(ctx, s2, dest, fsutil.ReceiveOpt{})
32 })
33
34 return eg.Wait()
35 }
36
37 func diffCopyProto(src, dest string) error {
38 return diffCopy(true, src, dest)
39 }
40 func diffCopyReg(src, dest string) error {
41 return diffCopy(false, src, dest)
42 }
43
44 func sockPair(ctx context.Context) (fsutil.Stream, fsutil.Stream) {
45 c1 := make(chan *fstypes.Packet, 64)
46 c2 := make(chan *fstypes.Packet, 64)
47 return &fakeConn{ctx, c1, c2}, &fakeConn{ctx, c2, c1}
48 }
49
50 func sockPairProto(ctx context.Context) (fsutil.Stream, fsutil.Stream) {
51 c1 := make(chan []byte, 64)
52 c2 := make(chan []byte, 64)
53 return &fakeConnProto{ctx, c1, c2}, &fakeConnProto{ctx, c2, c1}
54 }
55
56 type fakeConn struct {
57 ctx context.Context
58 recvChan chan *fstypes.Packet
59 sendChan chan *fstypes.Packet
60 }
61
62 func (fc *fakeConn) Context() context.Context {
63 return fc.ctx
64 }
65
66 func (fc *fakeConn) RecvMsg(m interface{}) error {
67 p, ok := m.(*fstypes.Packet)
68 if !ok {
69 return errors.Errorf("invalid msg: %#v", m)
70 }
71 select {
72 case <-fc.ctx.Done():
73 return fc.ctx.Err()
74 case p2 := <-fc.recvChan:
75 *p = *p2
76 return nil
77 }
78 }
79
80 func (fc *fakeConn) SendMsg(m interface{}) error {
81 p, ok := m.(*fstypes.Packet)
82 if !ok {
83 return errors.Errorf("invalid msg: %#v", m)
84 }
85 p2 := *p
86 p2.Data = append([]byte{}, p2.Data...)
87 select {
88 case <-fc.ctx.Done():
89 return fc.ctx.Err()
90 case fc.sendChan <- &p2:
91 return nil
92 }
93 }
94
95 type fakeConnProto struct {
96 ctx context.Context
97 recvChan chan []byte
98 sendChan chan []byte
99 }
100
101 func (fc *fakeConnProto) Context() context.Context {
102 return fc.ctx
103 }
104
105 func (fc *fakeConnProto) RecvMsg(m interface{}) error {
106 p, ok := m.(*fstypes.Packet)
107 if !ok {
108 return errors.Errorf("invalid msg: %#v", m)
109 }
110 select {
111 case <-fc.ctx.Done():
112 return fc.ctx.Err()
113 case dt := <-fc.recvChan:
114 return p.Unmarshal(dt)
115 }
116 }
117
118 func (fc *fakeConnProto) SendMsg(m interface{}) error {
119 p, ok := m.(*fstypes.Packet)
120 if !ok {
121 return errors.Errorf("invalid msg: %#v", m)
122 }
123 dt, err := p.Marshal()
124 if err != nil {
125 return err
126 }
127 select {
128 case <-fc.ctx.Done():
129 return fc.ctx.Err()
130 case fc.sendChan <- dt:
131 return nil
132 }
133 }