comms

package
v0.0.18 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2021 License: BSD-2-Clause Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Listen

func Listen(host, port string)

Listen sets up a socket to listen for client connections. When a client connects the connection made is passed to newClient to setup a client instance for housekeeping. client.Process is then launched as a new goroutine to handle the main I/O processing for the client.

TODO: currently there is no way to shut the server down other than Ctrl-C

func NewQuota added in v0.0.14

func NewQuota(ts TimeSource) *quota

NewQuota returns a new, initialised connection quota. Connection quota can either be per listening server port or per server. The TimeSource is a function returning the current time as a time.Time, typically time.Now.

Types

type Ring added in v0.0.14

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

Ring is a very specific implementation of a ring buffer with a fixed capacity for use in connection limiting. The implementation is a power of 2 ring buffer with free running indexes and bit-masking instead of modulo arithmetic.

The implementation is complete and can easily be reused and tweaked depending on requirements. Copious notes have been provided, but the implementation mostly depends on the ringSize, the Ring.elems data type and the Ring.start/Ring.end data types.

As is, the ring buffer functions do not return errors for an empty or full ring buffer. The ring buffer functions Empty and Full can be used to test the ring buffer before calling other functions that would fail.

For performance the ring buffer uses a fixed sized array instead of a slice as this eliminates most bounds checking. Also, for performance, the value of old elements in the buffer are not zeroed. However, old elements are not accessible unless the unexported elems member is accessed directly.

func (*Ring) Cap added in v0.0.14

func (r *Ring) Cap() int

Cap returns the total capacity of the ring buffer.

func (*Ring) Empty added in v0.0.14

func (r *Ring) Empty() bool

Empty returns true if the ring buffer is empty else false.

func (*Ring) First added in v0.0.14

func (r *Ring) First() (value int64)

First returns the value stored in the first element of the ring buffer. If the ring buffer is empty 0 will be returned. If 0 is a vlid value to store in the ring buffer then First should not be called if Empty returns true.

func (*Ring) FirstReplace added in v0.0.14

func (r *Ring) FirstReplace(value int64)

FirstReplace replaces the value of the first element of the ring buffer with the new, provided value. The update will be ignored if the ring buffer is empty.

func (*Ring) Full added in v0.0.14

func (r *Ring) Full() bool

Full returns true if the ring buffer is full else false.

func (*Ring) Last added in v0.0.14

func (r *Ring) Last() (value int64)

Last returns the value stored in the last element of the ring buffer. If the ring buffer is empty 0 will be returned. If 0 is a vlid value to store in the ring buffer then Last should not be called if Empty returns true.

func (*Ring) LastReplace added in v0.0.14

func (r *Ring) LastReplace(value int64)

LastReplace replaces the value of the last element of the ring buffer with the new, provided value. The updated will be ignored if the ring buffer is empty.

func (*Ring) Len added in v0.0.14

func (r *Ring) Len() int

Len returns the number of elements currently in use in the ring buffer.

func (*Ring) Peek added in v0.0.14

func (r *Ring) Peek(pos int) (value int64)

Peek returns the value stored in the ring buffer element at position pos. If the requested position is invalid, outside of the range 0 <= pos < Len(), then the value returned will be 0. If 0 is a valid value to store in the ring buffer then the position should be tested before calling Peek so that a 0 on error cannot be returned.

func (*Ring) Poke added in v0.0.14

func (r *Ring) Poke(pos int, value int64)

Poke stores the passed value in the ring buffer element at position pos. The update will be ignored if the position is invalid, outside of the range 0 <= pos < Len(),

func (*Ring) Pop added in v0.0.14

func (r *Ring) Pop() (value int64)

Pop removes the last element from the ring buffer and returns its value. If the ring buffer is empty then a value of 0 will be returned. If 0 is a vlid value to store in the ring buffer then Pop should not be called if Empty returns true.

func (*Ring) Popd added in v0.0.14

func (r *Ring) Popd()

Popd removes and discards the last element from the ring buffer. This is more efficient than calling Pop and ignoring the return value.

func (*Ring) Push added in v0.0.14

func (r *Ring) Push(value int64)

Push appends a new element to the end of the ring buffer and sets its value to the specified value. If the ring buffer is already full the update will be ignored. To avoid silent failures Full can be called before Push to make sure the ring buffer is not already full.

func (*Ring) Shift added in v0.0.14

func (r *Ring) Shift() (value int64)

Shift removes the first element from the ring buffer and returns its value. If the ring buffer is empty then a value of 0 will be returned. If 0 is a vlid value to store in the ring buffer then Shift should not be called if Empty returns true.

func (*Ring) Shiftd added in v0.0.14

func (r *Ring) Shiftd()

Shiftd removes and discards the first element from the ring buffer. This is more efficient than calling Shift and ignoring the return value.

func (Ring) String added in v0.0.14

func (r Ring) String() string

String returns the values of the elements of the ring buffer as a string. The values in the string will be in the order of the elements in the ring buffer. The format of the returned string is the same as for an int64 slice when used with fmt and the $v verb: [value value ...], for easier debugging.

func (*Ring) Unshift added in v0.0.14

func (r *Ring) Unshift(value int64)

Unshift appends a new element to the start of the ring buffer and sets its value to the specified value. If the ring buffer is already full the update will be ignored. To avoid silent failures Full can be called before Unshift to make sure the ring buffer is not already full.

type TimeSource added in v0.0.14

type TimeSource func() time.Time

TimeSource is a function that returns the current time as a time.Time. This is typically time.Now but may be replaced with a fake time for testing.

Jump to

Keyboard shortcuts

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