-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmock_stream.go
298 lines (253 loc) · 5.79 KB
/
mock_stream.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package mocknet
import (
"bytes"
"errors"
"io"
"net"
"strconv"
"sync/atomic"
"time"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/protocol"
)
var streamCounter atomic.Int64
// stream implements network.Stream
type stream struct {
rstream *stream
conn *conn
id int64
write *io.PipeWriter
read *io.PipeReader
toDeliver chan *transportObject
reset chan struct{}
close chan struct{}
closed chan struct{}
writeErr error
protocol atomic.Pointer[protocol.ID]
stat network.Stats
}
var ErrClosed = errors.New("stream closed")
type transportObject struct {
msg []byte
arrivalTime time.Time
}
func newStreamPair() (*stream, *stream) {
ra, wb := io.Pipe()
rb, wa := io.Pipe()
sa := newStream(wa, ra, network.DirOutbound)
sb := newStream(wb, rb, network.DirInbound)
sa.rstream = sb
sb.rstream = sa
return sa, sb
}
func newStream(w *io.PipeWriter, r *io.PipeReader, dir network.Direction) *stream {
s := &stream{
read: r,
write: w,
id: streamCounter.Add(1),
reset: make(chan struct{}, 1),
close: make(chan struct{}, 1),
closed: make(chan struct{}),
toDeliver: make(chan *transportObject),
stat: network.Stats{Direction: dir},
}
go s.transport()
return s
}
// How to handle errors with writes?
func (s *stream) Write(p []byte) (n int, err error) {
l := s.conn.link
delay := l.GetLatency() + l.RateLimit(len(p))
t := time.Now().Add(delay)
// Copy it.
cpy := make([]byte, len(p))
copy(cpy, p)
select {
case <-s.closed: // bail out if we're closing.
return 0, s.writeErr
case s.toDeliver <- &transportObject{msg: cpy, arrivalTime: t}:
}
return len(p), nil
}
func (s *stream) ID() string {
return strconv.FormatInt(s.id, 10)
}
func (s *stream) Protocol() protocol.ID {
p := s.protocol.Load()
if p == nil {
return ""
}
return *p
}
func (s *stream) Stat() network.Stats {
return s.stat
}
func (s *stream) SetProtocol(proto protocol.ID) error {
s.protocol.Store(&proto)
return nil
}
func (s *stream) CloseWrite() error {
select {
case s.close <- struct{}{}:
default:
}
<-s.closed
if s.writeErr != ErrClosed {
return s.writeErr
}
return nil
}
func (s *stream) CloseRead() error {
return s.read.CloseWithError(ErrClosed)
}
func (s *stream) Close() error {
_ = s.CloseRead()
return s.CloseWrite()
}
func (s *stream) Reset() error {
// Cancel any pending reads/writes with an error.
s.write.CloseWithError(network.ErrReset)
s.read.CloseWithError(network.ErrReset)
select {
case s.reset <- struct{}{}:
default:
}
<-s.closed
// No meaningful error case here.
return nil
}
func (s *stream) teardown() {
// at this point, no streams are writing.
s.conn.removeStream(s)
// Mark as closed.
close(s.closed)
}
func (s *stream) Conn() network.Conn {
return s.conn
}
func (s *stream) SetDeadline(t time.Time) error {
return &net.OpError{Op: "set", Net: "pipe", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
}
func (s *stream) SetReadDeadline(t time.Time) error {
return &net.OpError{Op: "set", Net: "pipe", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
}
func (s *stream) SetWriteDeadline(t time.Time) error {
return &net.OpError{Op: "set", Net: "pipe", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
}
func (s *stream) Read(b []byte) (int, error) {
return s.read.Read(b)
}
// transport will grab message arrival times, wait until that time, and
// then write the message out when it is scheduled to arrive
func (s *stream) transport() {
defer s.teardown()
bufsize := 256
buf := new(bytes.Buffer)
timer := time.NewTimer(0)
if !timer.Stop() {
select {
case <-timer.C:
default:
}
}
// cleanup
defer timer.Stop()
// writeBuf writes the contents of buf through to the s.Writer.
// done only when arrival time makes sense.
drainBuf := func() error {
if buf.Len() > 0 {
_, err := s.write.Write(buf.Bytes())
if err != nil {
return err
}
buf.Reset()
}
return nil
}
// deliverOrWait is a helper func that processes
// an incoming packet. it waits until the arrival time,
// and then writes things out.
deliverOrWait := func(o *transportObject) error {
buffered := len(o.msg) + buf.Len()
// Yes, we can end up extending a timer multiple times if we
// keep on making small writes but that shouldn't be too much of an
// issue. Fixing that would be painful.
if !timer.Stop() {
// FIXME: So, we *shouldn't* need to do this but we hang
// here if we don't... Go bug?
select {
case <-timer.C:
default:
}
}
delay := time.Until(o.arrivalTime)
if delay >= 0 {
timer.Reset(delay)
} else {
timer.Reset(0)
}
if buffered >= bufsize {
select {
case <-timer.C:
case <-s.reset:
select {
case s.reset <- struct{}{}:
default:
}
return network.ErrReset
}
if err := drainBuf(); err != nil {
return err
}
// write this message.
_, err := s.write.Write(o.msg)
if err != nil {
return err
}
} else {
buf.Write(o.msg)
}
return nil
}
for {
// Reset takes precedent.
select {
case <-s.reset:
s.writeErr = network.ErrReset
return
default:
}
select {
case <-s.reset:
s.writeErr = network.ErrReset
return
case <-s.close:
if err := drainBuf(); err != nil {
s.cancelWrite(err)
return
}
s.writeErr = s.write.Close()
if s.writeErr == nil {
s.writeErr = ErrClosed
}
return
case o := <-s.toDeliver:
if err := deliverOrWait(o); err != nil {
s.cancelWrite(err)
return
}
case <-timer.C: // ok, due to write it out.
if err := drainBuf(); err != nil {
s.cancelWrite(err)
return
}
}
}
}
func (s *stream) Scope() network.StreamScope {
return &network.NullScope{}
}
func (s *stream) cancelWrite(err error) {
s.write.CloseWithError(err)
s.writeErr = err
}