comm

package
v0.0.0-...-efe4ef5 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2023 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultTimeout is used in R/W macros when
	DefaultTimeout = 5 * time.Second
)

Variables

View Source
var (
	// ErrTimeout is generated by timeout on a reader or writer
	ErrTimeout = errors.New("io: timeout")
)

Functions

This section is empty.

Types

type CreationFunc

type CreationFunc func() (io.ReadWriteCloser, error)

CreationFunc is a function which returns a new "connection" to something a closure should be used to encapsulate the variables and functions needed

func BackingOffTCPConnMaker

func BackingOffTCPConnMaker(address string, timeout time.Duration) CreationFunc

BackingOffTCPConnMaker is a TCPConnMaker with hardcoded backoff parameters

func NetworkConnMaker

func NetworkConnMaker(network string, address string, timeout time.Duration) CreationFunc

NetworkConnMaker builds the closure needed to satisfy the creationFunc interface

func SerialConnMaker

func SerialConnMaker(cfg *serial.Config) CreationFunc

SerialConnMaker creates the closure for a new serial connection based on a config

func TCPConnMaker

func TCPConnMaker(address string, timeout time.Duration) CreationFunc

TCPConnMaker wraps NetworkConnmaker with TCP as the network

type Pool

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

Pool is a communication pool which holds one or more connections to a device that will be closed if they are not in use, and re-opened as needed. it is concurrent safe. Pools must be created with NewPool.

func NewPool

func NewPool(maxSize int, idleTimeout time.Duration, maker CreationFunc) *Pool

NewPool creates a new pool. At each interval of timeout, a connection may be freed if it is available. Calling Close terminates the background goroutine which closes idle connections and drains the pool as it is immediately if idleTimeout is zero, the pool never frees connections.

func (*Pool) Active

func (p *Pool) Active() int

Active returns the number of connections owned by the pool that are currently given out

func (*Pool) Close

func (p *Pool) Close()

Close interrupts the background collection of idle connections if it is not called, the garbage collector can never free the pool and the background worker will never stop

func (*Pool) Destroy

func (p *Pool) Destroy(rw io.ReadWriter)

Destroy immediately frees a communicator from the pool. This should be used instead of Put if the communicator has gone bad.

func (*Pool) Get

func (p *Pool) Get() (io.ReadWriter, error)

Get retrieves a communicator from the channel, blocking until one is available if all are in use. It is guaranteed that there is no contestion for the ReadWriter. The consumer should not attempt to cast it to its concrete type and use it outside this interface.

When done with the communicator, return it with Put(), or discard it with Destroy() if it has become no good (e.g., all calls error).

If the error from Get is not nil, you must not return it to the pool.

func (*Pool) Put

func (p *Pool) Put(rw io.ReadWriter)

Put restores a communicator to the pool. It may be reused, or will be be freed eventually by the pool. Junk communicators (ones that always error) should be Destroy()'d and not returned with Put.

func (*Pool) ReturnWithError

func (p *Pool) ReturnWithError(rw io.ReadWriter, err error)

ReturnWithError calls Put if err == nil, else Destroy

func (*Pool) Size

func (p *Pool) Size() int

Size returns the number of connections in the pool, or given out from it

type Terminator

type Terminator struct {
	Wterm byte
	Rterm byte
	// contains filtered or unexported fields
}

Terminator is a struct holding termination sequences and read/writers

func NewTerminator

func NewTerminator(rw io.ReadWriter, Rx, Tx byte) Terminator

NewTerminator returns a wrapper around a Read/Writer that appends and strips termination bytes

func (Terminator) Read

func (t Terminator) Read(buf []byte) (int, error)

Read implements io.Reader. The input is scanned up to the first encounter of Rterm. Rterm is stripped from the message and the remainder returned. buf is double buffered for this purpose.

func (Terminator) Write

func (t Terminator) Write(b []byte) (int, error)

type Timeout

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

Timeout is a wrapper for IO ReadWriter which adds a timeout

func NewTimeout

func NewTimeout(rw io.ReadWriter, timeout time.Duration) (Timeout, error)

NewTimeout creates a new timeout wrapping a read/writer

func (Timeout) Read

func (t Timeout) Read(b []byte) (int, error)

Read passes read to the embedded reader and stops early if the timeout elapses

func (Timeout) Write

func (t Timeout) Write(b []byte) (int, error)

Read passes read to the embedded reader and stops early if the timeout elapses

Jump to

Keyboard shortcuts

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