2021-11-07 12:08:29 +00:00
|
|
|
package lightpush
|
|
|
|
|
|
|
|
import (
|
2021-11-09 23:34:04 +00:00
|
|
|
"context"
|
|
|
|
|
2022-10-19 19:39:32 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/host"
|
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2023-08-10 12:58:22 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/peermanager"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/utils"
|
2022-01-18 18:17:06 +00:00
|
|
|
"go.uber.org/zap"
|
2021-11-07 12:08:29 +00:00
|
|
|
)
|
|
|
|
|
2023-07-19 16:25:35 +00:00
|
|
|
type lightPushParameters struct {
|
2021-11-09 23:34:04 +00:00
|
|
|
host host.Host
|
2021-11-07 12:08:29 +00:00
|
|
|
selectedPeer peer.ID
|
2023-07-19 16:25:35 +00:00
|
|
|
requestID []byte
|
2023-08-10 12:58:22 +00:00
|
|
|
pm *peermanager.PeerManager
|
2022-05-30 15:55:30 +00:00
|
|
|
log *zap.Logger
|
2021-11-07 12:08:29 +00:00
|
|
|
}
|
|
|
|
|
2023-07-19 16:25:35 +00:00
|
|
|
// Option is the type of options accepted when performing LightPush protocol requests
|
|
|
|
type Option func(*lightPushParameters)
|
2021-11-07 12:08:29 +00:00
|
|
|
|
2022-07-25 15:28:17 +00:00
|
|
|
// WithPeer is an option used to specify the peerID to push a waku message to
|
2023-07-19 16:25:35 +00:00
|
|
|
func WithPeer(p peer.ID) Option {
|
|
|
|
return func(params *lightPushParameters) {
|
2021-11-07 12:08:29 +00:00
|
|
|
params.selectedPeer = p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-25 15:28:17 +00:00
|
|
|
// WithAutomaticPeerSelection is an option used to randomly select a peer from the peer store
|
2022-11-24 21:50:43 +00:00
|
|
|
// to push a waku message to. 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
|
2023-07-19 16:25:35 +00:00
|
|
|
func WithAutomaticPeerSelection(fromThesePeers ...peer.ID) Option {
|
|
|
|
return func(params *lightPushParameters) {
|
2023-08-10 12:58:22 +00:00
|
|
|
var p peer.ID
|
|
|
|
var err error
|
|
|
|
if params.pm == nil {
|
|
|
|
p, err = utils.SelectPeer(params.host, LightPushID_v20beta1, fromThesePeers, params.log)
|
|
|
|
} else {
|
|
|
|
p, err = params.pm.SelectPeer(LightPushID_v20beta1, fromThesePeers, params.log)
|
|
|
|
}
|
2021-11-07 12:08:29 +00:00
|
|
|
if err == nil {
|
2023-01-08 18:33:30 +00:00
|
|
|
params.selectedPeer = p
|
2021-11-07 12:08:29 +00:00
|
|
|
} else {
|
2022-05-30 15:55:30 +00:00
|
|
|
params.log.Info("selecting peer", zap.Error(err))
|
2021-11-07 12:08:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-25 15:28:17 +00:00
|
|
|
// WithFastestPeerSelection is an option used to select a peer from the peer store
|
2022-11-24 21:50:43 +00:00
|
|
|
// 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
|
2023-07-19 16:25:35 +00:00
|
|
|
func WithFastestPeerSelection(ctx context.Context, fromThesePeers ...peer.ID) Option {
|
|
|
|
return func(params *lightPushParameters) {
|
2023-02-16 16:17:52 +00:00
|
|
|
p, err := utils.SelectPeerWithLowestRTT(ctx, params.host, LightPushID_v20beta1, fromThesePeers, params.log)
|
2021-11-09 23:34:04 +00:00
|
|
|
if err == nil {
|
2023-01-08 18:33:30 +00:00
|
|
|
params.selectedPeer = p
|
2021-11-09 23:34:04 +00:00
|
|
|
} else {
|
2022-05-30 15:55:30 +00:00
|
|
|
params.log.Info("selecting peer", zap.Error(err))
|
2021-11-09 23:34:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-19 16:25:35 +00:00
|
|
|
// WithRequestID is an option to set a specific request ID to be used when
|
2022-07-25 15:28:17 +00:00
|
|
|
// publishing a message
|
2023-07-19 16:25:35 +00:00
|
|
|
func WithRequestID(requestID []byte) Option {
|
|
|
|
return func(params *lightPushParameters) {
|
|
|
|
params.requestID = requestID
|
2021-11-07 12:08:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-19 16:25:35 +00:00
|
|
|
// WithAutomaticRequestID is an option to automatically generate a request ID
|
2022-07-25 15:28:17 +00:00
|
|
|
// when publishing a message
|
2023-07-19 16:25:35 +00:00
|
|
|
func WithAutomaticRequestID() Option {
|
|
|
|
return func(params *lightPushParameters) {
|
|
|
|
params.requestID = protocol.GenerateRequestId()
|
2021-11-07 12:08:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-25 15:28:17 +00:00
|
|
|
// DefaultOptions are the default options to be used when using the lightpush protocol
|
2023-07-19 16:25:35 +00:00
|
|
|
func DefaultOptions(host host.Host) []Option {
|
|
|
|
return []Option{
|
|
|
|
WithAutomaticRequestID(),
|
2022-08-15 17:13:45 +00:00
|
|
|
WithAutomaticPeerSelection(),
|
2021-11-07 12:08:29 +00:00
|
|
|
}
|
|
|
|
}
|