connection

package
v0.31.11 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2023 License: AGPL-3.0 Imports: 22 Imported by: 1

Documentation

Index

Constants

View Source
const (
	// PruningEnabled is a boolean flag to enable pruning of connections to peers that are not part of
	// the explicit update list.
	// If set to true, the connector will prune connections to peers that are not part of the explicit update list.
	PruningEnabled = true

	// PruningDisabled is a boolean flag to disable pruning of connections to peers that are not part of
	// the explicit update list.
	// If set to false, the connector will not prune connections to peers that are not part of the explicit update list.
	PruningDisabled = false
)

Variables

View Source
var DefaultPeerUpdateInterval = 10 * time.Minute

DefaultPeerUpdateInterval is default duration for which the peer manager waits in between attempts to update peer connections

Functions

func DefaultLibp2pBackoffConnectorFactory added in v0.31.0

func DefaultLibp2pBackoffConnectorFactory(host host.Host) func() (*discoveryBackoff.BackoffConnector, error)

DefaultLibp2pBackoffConnectorFactory is a factory function to create a new BackoffConnector. It uses the default values for the backoff connector. (https://github.com/libp2p/go-libp2p-pubsub/blob/master/discovery.go#L34)

Types

type ConnGater

type ConnGater struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

ConnGater is the implementation of the libp2p connmgr.ConnectionGater interface It provides node allowlisting by libp2p peer.ID which is derived from the node public networking key

func NewConnGater

func NewConnGater(log zerolog.Logger, identityProvider module.IdentityProvider, opts ...ConnGaterOption) *ConnGater

func (*ConnGater) InterceptAccept

func (c *ConnGater) InterceptAccept(cm network.ConnMultiaddrs) bool

InterceptAccept is not used. Currently, allowlisting is only implemented by Peer IDs and not multi-addresses

func (*ConnGater) InterceptAddrDial

func (c *ConnGater) InterceptAddrDial(_ peer.ID, ma multiaddr.Multiaddr) bool

InterceptAddrDial is not used. Currently, allowlisting is only implemented by Peer IDs and not multi-addresses

func (*ConnGater) InterceptPeerDial

func (c *ConnGater) InterceptPeerDial(p peer.ID) bool

InterceptPeerDial - a callback which allows or disallows outbound connection

func (*ConnGater) InterceptSecured

func (c *ConnGater) InterceptSecured(dir network.Direction, p peer.ID, addr network.ConnMultiaddrs) bool

InterceptSecured a callback executed after the libp2p security handshake. It tests whether to accept or reject an inbound connection based on its peer id.

func (*ConnGater) InterceptUpgraded

func (c *ConnGater) InterceptUpgraded(network.Conn) (allow bool, reason control.DisconnectReason)

InterceptUpgraded decision to continue or drop the connection should have been made before this call

type ConnGaterOption

type ConnGaterOption func(*ConnGater)

ConnGaterOption allow the connection gater to be configured with a list of PeerFilter funcs for a specific conn gater callback. In the current implementation of the ConnGater the following callbacks can be configured with peer filters. * InterceptPeerDial - peer filters can be configured with WithOnInterceptPeerDialFilters which will allow or disallow outbound connections. * InterceptSecured - peer filters can be configured with WithOnInterceptSecuredFilters which will allow or disallow inbound connections after libP2P security handshake.

func WithOnInterceptPeerDialFilters

func WithOnInterceptPeerDialFilters(filters []p2p.PeerFilter) ConnGaterOption

WithOnInterceptPeerDialFilters sets peer filters for outbound connections.

func WithOnInterceptSecuredFilters

func WithOnInterceptSecuredFilters(filters []p2p.PeerFilter) ConnGaterOption

WithOnInterceptSecuredFilters sets peer filters for inbound secured connections.

type ConnManager

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

ConnManager provides an implementation of Libp2p's ConnManager interface (https://pkg.go.dev/github.com/libp2p/go-libp2p/core/connmgr#ConnManager) It is called back by libp2p when certain events occur such as opening/closing a stream, opening/closing connection etc. Current implementation primarily acts as a wrapper around libp2p's BasicConnMgr (https://pkg.go.dev/github.com/libp2p/go-libp2p/p2p/net/connmgr#BasicConnMgr). However, we override the notifiee callback to add additional functionality so that it provides metrics and logging instrumentation for Flow.

func NewConnManager

func NewConnManager(logger zerolog.Logger, metric module.LibP2PConnectionMetrics, cfg *ManagerConfig) (*ConnManager, error)

NewConnManager creates a new connection manager. It errors if creating the basic connection manager of libp2p fails. The error is not benign, and we should crash the node if it happens. It is a malpractice to start the node without connection manager.

func (*ConnManager) Close added in v0.30.0

func (cm *ConnManager) Close() error

func (*ConnManager) GetTagInfo added in v0.30.0

func (cm *ConnManager) GetTagInfo(p peer.ID) *connmgr.TagInfo

func (*ConnManager) IsProtected

func (cm *ConnManager) IsProtected(id peer.ID, tag string) bool

func (*ConnManager) Notifee

func (cm *ConnManager) Notifee() network.Notifiee

func (*ConnManager) Protect

func (cm *ConnManager) Protect(id peer.ID, tag string)

func (*ConnManager) TagPeer added in v0.30.0

func (cm *ConnManager) TagPeer(id peer.ID, s string, i int)

func (*ConnManager) TrimOpenConns added in v0.30.0

func (cm *ConnManager) TrimOpenConns(ctx context.Context)

func (*ConnManager) Unprotect

func (cm *ConnManager) Unprotect(id peer.ID, tag string) bool

func (*ConnManager) UntagPeer added in v0.30.0

func (cm *ConnManager) UntagPeer(p peer.ID, tag string)

func (*ConnManager) UpsertTag added in v0.30.0

func (cm *ConnManager) UpsertTag(p peer.ID, tag string, upsert func(int) int)

type ConnectorConfig added in v0.31.0

type ConnectorConfig struct {
	// PruneConnections is a boolean flag to enable pruning of connections to peers that are not part of the explicit update list.
	PruneConnections bool

	// Logger is the logger to be used by the connector
	Logger zerolog.Logger

	// Host is the libp2p host to be used by the connector.
	Host p2p.ConnectorHost

	// BackoffConnectorFactory is a factory function to create a new BackoffConnector.
	BackoffConnectorFactory func() (*discoveryBackoff.BackoffConnector, error)
}

ConnectorConfig is the configuration for the libp2p based connector.

type ConnectorHost added in v0.31.0

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

ConnectorHost is a wrapper around the libp2p host.Host interface to provide the required functionality for the Connector interface.

func NewConnectorHost added in v0.31.0

func NewConnectorHost(h host.Host) *ConnectorHost

func (*ConnectorHost) ClosePeer added in v0.31.0

func (c *ConnectorHost) ClosePeer(id peer.ID) error

ClosePeer closes the connection to the given peer.ID. Args:

id: peer.ID for which the connection is to be closed

Returns:

error if there is any error while closing the connection to the given peer.ID. All errors are benign.

func (*ConnectorHost) Connections added in v0.31.0

func (c *ConnectorHost) Connections() []network.Conn

Connections returns all the connections of the underlying host.

func (*ConnectorHost) ID added in v0.31.0

func (c *ConnectorHost) ID() peer.ID

ID returns the peer.ID of the underlying host. Returns:

peer.ID of the underlying host.

ID returns the peer.ID of the underlying host.

func (*ConnectorHost) IsProtected added in v0.31.0

func (c *ConnectorHost) IsProtected(id peer.ID) bool

IsProtected returns true if the given peer.ID is protected from pruning. Args:

id: peer.ID for which the protection status is requested

Returns:

true if the given peer.ID is protected from pruning

func (*ConnectorHost) PeerInfo added in v0.31.0

func (c *ConnectorHost) PeerInfo(id peer.ID) peer.AddrInfo

PeerInfo returns the peer.AddrInfo for the given peer.ID. Args:

id: peer.ID for which the peer.AddrInfo is requested

Returns:

peer.AddrInfo for the given peer.ID

type Libp2pConnector

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

Libp2pConnector is a libp2p based Connector implementation to connect and disconnect from peers

func NewLibp2pConnector

func NewLibp2pConnector(cfg *ConnectorConfig) (*Libp2pConnector, error)

NewLibp2pConnector creates a new libp2p based connector Args:

  • cfg: configuration for the connector

Returns:

  • *Libp2pConnector: a new libp2p based connector
  • error: an error if there is any error while creating the connector. The errors are irrecoverable and unexpected.

func (*Libp2pConnector) UpdatePeers

func (l *Libp2pConnector) UpdatePeers(ctx context.Context, peerIDs peer.IDSlice)

UpdatePeers is the implementation of the Connector.UpdatePeers function. It connects to all of the ids and disconnects from any other connection that the libp2p node might have.

type ManagerConfig added in v0.30.0

type ManagerConfig struct {
	// HighWatermark and LowWatermark govern the number of connections are maintained by the ConnManager.
	// When the peer count exceeds the HighWatermark, as many peers will be pruned (and
	// their connections terminated) until LowWatermark peers remain. In other words, whenever the
	// peer count is x > HighWatermark, the ConnManager will prune x - LowWatermark peers.
	// The pruning algorithm is as follows:
	// 1. The ConnManager will not prune any peers that have been connected for less than GracePeriod.
	// 2. The ConnManager will not prune any peers that are protected.
	// 3. The ConnManager will sort the peers based on their number of streams and direction of connections, and
	// prunes the peers with the least number of streams. If there are ties, the peer with the incoming connection
	// will be pruned. If both peers have incoming connections, and there are still ties, one of the peers will be
	// pruned at random.
	// Algorithm implementation is in https://github.com/libp2p/go-libp2p/blob/master/p2p/net/connmgr/connmgr.go#L262-L318
	HighWatermark int // naming from libp2p
	LowWatermark  int // naming from libp2p

	// SilencePeriod is the time to wait before start pruning connections.
	SilencePeriod time.Duration // naming from libp2p
	// GracePeriod is the time to wait before pruning a new connection.
	GracePeriod time.Duration // naming from libp2p
}

func DefaultConnManagerConfig added in v0.30.0

func DefaultConnManagerConfig() *ManagerConfig

DefaultConnManagerConfig returns the default configuration for the connection manager.

type PeerManager

type PeerManager struct {
	component.Component
	// contains filtered or unexported fields
}

PeerManager adds and removes connections to peers periodically and on request

func NewPeerManager

func NewPeerManager(logger zerolog.Logger, updateInterval time.Duration, connector p2p.Connector) *PeerManager

NewPeerManager creates a new peer manager which calls the peersProvider callback to get a list of peers to connect to and it uses the connector to actually connect or disconnect from peers.

func (*PeerManager) ForceUpdatePeers

func (pm *PeerManager) ForceUpdatePeers(ctx context.Context)

ForceUpdatePeers initiates an update to the peer connections of this node immediately

func (*PeerManager) OnRateLimitedPeer added in v0.30.0

func (pm *PeerManager) OnRateLimitedPeer(pid peer.ID, role, msgType, topic, reason string)

OnRateLimitedPeer rate limiter distributor consumer func that will be called when a peer is rate limited, the rate limited peer is disconnected immediately after being rate limited.

func (*PeerManager) RequestPeerUpdate

func (pm *PeerManager) RequestPeerUpdate()

RequestPeerUpdate requests an update to the peer connections of this node. If a peer update has already been requested (either as a periodic request or an on-demand request) and is outstanding, then this call is a no-op.

func (*PeerManager) SetPeersProvider

func (pm *PeerManager) SetPeersProvider(peersProvider p2p.PeersProvider)

SetPeersProvider sets the peers provider SetPeersProvider may be called at most once

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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