mirror of https://github.com/tailscale/tailscale/
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
245 lines
5.0 KiB
Go
245 lines
5.0 KiB
Go
2 years ago
|
// Copyright (c) Tailscale Inc & AUTHORS
|
||
|
// SPDX-License-Identifier: BSD-3-Clause
|
||
5 years ago
|
|
||
2 years ago
|
package memnet
|
||
5 years ago
|
|
||
|
import (
|
||
4 years ago
|
"bytes"
|
||
5 years ago
|
"context"
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"log"
|
||
4 years ago
|
"net"
|
||
|
"os"
|
||
5 years ago
|
"sync"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
const debugPipe = false
|
||
|
|
||
|
// Pipe implements an in-memory FIFO with timeouts.
|
||
|
type Pipe struct {
|
||
|
name string
|
||
|
maxBuf int
|
||
4 years ago
|
mu sync.Mutex
|
||
|
cnd *sync.Cond
|
||
5 years ago
|
|
||
|
blocked bool
|
||
4 years ago
|
closed bool
|
||
|
buf bytes.Buffer
|
||
5 years ago
|
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 {
|
||
4 years ago
|
p := &Pipe{
|
||
5 years ago
|
name: name,
|
||
|
maxBuf: maxBuf,
|
||
|
}
|
||
4 years ago
|
p.cnd = sync.NewCond(&p.mu)
|
||
|
return p
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
// 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
|
||
|
}
|
||
5 years ago
|
|
||
|
// Read implements io.Reader.
|
||
4 years ago
|
// Once the buffer is drained (i.e. after Close), subsequent calls will
|
||
|
// return io.EOF.
|
||
5 years ago
|
func (p *Pipe) Read(b []byte) (n int, err error) {
|
||
|
if debugPipe {
|
||
|
orig := b
|
||
|
defer func() {
|
||
3 years ago
|
log.Printf("Pipe(%q).Read(%q) n=%d, err=%v", p.name, string(orig[:n]), n, err)
|
||
5 years ago
|
}()
|
||
|
}
|
||
4 years ago
|
for n == 0 {
|
||
|
n2, err := p.readOrBlock(b)
|
||
|
if err != nil {
|
||
|
return n2, err
|
||
5 years ago
|
}
|
||
4 years ago
|
n += n2
|
||
|
}
|
||
|
p.cnd.Signal()
|
||
|
return n, nil
|
||
|
}
|
||
5 years ago
|
|
||
4 years ago
|
// 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
|
||
5 years ago
|
}
|
||
4 years ago
|
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
|
||
5 years ago
|
}
|
||
|
|
||
|
// 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)
|
||
|
}()
|
||
|
}
|
||
4 years ago
|
for len(b) > 0 {
|
||
|
n2, err := p.writeOrBlock(b)
|
||
|
if err != nil {
|
||
|
return n + n2, err
|
||
5 years ago
|
}
|
||
4 years ago
|
n += n2
|
||
|
b = b[n2:]
|
||
5 years ago
|
}
|
||
4 years ago
|
return n, nil
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
// Close closes the pipe.
|
||
5 years ago
|
func (p *Pipe) Close() error {
|
||
|
p.mu.Lock()
|
||
4 years ago
|
defer p.mu.Unlock()
|
||
5 years ago
|
p.closed = true
|
||
4 years ago
|
p.blocked = false
|
||
5 years ago
|
if p.cancelWriteTimer != nil {
|
||
|
p.cancelWriteTimer()
|
||
|
p.cancelWriteTimer = nil
|
||
|
}
|
||
|
if p.cancelReadTimer != nil {
|
||
|
p.cancelReadTimer()
|
||
|
p.cancelReadTimer = nil
|
||
|
}
|
||
4 years ago
|
p.cnd.Broadcast()
|
||
5 years ago
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
4 years ago
|
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
|
||
|
}
|
||
|
|
||
5 years ago
|
// SetReadDeadline sets the deadline for future Read calls.
|
||
|
func (p *Pipe) SetReadDeadline(t time.Time) error {
|
||
|
p.mu.Lock()
|
||
4 years ago
|
defer p.mu.Unlock()
|
||
5 years ago
|
p.readTimeout = t
|
||
4 years ago
|
// If we already have a deadline, cancel it and create a new one.
|
||
5 years ago
|
if p.cancelReadTimer != nil {
|
||
|
p.cancelReadTimer()
|
||
|
p.cancelReadTimer = nil
|
||
|
}
|
||
4 years ago
|
p.cancelReadTimer = p.deadlineTimer(t)
|
||
5 years ago
|
return nil
|
||
|
}
|
||
|
|
||
|
// SetWriteDeadline sets the deadline for future Write calls.
|
||
|
func (p *Pipe) SetWriteDeadline(t time.Time) error {
|
||
|
p.mu.Lock()
|
||
4 years ago
|
defer p.mu.Unlock()
|
||
5 years ago
|
p.writeTimeout = t
|
||
4 years ago
|
// If we already have a deadline, cancel it and create a new one.
|
||
5 years ago
|
if p.cancelWriteTimer != nil {
|
||
|
p.cancelWriteTimer()
|
||
|
p.cancelWriteTimer = nil
|
||
|
}
|
||
4 years ago
|
p.cancelWriteTimer = p.deadlineTimer(t)
|
||
5 years ago
|
return nil
|
||
|
}
|
||
|
|
||
4 years ago
|
// Block will cause all calls to Read and Write to block until they either
|
||
|
// timeout, are unblocked or the pipe is closed.
|
||
5 years ago
|
func (p *Pipe) Block() error {
|
||
|
p.mu.Lock()
|
||
4 years ago
|
defer p.mu.Unlock()
|
||
5 years ago
|
closed := p.closed
|
||
|
blocked := p.blocked
|
||
|
p.blocked = true
|
||
|
|
||
|
if closed {
|
||
2 years ago
|
return fmt.Errorf("memnet.Pipe(%q).Block: closed", p.name)
|
||
5 years ago
|
}
|
||
|
if blocked {
|
||
2 years ago
|
return fmt.Errorf("memnet.Pipe(%q).Block: already blocked", p.name)
|
||
5 years ago
|
}
|
||
4 years ago
|
p.cnd.Broadcast()
|
||
5 years ago
|
return nil
|
||
|
}
|
||
|
|
||
4 years ago
|
// Unblock will cause all blocked Read/Write calls to continue execution.
|
||
5 years ago
|
func (p *Pipe) Unblock() error {
|
||
|
p.mu.Lock()
|
||
4 years ago
|
defer p.mu.Unlock()
|
||
5 years ago
|
closed := p.closed
|
||
|
blocked := p.blocked
|
||
|
p.blocked = false
|
||
|
|
||
|
if closed {
|
||
2 years ago
|
return fmt.Errorf("memnet.Pipe(%q).Block: closed", p.name)
|
||
5 years ago
|
}
|
||
|
if !blocked {
|
||
2 years ago
|
return fmt.Errorf("memnet.Pipe(%q).Block: already unblocked", p.name)
|
||
5 years ago
|
}
|
||
4 years ago
|
p.cnd.Broadcast()
|
||
5 years ago
|
return nil
|
||
|
}
|