go-waku/waku/v2/protocol/peer_exchange/waku_peer_exchange_option.go

71 lines
2.3 KiB
Go
Raw Normal View History

2022-10-23 13:13:43 +00:00
package peer_exchange
import (
"context"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/waku-org/go-waku/waku/v2/peermanager"
"github.com/waku-org/go-waku/waku/v2/utils"
2022-10-23 13:13:43 +00:00
"go.uber.org/zap"
)
type PeerExchangeParameters struct {
host host.Host
selectedPeer peer.ID
pm *peermanager.PeerManager
2022-10-23 13:13:43 +00:00
log *zap.Logger
}
type PeerExchangeOption func(*PeerExchangeParameters)
// WithPeer is an option used to specify the peerID to push a waku message to
func WithPeer(p peer.ID) PeerExchangeOption {
return func(params *PeerExchangeParameters) {
params.selectedPeer = p
}
}
// WithAutomaticPeerSelection is an option used to randomly select a peer from the peer store
// to obtains peers from. If a list of specific peers is passed, the peer will be chosen
// from that list assuming it supports the chosen protocol, otherwise it will chose a peer
// from the node peerstore
func WithAutomaticPeerSelection(fromThesePeers ...peer.ID) PeerExchangeOption {
2022-10-23 13:13:43 +00:00
return func(params *PeerExchangeParameters) {
var p peer.ID
var err error
if params.pm == nil {
p, err = utils.SelectPeer(params.host, PeerExchangeID_v20alpha1, fromThesePeers, params.log)
} else {
p, err = params.pm.SelectPeer(PeerExchangeID_v20alpha1, fromThesePeers)
}
2022-10-23 13:13:43 +00:00
if err == nil {
params.selectedPeer = p
2022-10-23 13:13:43 +00:00
} else {
params.log.Info("selecting peer", zap.Error(err))
}
}
}
// WithFastestPeerSelection is an option used to select a peer from the peer store
// with the lowest ping. If a list of specific peers is passed, the peer will be chosen
// from that list assuming it supports the chosen protocol, otherwise it will chose a peer
// from the node peerstore
func WithFastestPeerSelection(ctx context.Context, fromThesePeers ...peer.ID) PeerExchangeOption {
2022-10-23 13:13:43 +00:00
return func(params *PeerExchangeParameters) {
2023-02-16 16:17:52 +00:00
p, err := utils.SelectPeerWithLowestRTT(ctx, params.host, PeerExchangeID_v20alpha1, fromThesePeers, params.log)
2022-10-23 13:13:43 +00:00
if err == nil {
params.selectedPeer = p
2022-10-23 13:13:43 +00:00
} else {
params.log.Info("selecting peer", zap.Error(err))
}
}
}
// DefaultOptions are the default options to be used when using the lightpush protocol
func DefaultOptions(host host.Host) []PeerExchangeOption {
return []PeerExchangeOption{
WithAutomaticPeerSelection(),
}
}