multiplexed

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: May 16, 2024 License: Apache-2.0, BSD-2-Clause, BSD-3-Clause, + 2 more Imports: 15 Imported by: 0

Documentation

Overview

Package multiplexed provides multiplexed pool implementation.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrFrameParserNil indicates that frame parse is nil.
	ErrFrameParserNil = errors.New("frame parser is nil")
	// ErrRecvQueueFull receive queue full.
	ErrRecvQueueFull = errors.New("virtual connection's recv queue is full")
	// ErrSendQueueFull send queue is full.
	ErrSendQueueFull = errors.New("connection's send queue is full")
	// ErrChanClose connection is closed.
	ErrChanClose = errors.New("unexpected recv chan close")
	// ErrAssertFail type assert fail.
	ErrAssertFail = errors.New("type assert fail")
	// ErrDupRequestID duplicated request id.
	ErrDupRequestID = errors.New("duplicated Request ID")
	// ErrInitPoolFail failed to initialize connection.
	ErrInitPoolFail = errors.New("init pool for specific node fail")
	// ErrWriteNotFinished write operation is not completed.
	ErrWriteNotFinished = errors.New("write not finished")
	// ErrNetworkNotSupport does not support network type.
	ErrNetworkNotSupport = errors.New("network not support")
	// ErrConnectionsHaveBeenExpelled denotes that the connections to a certain ip:port have been expelled.
	ErrConnectionsHaveBeenExpelled = errors.New("connections have been expelled")
)
View Source
var DefaultMultiplexedPool = New()

DefaultMultiplexedPool is the default multiplexed implementation.

Functions

This section is empty.

Types

type Connection

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

Connection represents the underlying tcp connection.

type Connections

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

Connections represents a collection of concrete connections.

type FrameParser

type FrameParser interface {
	// Parse parses vid and frame from io.ReadCloser. rc.Close must be called before Parse return.
	Parse(rc io.Reader) (vid uint32, buf []byte, err error)
}

FrameParser is the interface to parse a single frame.

type GetOptions

type GetOptions struct {
	FP  FrameParser
	VID uint32

	CACertFile    string // CA certificate.
	TLSCertFile   string // Client certificate.
	TLSKeyFile    string // Client secret key.
	TLSServerName string // The client verifies the server's service name,

	LocalAddr string
	// contains filtered or unexported fields
}

GetOptions get conn configuration.

func NewGetOptions

func NewGetOptions() GetOptions

NewGetOptions creates GetOptions.

func (*GetOptions) WithDialTLS

func (o *GetOptions) WithDialTLS(certFile, keyFile, caFile, serverName string)

WithDialTLS returns an Option which sets the client to support TLS.

func (*GetOptions) WithFrameParser

func (o *GetOptions) WithFrameParser(fp FrameParser)

WithFrameParser sets the FrameParser of a single Get.

func (*GetOptions) WithLocalAddr

func (o *GetOptions) WithLocalAddr(addr string)

WithLocalAddr returns an Option which sets the local address when establishing a connection, and it is randomly selected by default when there are multiple network cards.

func (*GetOptions) WithVID

func (o *GetOptions) WithVID(vid uint32)

WithVID returns an Option which sets virtual connection ID.

type Multiplexed

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

Multiplexed represents multiplexing.

func New

func New(opt ...PoolOption) *Multiplexed

New creates a new multiplexed instance.

func (*Multiplexed) GetMuxConn

func (p *Multiplexed) GetMuxConn(
	ctx context.Context,
	network string,
	address string,
	opts GetOptions,
) (MuxConn, error)

GetMuxConn gets a multiplexing connection to the address on named network.

type MuxConn

type MuxConn interface {
	// Write writes data to the connection.
	Write([]byte) error

	// Read reads a packet from connection.
	Read() ([]byte, error)

	// LocalAddr returns the local network address, if known.
	LocalAddr() net.Addr

	// RemoteAddr returns the remote network address, if known.
	RemoteAddr() net.Addr

	// Close closes the connection.
	// Any blocked Read or Write operations will be unblocked and return errors.
	Close()
}

MuxConn is virtual connection multiplexing on a real connection.

type Pool

type Pool interface {
	// GetMuxConn gets a multiplexing connection to the address on named network.
	GetMuxConn(ctx context.Context, network string, address string, opts GetOptions) (MuxConn, error)
}

Pool is a connection pool for multiplexing.

type PoolOption

type PoolOption func(*PoolOptions)

PoolOption is the Options helper.

func WithConnectNumber

func WithConnectNumber(number int) PoolOption

WithConnectNumber returns an Option which sets the number of connections for each peer in the connection pool.

func WithDialTimeout

func WithDialTimeout(d time.Duration) PoolOption

WithDialTimeout returns an Option which sets the connection timeout.

func WithDropFull

func WithDropFull(drop bool) PoolOption

WithDropFull returns an Option which sets whether to drop the request when the queue is full.

func WithMaxIdleConnsPerHost

func WithMaxIdleConnsPerHost(n int) PoolOption

WithMaxIdleConnsPerHost returns an Option which sets the maximum number of idle connections for each peer ip:port, this value should not be less than ConnectNumber, This option is usually used with MaxVirConnsPerConn in streaming scenarios to dynamically adjust the number of connections, This option takes effect only when MaxVirConnsPerConn is set, 0 means no limit.

func WithMaxVirConnsPerConn

func WithMaxVirConnsPerConn(n int) PoolOption

WithMaxVirConnsPerConn returns an Option which sets the maximum number of virtual connections per real connection, 0 means no limit.

func WithQueueSize

func WithQueueSize(n int) PoolOption

WithQueueSize returns an Option which sets the length of each Connection sending queue in the connection pool.

type PoolOptions

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

PoolOptions represents some settings for the connection pool.

type VirtualConnection

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

VirtualConnection multiplexes virtual connections.

func (*VirtualConnection) Close

func (vc *VirtualConnection) Close()

Close closes the connection.

func (*VirtualConnection) LocalAddr

func (vc *VirtualConnection) LocalAddr() net.Addr

LocalAddr gets the local address of the connection.

func (*VirtualConnection) Read

func (vc *VirtualConnection) Read() ([]byte, error)

Read reads back the packet. Write and Read can be concurrent, but not concurrent Read.

func (*VirtualConnection) RemoteAddr

func (vc *VirtualConnection) RemoteAddr() net.Addr

RemoteAddr gets the peer address of the connection.

func (*VirtualConnection) Write

func (vc *VirtualConnection) Write(b []byte) error

Write writes request packet. Write and Read can be concurrent, multiple Write can be concurrent.

Jump to

Keyboard shortcuts

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