Documentation
¶
Overview ¶
Package connmanager owns the lifecycle of network connections between Dingo and its peers and clients. It accepts inbound connections on configured listeners (TCP, Unix sockets, Windows named pipes), opens outbound connections on demand, and enforces per-IP and global connection caps.
ConnectionManager is the top-level type. It publishes events on the EventBus for every connection lifecycle transition — inbound, outbound, closed, recycle-requested — which the ouroboros, peergov, and chainselection packages subscribe to.
This package intentionally does not know anything about Ouroboros mini-protocols. It hands accepted connections over to the ouroboros package, which sets up the chainsync, blockfetch, keepalive, and other mini-protocol handlers.
The connection manager is also the recipient of ConnectionRecycleRequestedEvent from the node's stall recycler: when a peer is selected for recycling, this package is the one that actually closes the underlying socket.
Index ¶
- Constants
- func NormalizePeerAddr(peerAddr string) string
- type ConnectionClosedEvent
- type ConnectionManager
- func (c *ConnectionManager) AddConnection(conn *ouroboros.Connection, isInbound bool, peerAddr string) bool
- func (c *ConnectionManager) CreateOutboundConn(ctx context.Context, address string) (*ouroboros.Connection, error)
- func (c *ConnectionManager) GetConnectionById(connId ouroboros.ConnectionId) *ouroboros.Connection
- func (c *ConnectionManager) HandleConnectionRecycleRequestedEvent(evt event.Event)
- func (c *ConnectionManager) HasInboundPeerAddress(peerAddr string) bool
- func (c *ConnectionManager) IPConnCount(ipKey string) int
- func (c *ConnectionManager) InboundCount() int
- func (c *ConnectionManager) IsInboundConnection(connId ouroboros.ConnectionId) bool
- func (c *ConnectionManager) RemoveConnection(connId ouroboros.ConnectionId, conn *ouroboros.Connection) bool
- func (c *ConnectionManager) Start(ctx context.Context) error
- func (c *ConnectionManager) Stop(ctx context.Context) error
- type ConnectionManagerConfig
- type ConnectionManagerConnClosedFunc
- type ConnectionRecycleRequestedEvent
- type InboundConnectionEvent
- type ListenerConfig
- type UnixConn
- type UnixConnAddr
Constants ¶
const ( InboundConnectionEventType = "connmanager.inbound_conn" ConnectionClosedEventType = "connmanager.conn_closed" ConnectionRecycleRequestedEventType = "connmanager.connection_recycle_requested" )
const DefaultMaxConnectionsPerIP = 5
DefaultMaxConnectionsPerIP is the default maximum number of concurrent connections allowed from a single IP address (or /64 prefix for IPv6).
const ( // DefaultMaxInboundConnections is the default maximum number of // simultaneous inbound connections accepted by the connection manager. // This prevents resource exhaustion from malicious or accidental // connection floods. DefaultMaxInboundConnections = 100 )
Variables ¶
This section is empty.
Functions ¶
func NormalizePeerAddr ¶ added in v0.37.0
NormalizePeerAddr canonicalizes a host:port string into the form used as the connmanager's transport-layer peer key: IPs are normalized to their canonical form, hostnames are lowercased, and DNS is never consulted. Exposed so that subscribers of InboundConnectionEvent can key on the same identity the connection manager does, and so the inbound-arrival path in peergov does not drift from inboundPeerAddrs / HasInboundPeerAddress.
Types ¶
type ConnectionClosedEvent ¶ added in v0.2.0
type ConnectionClosedEvent struct {
ConnectionId ouroboros.ConnectionId
Error error
}
type ConnectionManager ¶
type ConnectionManager struct {
// contains filtered or unexported fields
}
func NewConnectionManager ¶
func NewConnectionManager(cfg ConnectionManagerConfig) *ConnectionManager
func (*ConnectionManager) AddConnection ¶
func (c *ConnectionManager) AddConnection( conn *ouroboros.Connection, isInbound bool, peerAddr string, ) bool
func (*ConnectionManager) CreateOutboundConn ¶ added in v0.2.0
func (c *ConnectionManager) CreateOutboundConn( ctx context.Context, address string, ) (*ouroboros.Connection, error)
func (*ConnectionManager) GetConnectionById ¶
func (c *ConnectionManager) GetConnectionById( connId ouroboros.ConnectionId, ) *ouroboros.Connection
func (*ConnectionManager) HandleConnectionRecycleRequestedEvent ¶ added in v0.22.0
func (c *ConnectionManager) HandleConnectionRecycleRequestedEvent( evt event.Event, )
HandleConnectionRecycleRequestedEvent closes a connection when a recycle request event is received.
func (*ConnectionManager) HasInboundPeerAddress ¶ added in v0.27.1
func (c *ConnectionManager) HasInboundPeerAddress( peerAddr string, ) bool
HasInboundPeerAddress returns true if there is already an inbound connection from the same remote peer address as peerAddr.
This intentionally matches on the full remote address, not just the host. TCP collisions require the same 4-tuple, so a connection from the same host but a different source port must not suppress a valid outbound dial.
When OutboundSourcePort is set (listen-port reuse), an outbound connection to the same remote peer address may collide with an existing inbound connection if the peer also connected from its listening port. In that case the existing inbound connection should be treated as the reusable duplex connection, matching ouroboros-network's exact-address connection tracking.
func (*ConnectionManager) IPConnCount ¶ added in v0.22.0
func (c *ConnectionManager) IPConnCount(ipKey string) int
ipConnCount returns the current connection count for an IP key. Exported for testing only.
func (*ConnectionManager) InboundCount ¶ added in v0.22.0
func (c *ConnectionManager) InboundCount() int
InboundCount returns the current number of inbound connections.
func (*ConnectionManager) IsInboundConnection ¶ added in v0.27.4
func (c *ConnectionManager) IsInboundConnection( connId ouroboros.ConnectionId, ) bool
IsInboundConnection returns true if the given connection ID is an inbound connection (a remote peer connected to us). Inbound peers are clients pulling data from us and should not be treated as chain truth sources.
func (*ConnectionManager) RemoveConnection ¶
func (c *ConnectionManager) RemoveConnection( connId ouroboros.ConnectionId, conn *ouroboros.Connection, ) bool
type ConnectionManagerConfig ¶
type ConnectionManagerConfig struct {
PromRegistry prometheus.Registerer
Logger *slog.Logger
EventBus *event.EventBus
ConnClosedFunc ConnectionManagerConnClosedFunc
Listeners []ListenerConfig
OutboundConnOpts []ouroboros.ConnectionOptionFunc
OutboundSourcePort uint
MaxInboundConns int // 0 means use DefaultMaxInboundConnections
// MaxConnectionsPerIP limits the number of concurrent inbound
// connections from the same IP address. IPv6 addresses are grouped
// by /64 prefix. A value of 0 means use DefaultMaxConnectionsPerIP.
MaxConnectionsPerIP int
}
type ConnectionManagerConnClosedFunc ¶
type ConnectionManagerConnClosedFunc func(ouroboros.ConnectionId, error)
ConnectionManagerConnClosedFunc is a function that takes a connection ID and an optional error
type ConnectionRecycleRequestedEvent ¶ added in v0.22.0
type ConnectionRecycleRequestedEvent struct {
ConnectionId ouroboros.ConnectionId
ConnKey string
Reason string
}
ConnectionRecycleRequestedEvent requests that a specific connection be recycled.
type InboundConnectionEvent ¶ added in v0.1.7
type InboundConnectionEvent struct {
ConnectionId ouroboros.ConnectionId
LocalAddr net.Addr
RemoteAddr net.Addr
IsNtC bool // true for node-to-client (local) connections
// NormalizedRemoteAddr is RemoteAddr.String() passed through
// NormalizePeerAddr, so subscribers key on the same transport
// identity the connection manager uses internally (and exposes via
// HasInboundPeerAddress). Populated by the listener; empty on
// events synthesized without a listener, in which case subscribers
// must call NormalizePeerAddr themselves to preserve the contract.
NormalizedRemoteAddr string
// IsDuplex reports whether the handshake negotiated
// InitiatorAndResponder (full-duplex) mode. Populated by the listener
// after ouroboros.NewConnection completes. When false, subscribers
// should treat the value as best-effort and fall back to
// ConnectionManager.GetConnectionById for authoritative state.
IsDuplex bool
}
type ListenerConfig ¶ added in v0.1.7
type UnixConn ¶ added in v0.4.0
UnixConn is a wrapper around net.UnixConn that provides a unique remote address
func (UnixConn) RemoteAddr ¶ added in v0.4.0
type UnixConnAddr ¶ added in v0.4.0
type UnixConnAddr struct {
// contains filtered or unexported fields
}
func (UnixConnAddr) Network ¶ added in v0.4.0
func (UnixConnAddr) Network() string
func (UnixConnAddr) String ¶ added in v0.4.0
func (u UnixConnAddr) String() string