memnet

package
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 27, 2023 License: MIT, BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Overview

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

This section is empty.

Variables

This section is empty.

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 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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL