From 63256dbe54baeb42749ad191a431dfac4156a36c Mon Sep 17 00:00:00 2001 From: vyzo Date: Thu, 23 Apr 2020 19:37:05 +0300 Subject: [PATCH] add sqrt behaviour to randomsub --- randomsub.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/randomsub.go b/randomsub.go index 9273b1a..497f5fe 100644 --- a/randomsub.go +++ b/randomsub.go @@ -2,6 +2,7 @@ package pubsub import ( "context" + "math" "github.com/libp2p/go-libp2p-core/host" "github.com/libp2p/go-libp2p-core/peer" @@ -25,7 +26,8 @@ func NewRandomSub(ctx context.Context, h host.Host, opts ...Option) (*PubSub, er } // RandomSubRouter is a router that implements a random propagation strategy. -// For each message, it selects RandomSubD peers and forwards the message to them. +// For each message, it selects the square root of peers, with a min of RandomSubD, +// and forwards the message to them. type RandomSubRouter struct { p *PubSub peers map[peer.ID]protocol.ID @@ -119,6 +121,11 @@ func (rs *RandomSubRouter) Publish(msg *Message) { } if len(rspeers) > RandomSubD { + target := RandomSubD + sqrt := int(math.Ceil(math.Sqrt(float64(len(rspeers))))) + if sqrt > target { + target = sqrt + } xpeers := peerMapToList(rspeers) shufflePeers(xpeers) xpeers = xpeers[:RandomSubD]