2021-11-07 12:08:29 +00:00
|
|
|
package lightpush
|
|
|
|
|
|
|
|
import (
|
2021-11-09 23:34:04 +00:00
|
|
|
"context"
|
|
|
|
|
2021-11-07 12:08:29 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/host"
|
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol"
|
|
|
|
"github.com/status-im/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
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
requestId []byte
|
2022-05-30 15:55:30 +00:00
|
|
|
log *zap.Logger
|
2021-11-07 12:08:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type LightPushOption func(*LightPushParameters)
|
|
|
|
|
2022-07-25 15:28:17 +00:00
|
|
|
// WithPeer is an option used to specify the peerID to push a waku message to
|
2021-11-07 12:08:29 +00:00
|
|
|
func WithPeer(p peer.ID) LightPushOption {
|
|
|
|
return func(params *LightPushParameters) {
|
|
|
|
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
|
|
|
|
// to push a waku message to
|
2022-08-15 17:13:45 +00:00
|
|
|
func WithAutomaticPeerSelection() LightPushOption {
|
2021-11-07 12:08:29 +00:00
|
|
|
return func(params *LightPushParameters) {
|
2022-08-15 17:13:45 +00:00
|
|
|
p, err := utils.SelectPeer(params.host, string(LightPushID_v20beta1), params.log)
|
2021-11-07 12:08:29 +00:00
|
|
|
if err == nil {
|
|
|
|
params.selectedPeer = *p
|
|
|
|
} 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
|
|
|
|
// with the lowest ping
|
2021-11-09 23:34:04 +00:00
|
|
|
func WithFastestPeerSelection(ctx context.Context) LightPushOption {
|
|
|
|
return func(params *LightPushParameters) {
|
2022-05-30 15:55:30 +00:00
|
|
|
p, err := utils.SelectPeerWithLowestRTT(ctx, params.host, string(LightPushID_v20beta1), params.log)
|
2021-11-09 23:34:04 +00:00
|
|
|
if err == nil {
|
|
|
|
params.selectedPeer = *p
|
|
|
|
} 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-25 15:28:17 +00:00
|
|
|
// WithRequestId is an option to set a specific request ID to be used when
|
|
|
|
// publishing a message
|
2021-11-07 12:08:29 +00:00
|
|
|
func WithRequestId(requestId []byte) LightPushOption {
|
|
|
|
return func(params *LightPushParameters) {
|
|
|
|
params.requestId = requestId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-25 15:28:17 +00:00
|
|
|
// WithAutomaticRequestId is an option to automatically generate a request ID
|
|
|
|
// when publishing a message
|
2021-11-07 12:08:29 +00:00
|
|
|
func WithAutomaticRequestId() LightPushOption {
|
|
|
|
return func(params *LightPushParameters) {
|
|
|
|
params.requestId = protocol.GenerateRequestId()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-25 15:28:17 +00:00
|
|
|
// DefaultOptions are the default options to be used when using the lightpush protocol
|
2021-11-07 12:08:29 +00:00
|
|
|
func DefaultOptions(host host.Host) []LightPushOption {
|
|
|
|
return []LightPushOption{
|
|
|
|
WithAutomaticRequestId(),
|
2022-08-15 17:13:45 +00:00
|
|
|
WithAutomaticPeerSelection(),
|
2021-11-07 12:08:29 +00:00
|
|
|
}
|
|
|
|
}
|