summaryrefslogtreecommitdiffhomepage
path: root/net/memnet/pipe.go
blob: 8caca57df2f813e031aad2be3590fe2c7da03cc8 (plain)
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
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause

package memnet

import (
	"bytes"
	"context"
	"fmt"
	"io"
	"log"
	"net"
	"os"
	"sync"
	"time"
)

const debugPipe = false

// Pipe implements an in-memory FIFO with timeouts.
type Pipe struct {
	name   string
	maxBuf int
	mu     sync.Mutex
	cnd    *sync.Cond

	blocked          bool
	closed           bool
	buf              bytes.Buffer
	readTimeout      time.Time
	writeTimeout     time.Time
	cancelReadTimer  func()
	cancelWriteTimer func()
}

// NewPipe creates a Pipe with a buffer size fixed at maxBuf.
func NewPipe(name string, maxBuf int) *Pipe {
	p := &Pipe{
		name:   name,
		maxBuf: maxBuf,
	}
	p.cnd = sync.NewCond(&p.mu)
	return p
}

// readOrBlock attempts to read from the buffer, if the buffer is empty and
// the connection hasn't been closed it will block until there is a change.
func (p *Pipe) readOrBlock(b []byte) (int, error) {
	p.mu.Lock()
	defer p.mu.Unlock()
	if !p.readTimeout.IsZero() && !time.Now().Before(p.readTimeout) {
		return 0, os.ErrDeadlineExceeded
	}
	if p.blocked {
		p.cnd.Wait()
		return 0, nil
	}

	n, err := p.buf.Read(b)
	// err will either be nil or io.EOF.
	if err == io.EOF {
		if p.closed {
			return n, err
		}
		// Wait for something to change.
		p.cnd.Wait()
	}
	return n, nil
}

// Read implements io.Reader.
// Once the buffer is drained (i.e. after Close), subsequent calls will
// return io.EOF.
func (p *Pipe) Read(b []byte) (n int, err error) {
	if debugPipe {
		orig := b
		defer func() {
			log.Printf("Pipe(%q).Read(%q) n=%d, err=%v", p.name, string(orig[:n]), n, err)
		}()
	}
	for n == 0 {
		n2, err := p.readOrBlock(b)
		if err != nil {
			return n2, err
		}
		n += n2
	}
	p.cnd.Signal()
	return n, nil
}

// writeOrBlock attempts to write to the buffer, if the buffer is full it will
// block until there is a change.
func (p *Pipe) writeOrBlock(b []byte) (int, error) {
	p.mu.Lock()
	defer p.mu.Unlock()
	if p.closed {
		return 0, net.ErrClosed
	}
	if !p.writeTimeout.IsZero() && !time.Now().Before(p.writeTimeout) {
		return 0, os.ErrDeadlineExceeded
	}
	if p.blocked {
		p.cnd.Wait()
		return 0, nil
	}

	// Optimistically we want to write the entire slice.
	n := len(b)
	if limit := p.maxBuf - p.buf.Len(); limit < n {
		// However, we don't have enough capacity to write everything.
		n = limit
	}
	if n == 0 {
		// Wait for something to change.
		p.cnd.Wait()
		return 0, nil
	}

	p.buf.Write(b[:n])
	p.cnd.Signal()
	return n, nil
}

// Write implements io.Writer.
func (p *Pipe) Write(b []byte) (n int, err error) {
	if debugPipe {
		orig := b
		defer func() {
			log.Printf("Pipe(%q).Write(%q) n=%d, err=%v", p.name, string(orig), n, err)
		}()
	}
	for len(b) > 0 {
		n2, err := p.writeOrBlock(b)
		if err != nil {
			return n + n2, err
		}
		n += n2
		b = b[n2:]
	}
	return n, nil
}

// Close closes the pipe.
func (p *Pipe) Close() error {
	p.mu.Lock()
	defer p.mu.Unlock()
	p.closed = true
	p.blocked = false
	if p.cancelWriteTimer != nil {
		p.cancelWriteTimer()
		p.cancelWriteTimer = nil
	}
	if p.cancelReadTimer != nil {
		p.cancelReadTimer()
		p.cancelReadTimer = nil
	}
	p.cnd.Broadcast()

	return nil
}

func (p *Pipe) deadlineTimer(t time.Time) func() {
	if t.IsZero() {
		return nil
	}
	if t.Before(time.Now()) {
		p.cnd.Broadcast()
		return nil
	}
	ctx, cancel := context.WithDeadline(context.Background(), t)
	go func() {
		<-ctx.Done()
		if ctx.Err() == context.DeadlineExceeded {
			p.cnd.Broadcast()
		}
	}()
	return cancel
}

// SetReadDeadline sets the deadline for future Read calls.
func (p *Pipe) SetReadDeadline(t time.Time) error {
	p.mu.Lock()
	defer p.mu.Unlock()
	p.readTimeout = t
	// If we already have a deadline, cancel it and create a new one.
	if p.cancelReadTimer != nil {
		p.cancelReadTimer()
		p.cancelReadTimer = nil
	}
	p.cancelReadTimer = p.deadlineTimer(t)
	return nil
}

// SetWriteDeadline sets the deadline for future Write calls.
func (p *Pipe) SetWriteDeadline(t time.Time) error {
	p.mu.Lock()
	defer p.mu.Unlock()
	p.writeTimeout = t
	// If we already have a deadline, cancel it and create a new one.
	if p.cancelWriteTimer != nil {
		p.cancelWriteTimer()
		p.cancelWriteTimer = nil
	}
	p.cancelWriteTimer = p.deadlineTimer(t)
	return nil
}

// Block will cause all calls to Read and Write to block until they either
// timeout, are unblocked or the pipe is closed.
func (p *Pipe) Block() error {
	p.mu.Lock()
	defer p.mu.Unlock()
	closed := p.closed
	blocked := p.blocked
	p.blocked = true

	if closed {
		return fmt.Errorf("memnet.Pipe(%q).Block: closed", p.name)
	}
	if blocked {
		return fmt.Errorf("memnet.Pipe(%q).Block: already blocked", p.name)
	}
	p.cnd.Broadcast()
	return nil
}

// Unblock will cause all blocked Read/Write calls to continue execution.
func (p *Pipe) Unblock() error {
	p.mu.Lock()
	defer p.mu.Unlock()
	closed := p.closed
	blocked := p.blocked
	p.blocked = false

	if closed {
		return fmt.Errorf("memnet.Pipe(%q).Block: closed", p.name)
	}
	if !blocked {
		return fmt.Errorf("memnet.Pipe(%q).Block: already unblocked", p.name)
	}
	p.cnd.Broadcast()
	return nil
}