2021-11-07 13:08:29 +01:00
|
|
|
package lightpush
|
|
|
|
|
|
|
|
import (
|
2023-11-15 19:56:55 +05:30
|
|
|
"errors"
|
|
|
|
|
2022-10-19 15:39:32 -04:00
|
|
|
"github.com/libp2p/go-libp2p/core/host"
|
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2023-11-15 19:56:55 +05:30
|
|
|
"github.com/multiformats/go-multiaddr"
|
2023-08-10 18:28:22 +05:30
|
|
|
"github.com/waku-org/go-waku/waku/v2/peermanager"
|
2022-11-09 15:53:01 -04:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol"
|
2023-10-30 12:30:25 -04:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/relay"
|
2022-01-18 14:17:06 -04:00
|
|
|
"go.uber.org/zap"
|
2024-02-05 08:53:15 -04:00
|
|
|
"golang.org/x/time/rate"
|
2021-11-07 13:08:29 +01:00
|
|
|
)
|
|
|
|
|
2024-02-05 08:53:15 -04:00
|
|
|
type LightpushParameters struct {
|
|
|
|
limiter *rate.Limiter
|
|
|
|
}
|
|
|
|
|
|
|
|
type Option func(*LightpushParameters)
|
|
|
|
|
|
|
|
// WithRateLimiter is an option used to specify a rate limiter for requests received in lightpush protocol
|
|
|
|
func WithRateLimiter(r rate.Limit, b int) Option {
|
|
|
|
return func(params *LightpushParameters) {
|
|
|
|
params.limiter = rate.NewLimiter(r, b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type lightPushRequestParameters struct {
|
2023-10-16 22:12:01 +05:30
|
|
|
host host.Host
|
2023-11-15 19:56:55 +05:30
|
|
|
peerAddr multiaddr.Multiaddr
|
2023-10-16 22:12:01 +05:30
|
|
|
selectedPeer peer.ID
|
|
|
|
peerSelectionType peermanager.PeerSelection
|
|
|
|
preferredPeers peer.IDSlice
|
|
|
|
requestID []byte
|
|
|
|
pm *peermanager.PeerManager
|
|
|
|
log *zap.Logger
|
|
|
|
pubsubTopic string
|
2021-11-07 13:08:29 +01:00
|
|
|
}
|
|
|
|
|
2024-02-05 08:53:15 -04:00
|
|
|
// RequestOption is the type of options accepted when performing LightPush protocol requests
|
|
|
|
type RequestOption func(*lightPushRequestParameters) error
|
2021-11-07 13:08:29 +01:00
|
|
|
|
2022-07-25 11:28:17 -04:00
|
|
|
// WithPeer is an option used to specify the peerID to push a waku message to
|
2024-02-05 08:53:15 -04:00
|
|
|
func WithPeer(p peer.ID) RequestOption {
|
|
|
|
return func(params *lightPushRequestParameters) error {
|
2021-11-07 13:08:29 +01:00
|
|
|
params.selectedPeer = p
|
2023-11-15 19:56:55 +05:30
|
|
|
if params.peerAddr != nil {
|
|
|
|
return errors.New("peerAddr and peerId options are mutually exclusive")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithPeerAddr is an option used to specify a peerAddress
|
|
|
|
// This new peer will be added to peerStore.
|
|
|
|
// Note that this option is mutually exclusive to WithPeerAddr, only one of them can be used.
|
2024-02-05 08:53:15 -04:00
|
|
|
func WithPeerAddr(pAddr multiaddr.Multiaddr) RequestOption {
|
|
|
|
return func(params *lightPushRequestParameters) error {
|
2023-11-15 19:56:55 +05:30
|
|
|
params.peerAddr = pAddr
|
|
|
|
if params.selectedPeer != "" {
|
|
|
|
return errors.New("peerAddr and peerId options are mutually exclusive")
|
|
|
|
}
|
|
|
|
return nil
|
2021-11-07 13:08:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-25 11:28:17 -04:00
|
|
|
// WithAutomaticPeerSelection is an option used to randomly select a peer from the peer store
|
2022-11-24 17:50:43 -04: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
|
2024-02-05 08:53:15 -04:00
|
|
|
func WithAutomaticPeerSelection(fromThesePeers ...peer.ID) RequestOption {
|
|
|
|
return func(params *lightPushRequestParameters) error {
|
2023-10-16 22:12:01 +05:30
|
|
|
params.peerSelectionType = peermanager.Automatic
|
|
|
|
params.preferredPeers = fromThesePeers
|
2023-11-15 19:56:55 +05:30
|
|
|
return nil
|
2021-11-07 13:08:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-25 11:28:17 -04:00
|
|
|
// WithFastestPeerSelection is an option used to select a peer from the peer store
|
2022-11-24 17:50:43 -04: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
|
2024-02-05 08:53:15 -04:00
|
|
|
func WithFastestPeerSelection(fromThesePeers ...peer.ID) RequestOption {
|
|
|
|
return func(params *lightPushRequestParameters) error {
|
2023-10-16 22:12:01 +05:30
|
|
|
params.peerSelectionType = peermanager.LowestRTT
|
2023-11-15 19:56:55 +05:30
|
|
|
return nil
|
2021-11-09 19:34:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-30 12:30:25 -04:00
|
|
|
// WithPubSubTopic is used to specify the pubsub topic on which a WakuMessage will be broadcasted
|
2024-02-05 08:53:15 -04:00
|
|
|
func WithPubSubTopic(pubsubTopic string) RequestOption {
|
|
|
|
return func(params *lightPushRequestParameters) error {
|
2023-10-30 12:30:25 -04:00
|
|
|
params.pubsubTopic = pubsubTopic
|
2023-11-15 19:56:55 +05:30
|
|
|
return nil
|
2023-10-30 12:30:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithDefaultPubsubTopic is used to indicate that the message should be broadcasted in the default pubsub topic
|
2024-02-05 08:53:15 -04:00
|
|
|
func WithDefaultPubsubTopic() RequestOption {
|
|
|
|
return func(params *lightPushRequestParameters) error {
|
2023-10-30 12:30:25 -04:00
|
|
|
params.pubsubTopic = relay.DefaultWakuTopic
|
2023-11-15 19:56:55 +05:30
|
|
|
return nil
|
2023-10-30 12:30:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-19 12:25:35 -04:00
|
|
|
// WithRequestID is an option to set a specific request ID to be used when
|
2022-07-25 11:28:17 -04:00
|
|
|
// publishing a message
|
2024-02-05 08:53:15 -04:00
|
|
|
func WithRequestID(requestID []byte) RequestOption {
|
|
|
|
return func(params *lightPushRequestParameters) error {
|
2023-07-19 12:25:35 -04:00
|
|
|
params.requestID = requestID
|
2023-11-15 19:56:55 +05:30
|
|
|
return nil
|
2021-11-07 13:08:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-19 12:25:35 -04:00
|
|
|
// WithAutomaticRequestID is an option to automatically generate a request ID
|
2022-07-25 11:28:17 -04:00
|
|
|
// when publishing a message
|
2024-02-05 08:53:15 -04:00
|
|
|
func WithAutomaticRequestID() RequestOption {
|
|
|
|
return func(params *lightPushRequestParameters) error {
|
2023-09-11 10:24:05 -04:00
|
|
|
params.requestID = protocol.GenerateRequestID()
|
2023-11-15 19:56:55 +05:30
|
|
|
return nil
|
2021-11-07 13:08:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-25 11:28:17 -04:00
|
|
|
// DefaultOptions are the default options to be used when using the lightpush protocol
|
2024-02-05 08:53:15 -04:00
|
|
|
func DefaultOptions(host host.Host) []RequestOption {
|
|
|
|
return []RequestOption{
|
2023-07-19 12:25:35 -04:00
|
|
|
WithAutomaticRequestID(),
|
2022-08-15 13:13:45 -04:00
|
|
|
WithAutomaticPeerSelection(),
|
2021-11-07 13:08:29 +01:00
|
|
|
}
|
|
|
|
}
|