Documentation
¶
Overview ¶
Package keepalive implements the Ouroboros KeepAlive mini-protocol, which is used to detect and maintain liveness between nodes in a network.
Index ¶
- Constants
- Variables
- func NewMsgFromCbor(msgType uint, data []byte) (protocol.Message, error)
- type CallbackContext
- type Client
- type Config
- type DoneFunc
- type KeepAlive
- type KeepAliveFunc
- type KeepAliveOptionFunc
- func WithCookie(cookie uint16) KeepAliveOptionFunc
- func WithDoneFunc(doneFunc DoneFunc) KeepAliveOptionFunc
- func WithKeepAliveFunc(keepAliveFunc KeepAliveFunc) KeepAliveOptionFunc
- func WithKeepAliveResponseFunc(keepAliveResponseFunc KeepAliveResponseFunc) KeepAliveOptionFunc
- func WithPeriod(period time.Duration) KeepAliveOptionFunc
- func WithTimeout(timeout time.Duration) KeepAliveOptionFunc
- type KeepAliveResponseFunc
- type MsgDone
- type MsgKeepAlive
- type MsgKeepAliveResponse
- type Server
Constants ¶
const ( // ProtocolName is the name of the keep-alive protocol. ProtocolName = "keep-alive" // ProtocolId is the unique protocol identifier for the keep-alive protocol. ProtocolId uint16 = 8 // DefaultKeepAlivePeriod is the default interval between keep-alive probes, in seconds. DefaultKeepAlivePeriod = 60 // DefaultKeepAliveTimeout is the default timeout for keep-alive responses, in seconds. DefaultKeepAliveTimeout = 10 )
const ( // ClientTimeout is the maximum time the server waits to receive the next keep-alive ping from the client (while in the "Client" protocol state). ClientTimeout = 60 * time.Second // ServerTimeout is the maximum time the client waits for a keep-alive pong from the server (while in the "Server" protocol state). ServerTimeout = 10 * time.Second )
Protocol state timeout constants as specified by the network protocol.
const ( // MessageTypeKeepAlive is the message type for keep-alive requests. MessageTypeKeepAlive = 0 // MessageTypeKeepAliveResponse is the message type for keep-alive responses. MessageTypeKeepAliveResponse = 1 // MessageTypeDone is the message type for done messages. MessageTypeDone = 2 )
Variables ¶
var ( // StateClient is the protocol state for the client. StateClient = protocol.NewState(1, "Client") // StateServer is the protocol state for the server. StateServer = protocol.NewState(2, "Server") // StateDone is the protocol state indicating completion. StateDone = protocol.NewState(3, "Done") )
var StateMap = protocol.StateMap{ StateClient: protocol.StateMapEntry{ Agency: protocol.AgencyClient, Timeout: ClientTimeout, Transitions: []protocol.StateTransition{ { MsgType: MessageTypeKeepAlive, NewState: StateServer, }, { MsgType: MessageTypeDone, NewState: StateDone, }, }, }, StateServer: protocol.StateMapEntry{ Agency: protocol.AgencyServer, Timeout: ServerTimeout, Transitions: []protocol.StateTransition{ { MsgType: MessageTypeKeepAliveResponse, NewState: StateClient, }, }, }, StateDone: protocol.StateMapEntry{ Agency: protocol.AgencyNone, }, }
StateMap defines the valid state transitions for the keep-alive protocol.
Functions ¶
Types ¶
type CallbackContext ¶ added in v0.78.0
type CallbackContext struct {
ConnectionId connection.ConnectionId
Client *Client
Server *Server
}
CallbackContext provides context information to keep-alive protocol callbacks, including connection and role references.
type Client ¶
Client implements the keep-alive protocol client, responsible for sending keep-alive messages and handling responses.
type Config ¶
type Config struct {
KeepAliveFunc KeepAliveFunc
KeepAliveResponseFunc KeepAliveResponseFunc
DoneFunc DoneFunc
Timeout time.Duration
Period time.Duration
Cookie uint16
}
Config contains configuration options for the keep-alive protocol, including callbacks and timing parameters.
func NewConfig ¶
func NewConfig(options ...KeepAliveOptionFunc) Config
NewConfig creates a new Config with default values, applying any provided option functions.
type DoneFunc ¶
type DoneFunc func(CallbackContext) error
DoneFunc is a callback function type for handling done messages.
type KeepAlive ¶
KeepAlive provides both client and server implementations of the keep-alive protocol.
type KeepAliveFunc ¶
type KeepAliveFunc func(CallbackContext, uint16) error
KeepAliveFunc is a callback function type for handling keep-alive messages.
type KeepAliveOptionFunc ¶
type KeepAliveOptionFunc func(*Config)
KeepAliveOptionFunc is a function that modifies a Config.
func WithCookie ¶ added in v0.65.0
func WithCookie(cookie uint16) KeepAliveOptionFunc
WithCookie sets the cookie value in the Config.
func WithDoneFunc ¶
func WithDoneFunc(doneFunc DoneFunc) KeepAliveOptionFunc
WithDoneFunc sets the DoneFunc callback in the Config.
func WithKeepAliveFunc ¶
func WithKeepAliveFunc(keepAliveFunc KeepAliveFunc) KeepAliveOptionFunc
WithKeepAliveFunc sets the KeepAliveFunc callback in the Config.
func WithKeepAliveResponseFunc ¶
func WithKeepAliveResponseFunc( keepAliveResponseFunc KeepAliveResponseFunc, ) KeepAliveOptionFunc
WithKeepAliveResponseFunc sets the KeepAliveResponseFunc callback in the Config.
func WithPeriod ¶
func WithPeriod(period time.Duration) KeepAliveOptionFunc
WithPeriod sets the keep-alive period duration in the Config.
func WithTimeout ¶
func WithTimeout(timeout time.Duration) KeepAliveOptionFunc
WithTimeout sets the timeout duration in the Config.
type KeepAliveResponseFunc ¶
type KeepAliveResponseFunc func(CallbackContext, uint16) error
KeepAliveResponseFunc is a callback function type for handling keep-alive response messages.
type MsgDone ¶
type MsgDone struct {
protocol.MessageBase
}
MsgDone represents a done message in the keep-alive protocol, indicating the end of the session.
type MsgKeepAlive ¶
type MsgKeepAlive struct {
protocol.MessageBase
Cookie uint16
}
MsgKeepAlive represents a keep-alive request message containing a cookie value.
func NewMsgKeepAlive ¶
func NewMsgKeepAlive(cookie uint16) *MsgKeepAlive
NewMsgKeepAlive creates and returns a new keep-alive request message with the given cookie value.
type MsgKeepAliveResponse ¶
type MsgKeepAliveResponse struct {
protocol.MessageBase
Cookie uint16
}
MsgKeepAliveResponse represents a keep-alive response message containing a cookie value.
func NewMsgKeepAliveResponse ¶
func NewMsgKeepAliveResponse(cookie uint16) *MsgKeepAliveResponse
NewMsgKeepAliveResponse creates and returns a new keep-alive response message with the given cookie value.