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
- func NewConn(name string, maxBuf int) (Conn, Conn)
- func NewTCPConn(src, dst netip.AddrPort, maxBuf int) (local Conn, remote Conn)
- type Conn
- type Listener
- func Listen(addr string) *Listener
- func (l *Listener) Accept() (net.Conn, error)
- func (l *Listener) Addr() net.Addr
- func (l *Listener) Close() error
- func (l *Listener) Dial(ctx context.Context, network, addr string) (_ net.Conn, err error)
- type Network
- func (m *Network) Dial(ctx context.Context, network, address string) (net.Conn, error)
- func (m *Network) Listen(network, address string) (net.Listener, error)
- func (m *Network) NewLocalTCPListener() net.Listener
- type Pipe
- func NewPipe(name string, maxBuf int) *Pipe
- func (p *Pipe) Block() error
- func (p *Pipe) Close() error
- func (p *Pipe) Read(b []byte) (n int, err error)
- func (p *Pipe) SetReadDeadline(t time.Time) error
- func (p *Pipe) SetWriteDeadline(t time.Time) error
- func (p *Pipe) Unblock() error
- func (p *Pipe) Write(b []byte) (n int, err error)
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 ¶
NewConn creates a pair of Conns that are wired together by pipes.
func NewTCPConn ¶
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 ¶
Listen returns a new Listener for the provided address.
func (*Listener) Accept ¶
Accept blocks until a new connection is available or the listener is closed.
func (*Listener) Addr ¶
Addr implements net.Listener.Addr.
func (*Listener) Close ¶
Close closes the pipe listener.
func (*Listener) Dial ¶
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 (*Network) Listen ¶
func (*Network) NewLocalTCPListener ¶
type Pipe ¶
type Pipe struct {
// contains filtered or unexported fields
}
Pipe implements an in-memory FIFO with timeouts.
func NewPipe ¶
NewPipe creates a Pipe with a buffer size fixed at maxBuf.
func (*Pipe) Block ¶
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 ¶
Close closes the pipe.
func (*Pipe) Read ¶
Read implements io.Reader. Once the buffer is drained (i.e. after Close), subsequent calls will return io.EOF.
func (*Pipe) SetReadDeadline ¶
SetReadDeadline sets the deadline for future Read calls.
func (*Pipe) SetWriteDeadline ¶
SetWriteDeadline sets the deadline for future Write calls.
func (*Pipe) Unblock ¶
Unblock will cause all blocked Read/Write calls to continue execution.
func (*Pipe) Write ¶
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.