common

package
v0.0.0-...-f37ca10 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2024 License: MPL-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ServerRateLimitIntervalSec is the interval at which client rate limit counter operates, i.e. maximum N clients per interval of X.
	ServerRateLimitIntervalSec = 1
	/*
		ServerDefaultIOTimeoutSec is the default IO timeout applied to all client connections. The IO timeout prevents a
		potentially malfunctioning server application from hanging at a lingering client.
		Server application should always override the default IO timeout by setting a new timeout in connection handler.
	*/
	ServerDefaultIOTimeoutSec = 10 * 60
)
View Source
const (
	// MaxPacketSize is the maximum acceptable size for a single UDP packet
	MaxUDPPacketSize = 9038
)
View Source
const (
	TimerCommandTimeoutSec = 10 // TimerCommandTimeoutSec is a hard coded timeout number constraining all commands run by timer.
)

Variables

This section is empty.

Functions

This section is empty.

Types

type RecurringCommands

type RecurringCommands struct {
	// PreConfiguredCommands are toolbox commands pre-configured to run by user, they never deleted upon clearing.
	PreConfiguredCommands []string `json:"PreConfiguredCommands"`
	// IntervalSec is the number of seconds to sleep between execution of all commands.
	IntervalSec int `json:"IntervalSec"`
	// MaxResults is the maximum number of results to memorise from command execution and text messages.
	MaxResults int `json:"MaxResults"`
	// CommandProcessor is the one going to run all commands.
	CommandProcessor *toolbox.CommandProcessor `json:"-"`
	// contains filtered or unexported fields
}

RecurringCommands executes series of commands, one at a time, at regular interval. Execution results of recent commands are memorised and can be retrieved at a later time. Beyond command execution results, arbitrary text messages may also be memorised and retrieved together with command results. RecurringCommands is a useful structure for implementing notification kind of mechanism.

func (*RecurringCommands) AddArbitraryTextToResult

func (cmds *RecurringCommands) AddArbitraryTextToResult(text string)

AddArbitraryTextToResult simply places an arbitrary text string into result.

func (*RecurringCommands) AddTransientCommand

func (cmds *RecurringCommands) AddTransientCommand(cmd string)

AddTransientCommand places a new toolbox command toward the end of transient command list.

func (*RecurringCommands) ClearTransientCommands

func (cmds *RecurringCommands) ClearTransientCommands()

ClearTransientCommands removes all transient commands.

func (*RecurringCommands) GetResults

func (cmds *RecurringCommands) GetResults() []string

GetResults returns the latest command execution results and text messages, then clears the result buffer.

func (*RecurringCommands) GetTransientCommands

func (cmds *RecurringCommands) GetTransientCommands() []string

GetTransientCommands returns a copy of all transient commands memorises for execution. If there is none, it returns an empty string array.

func (*RecurringCommands) Initialise

func (cmds *RecurringCommands) Initialise() error

Initialise prepares internal states of a new RecurringCommands.

func (*RecurringCommands) Start

func (cmds *RecurringCommands) Start()

Start runs an infinite loop to execute all commands one after another, then sleep for an interval. The function blocks caller until Stop function is called. If Start function is already running, calling it a second time will do nothing and return immediately.

func (*RecurringCommands) Stop

func (cmds *RecurringCommands) Stop()

Stop informs the running command processing loop to terminate as early as possible. Blocks until the loop has terminated. Calling the function while command processing loop is not running yields no effect.

type TCPApp

type TCPApp interface {
	// GetTCPStatsCollector returns the stats collector that counts and times client connections for the TCP application.
	GetTCPStatsCollector() *misc.Stats
	// HandleTCPConnection converses with the TCP client. The client connection is closed by server upon returning from the implementation.
	HandleTCPConnection(*lalog.Logger, string, *net.TCPConn)
}

TCPApp defines routines for a TCP server application to accept, process, and interact with client connections.

type TCPServer

