mirror of
https://github.com/status-im/go-waku.git
synced 2025-01-27 22:15:38 +00:00
11d1f8fb0d
* feat: result aggregation in resume and enforce max page size * feat: add WithLogger option to wakunode (#184) * fix: rebase issues
68 lines
1.6 KiB
Go
68 lines
1.6 KiB
Go
package lightpush
|
|
|
|
import (
|
|
"context"
|
|
|
|
"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"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
type LightPushParameters struct {
|
|
host host.Host
|
|
selectedPeer peer.ID
|
|
requestId []byte
|
|
log *zap.SugaredLogger
|
|
}
|
|
|
|
type LightPushOption func(*LightPushParameters)
|
|
|
|
func WithPeer(p peer.ID) LightPushOption {
|
|
return func(params *LightPushParameters) {
|
|
params.selectedPeer = p
|
|
}
|
|
}
|
|
|
|
func WithAutomaticPeerSelection(host host.Host) LightPushOption {
|
|
return func(params *LightPushParameters) {
|
|
p, err := utils.SelectPeer(host, string(LightPushID_v20beta1), params.log)
|
|
if err == nil {
|
|
params.selectedPeer = *p
|
|
} else {
|
|
params.log.Info("Error selecting peer: ", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func WithFastestPeerSelection(ctx context.Context) LightPushOption {
|
|
return func(params *LightPushParameters) {
|
|
p, err := utils.SelectPeerWithLowestRTT(ctx, params.host, string(LightPushID_v20beta1), params.log)
|
|
if err == nil {
|
|
params.selectedPeer = *p
|
|
} else {
|
|
params.log.Info("Error selecting peer: ", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func WithRequestId(requestId []byte) LightPushOption {
|
|
return func(params *LightPushParameters) {
|
|
params.requestId = requestId
|
|
}
|
|
}
|
|
|
|
func WithAutomaticRequestId() LightPushOption {
|
|
return func(params *LightPushParameters) {
|
|
params.requestId = protocol.GenerateRequestId()
|
|
}
|
|
}
|
|
|
|
func DefaultOptions(host host.Host) []LightPushOption {
|
|
return []LightPushOption{
|
|
WithAutomaticRequestId(),
|
|
WithAutomaticPeerSelection(host),
|
|
}
|
|
}
|