Documentation
¶
Overview ¶
plex is a library for managing net.Conn connection types with minimal effort this library utilizes the options pattern to allow for easy configuration and customization of the multiplexer. This library provides multiple types which are used as wrappers to the configured net.Conn types which allow for general streaming of data through channels of bytes as an alternative to the more go traditional io.Reader/io.Writer interfaces.
One important caveat is that the Plex library makes no assumptions of which net.Conn object is returned from either Reader or Writer. This means that messages must be routed by the consumer based on the data in the message rather than assuming that a response to a received message will be sent to the same connection which initiated the message round trip transmission.
The goal of this library is to abstract the complexity of managing multiple connections and their associated channels of data into a single API which consumers can request Reader and Writer types from. These types implement the io.Closer method which will re-queue the Reader or Writer to the correct internal buffer within the multiplexer for another consumer down the line.
Index ¶
- type Conn
- type Connect
- type ErrConnection
- type Killer
- type Multiplexer
- func (m *Multiplexer) Add(ctx context.Context, conns ...Conn) (left []Conn, err error)
- func (m *Multiplexer) Addr() net.Addr
- func (m *Multiplexer) Close() (err error)
- func (m *Multiplexer) Reader(ctx context.Context, timeout *time.Duration) (Reader, error)
- func (m *Multiplexer) Writer(ctx context.Context, timeout *time.Duration) (Writer, error)
- type Option
- type Reader
- type Writer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ErrConnection ¶
type Multiplexer ¶
type Multiplexer struct {
// contains filtered or unexported fields
}
Multiplexer is the type which manages the multiplexing of net.Conn types through the use of channels and goroutines.
func New ¶
func New(ctx context.Context, opts ...Option) (*Multiplexer, error)
New creates a net.Conn Multiplexer with the provided options.
CRITICAL: A Multiplexer must be closed by calling the Close method when it is no longer needed. Failure to do so may result in goroutines leaking.
NOTE: A single instance of the Multiplexer can ONLY be used to manage a set of connections for a SINGLE host.
NOTE: It is important to read the documentation for the options which you wish to use to ensure proper configuration of the multiplexer.
func (*Multiplexer) Add ¶
Add adds the provided net.Conn instances to the multiplexer.
NOTE: If the Multiplexer does not have the capacity to add all of the provided connections, then the connections that were unable to be added will be returned. It is possible the the connection was able to be added for a reader or writer, or both but will not indicate which.
TODO: Correct this in the future, we should be able to know if it was added to one of them or both.
func (*Multiplexer) Addr ¶
func (m *Multiplexer) Addr() net.Addr
Addr returns the net.Addr of the connections in this instance of the multiplexer.
func (*Multiplexer) Close ¶
func (m *Multiplexer) Close() (err error)
Close closes the multiplexer and all of the connections it manages.
func (*Multiplexer) Reader ¶
Reader returns a wrapped net.Conn object as a Reader which provides stream capabilities to the net.Conn as well as cuts down on the available methods on the net.Conn. This is done on purpose to ensure that the consumer is less likely to misuse the Reader when requesting one for the purpose of reading. nolint:dupl
func (*Multiplexer) Writer ¶
Writer returns a wrapped net.Conn object as a Writer which provides stream capabilities to the net.Conn as well as cuts down on the available methods on the net.Conn. This is done on purpose to ensure that the consumer is less likely to misuse the Writer when requesting one for the purpose of writing. nolint:dupl
type Option ¶
type Option func(*Multiplexer) error
Option is the function literate that is used to configure the multiplexer during initialization.
func WithAutoScaling ¶
WithAutoScaling sets a timeout on the multiplexer to automatically scale connections when a connection request exceeds the current the timeout when attempting to read from the internal buffer.
NOTE: This options is ONLY available when a connector is provided via the `WithConnector` option.
TODO: This needs to have the ability to set a timer for the new connection to be killed when the connection is no longer used.
func WithConnections ¶
WithConnections provides an initial set of connections to be used by the multiplexer.
NOTE: See note on `WithMaxCapacity` regarding the default buffer size when this option is used in conjunction with `WithMaxCapacity`, or no connections are provided via the `WithConnections` option.
func WithConnector ¶
WithConnector provides a function that can be used to create new connections in the multiplexer.
NOTE: This method is only used when the current length of the multiplexer's connections is less than the multiplexer's capacity and a connection already in the multiplexer fails and must be recreated OR the multiplexer is configured for auto scaling.
func WithMaxCapacity ¶
WithMaxCapacity allocates the internal buffer for the multiplexer to a static capacity not based on the number of connections supplied at initialization but a set capacity.
NOTE: If no connections are provided via the `WithConnections` option, and the MaxCapacity is NOT set, the multiplexer will allocate a buffer of the default size which is 1.