type TCPServer struct {
	// ListenAddr is the IP address to listen on. Use 0.0.0.0 to listen on all network interfaces.
	ListenAddr string
	// ListenPort is the port number to listen on.
	ListenPort int
	// AppName is a human readable name that identifies the server application in log entries.
	AppName string
	// App is the concrete implementation of TCP server application.
	App TCPApp
	/*
		LimitPerSec is the maximum number of actions and connections acceptable from a single IP at a time.
		Once the limit is reached, new connections from the IP will be closed right away, and existing conversations are
		terminated.
	*/
	LimitPerSec int
	// contains filtered or unexported fields
}

TCPServer implements common routines for a TCP server that interacts with unlimited number of clients while applying a rate limit.

func NewTCPServer

func NewTCPServer(listenAddr string, listenPort int, appName string, app TCPApp, limitPerSec int) (srv *TCPServer)

NewTCPServer constructs a new TCP server and initialises its internal structures.

func (*TCPServer) AddAndCheckRateLimit

func (srv *TCPServer) AddAndCheckRateLimit(clientIP string) bool

AddAndCheckRateLimit may be optionally invoked by TCP application in the middle of an ongoing conversation to check whether conversation is going on too fast.

func (*TCPServer) Initialise

func (srv *TCPServer) Initialise()

Initialise initialises the internal structures of the TCP server, preparing it for accepting clients.

func (*TCPServer) StartAndBlock

func (srv *TCPServer) StartAndBlock() error

StartAndBlock starts TCP listener to process client connections and blocks until the server is told to stop. Call this function after having initialised the TCP server.

func (*TCPServer) Stop

func (srv *TCPServer) Stop()

Stop the TCP server from accepting new connections. Ongoing connections will continue nonetheless.

type UDPApp

type UDPApp interface {
	// GetUDPStatsCollector returns the stats collector that counts and times UDP conversations.
	GetUDPStatsCollector() *misc.Stats
	// HandleUDPClient converses with a UDP client based on a received packet.
	HandleUDPClient(*lalog.Logger, string, *net.UDPAddr, []byte, *net.UDPConn)
}

UDPApp defines routines for a UDP server to read, process, and interact with UDP clients.

type UDPServer

type UDPServer struct {
	// ListenAddr is the IP address to listen on. Use 0.0.0.0 to listen on all network interfaces.
	ListenAddr string
	// ListenPort is the port number to listen on.
	ListenPort int
	// AppName is a human readable name that identifies the server application in log entries.
	AppName string
	// App is the concrete implementation of UDP server application.
	App UDPApp
	/*
		LimitPerSec is the maximum number of actions and connections acceptable from a single IP at a time.
		Once the limit is reached, new connections from the IP will be closed right away, and existing conversations are
		terminated.
	*/
	LimitPerSec int
	// contains filtered or unexported fields
}

UDPServer implements common routines for a UDP server that interacts with unlimited number of clients while applying a rate limit.

func NewUDPServer

func NewUDPServer(listenAddr string, listenPort int, appName string, app UDPApp, limitPerSec int) (srv *UDPServer)

NewUDPServer constructs a new UDP server and initialises its internal structures.

func (*UDPServer) AddAndCheckRateLimit

func (srv *UDPServer) AddAndCheckRateLimit(clientIP string) bool

AddAndCheckRateLimit may be optionally invoked by UDP application in the middle of an ongoing conversation to check whether conversation is going on too fast.

func (*UDPServer) Initialise

func (srv *UDPServer) Initialise()

Initialise initialises the internal structures of UDP server, preparing it for processing clients.

func (*UDPServer) IsRunning

func (srv *UDPServer) IsRunning() bool

IsRunning returns true only if the server has started and has not been told to stop.

func (*UDPServer) StartAndBlock

func (srv *UDPServer) StartAndBlock() error

StartAndBlock starts UDP listener to process clients and blocks until the server is told to stop. Call this function after having initialised the UDP server.

func (*UDPServer) Stop

func (srv *UDPServer) Stop()

Stop the UDP server from accepting new clients. Ongoing conversations will continue nonetheless.

Jump to

Keyboard shortcuts

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