tailscale.comtailscale.com/net/memnet Index | Files

package memnet

import "tailscale.com/net/memnet"

Package memnet implements an in-memory network implementation. It is useful for dialing and listening on in-memory addresses in tests and other situations where you don't want to use the network.

Index

Constants

const NetworkName = "mem"

NetworkName is the network name returned by net.Addr.Network for net.Conn.LocalAddr and net.Conn.RemoteAddr from the Conn type.

Functions

func NewConn

func NewConn(name string, maxBuf int) (Conn, Conn)

NewConn creates a pair of Conns that are wired together by pipes.

func NewTCPConn

func NewTCPConn(src, dst netip.AddrPort, maxBuf int) (local Conn, remote Conn)

NewTCPConn creates a pair of Conns that are wired together by pipes.

Types

type Conn

type Conn interface {
	net.Conn

	// SetReadBlock blocks or unblocks the Read method of this Conn.
	// It reports an error if the existing value matches the new value,
	// or if the Conn has been Closed.
	SetReadBlock(bool) error

	// SetWriteBlock blocks or unblocks the Write method of this Conn.
	// It reports an error if the existing value matches the new value,
	// or if the Conn has been Closed.
	SetWriteBlock(bool) error
}

Conn is a net.Conn that can additionally have its reads and writes blocked and unblocked.

type Listener

type Listener struct {

	// NewConn, if non-nil, is called to create a new pair of connections
	// when dialing. If nil, NewConn is used.
	NewConn func(network, addr string, maxBuf int) (Conn, Conn)
	// contains filtered or unexported fields
}

Listener is a net.Listener using NewConn to create pairs of network connections connected in memory using a buffered pipe. It also provides a Dial method to establish new connections.

func Listen

func Listen(addr string) *Listener

Listen returns a new Listener for the provided address.

func (*Listener) Accept

func (l *Listener) Accept() (net.Conn, error)

Accept blocks until a new connection is available or the listener is closed.

func (*Listener) Addr

func (l *Listener) Addr() net.Addr

Addr implements net.Listener.Addr.

func (*Listener) Close

func (l *Listener) Close() error

Close closes the pipe listener.

func (*Listener) Dial

func (l *Listener) Dial(ctx context.Context, network, addr string) (_ net.Conn, err error)

Dial connects to the listener using the provided context. The provided Context must be non-nil. If the context expires before the connection is complete, an error is returned. Once successfully connected any expiration of the context will not affect the connection.

type Network

type Network struct {
	// contains filtered or unexported fields
}

Network implements Network using an in-memory network, usually used for testing.

As of 2025-04-08, it only supports TCP.

Its zero value is a valid netx.Network implementation.

func (*Network) Dial

func (m *Network) Dial(ctx context.Context, network, address string) (net.Conn, error)

func (*Network) Listen

func (m *Network) Listen(network, address string) (net.Listener, error)

func (*Network) NewLocalTCPListener

func (m *Network) NewLocalTCPListener() net.Listener

type Pipe

type Pipe struct {
	// contains filtered or unexported fields
}

Pipe implements an in-memory FIFO with timeouts.

func NewPipe

func NewPipe(name string, maxBuf int) *Pipe

NewPipe creates a Pipe with a buffer size fixed at maxBuf.

func (*Pipe) Block

func (p *Pipe) Block() error

Block will cause all calls to Read and Write to block until they either timeout, are unblocked or the pipe is closed.

func (*Pipe) Close

func (p *Pipe) Close() error

Close closes the pipe.

func (*Pipe) Read

func (p *Pipe) Read(b []byte) (n int, err error)

Read implements io.Reader. Once the buffer is drained (i.e. after Close), subsequent calls will return io.EOF.

func (*Pipe) SetReadDeadline

func (p *Pipe) SetReadDeadline(t time.Time) error

SetReadDeadline sets the deadline for future Read calls.

func (*Pipe) SetWriteDeadline

func (p *Pipe) SetWriteDeadline(t time.Time) error

SetWriteDeadline sets the deadline for future Write calls.

func (*Pipe) Unblock

func (p *Pipe) Unblock() error

Unblock will cause all blocked Read/Write calls to continue execution.

func (*Pipe) Write

func (p *Pipe) Write(b []byte) (n int, err error)

Write implements io.Writer.

Source Files

conn.go listener.go memnet.go pipe.go

Version
v1.84.1 (latest)
Published
May 29, 2025
Platform
linux/amd64
Imports
12 packages
Last checked
9 hours ago

Tools for package owners.