keepalive

package
v0.140.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2025 License: Apache-2.0 Imports: 6 Imported by: 3

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

View Source
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
)
View Source
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.

View Source
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

View Source
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")
)

StateMap defines the valid state transitions for the keep-alive protocol.

Functions

func NewMsgFromCbor

func NewMsgFromCbor(msgType uint, data []byte) (protocol.Message, error)

NewMsgFromCbor decodes a CBOR-encoded message of the given type and returns the corresponding protocol.Message.

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

type Client struct {
	*protocol.Protocol
	// contains filtered or unexported fields
}

Client implements the keep-alive protocol client, responsible for sending keep-alive messages and handling responses.

func NewClient

func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client

NewClient creates and returns a new keep-alive protocol client with the given options and configuration.

func (*Client) Start

func (c *Client) Start()

Start begins the keep-alive protocol client and starts sending keep-alive messages at the configured interval.

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

type KeepAlive struct {
	Client *Client
	Server *Server
}

KeepAlive provides both client and server implementations of the keep-alive protocol.

func New

func New(protoOptions protocol.ProtocolOptions, cfg *Config) *KeepAlive

New creates and returns a new KeepAlive protocol instance using the provided protocol options and configuration.

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.

func NewMsgDone

func NewMsgDone() *MsgDone

NewMsgDone creates and returns a new done message.

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.

type Server

type Server struct {
	*protocol.Protocol
	// contains filtered or unexported fields
}

Server implements the keep-alive protocol server, responsible for handling keep-alive requests and sending responses.

func NewServer

func NewServer(protoOptions protocol.ProtocolOptions, cfg *Config) *Server

NewServer creates and returns a new keep-alive protocol server with the given options and configuration.

Jump to

Keyboard shortcuts

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