feat: add direct connect ticks option

In [drand](https://github.com/drand/drand) we have a gossipsub relay to allow users to subscribe to getting random values over pubsub. We want to support pure gossip relays who relay from a relay. For this we need direct peering agreements and want to mitigate the possibility of "missing" randomness messages by ensuring the direct connect ticks period is less than the period between updates.

This PR simply adds a new functional option allowing us to set the direct connect ticks value without modifying the global variable.
This commit is contained in:
Alan Shaw 2020-05-27 09:51:48 +01:00 committed by vyzo
parent 9a1171a0ef
commit c0712c6e92
2 changed files with 18 additions and 9 deletions

View File

@ -305,6 +305,21 @@ func WithDirectPeers(pis []peer.AddrInfo) Option {
} }
} }
// WithDirectConnectTicks is a gossipsub router option that sets the number of
// heartbeat ticks between attempting to reconnect direct peers that are not
// currently connected. A "tick" is based on the heartbeat interval, which is
// 1s by default. The default value for direct connect ticks is 300.
func WithDirectConnectTicks(t uint64) Option {
return func(ps *PubSub) error {
gs, ok := ps.rt.(*GossipSubRouter)
if !ok {
return fmt.Errorf("pubsub router is not gossipsub")
}
gs.directConnectTicks = t
return nil
}
}
// GossipSubRouter is a router that implements the gossipsub protocol. // GossipSubRouter is a router that implements the gossipsub protocol.
// For each topic we have joined, we maintain an overlay through which // For each topic we have joined, we maintain an overlay through which
// messages flow; this is the mesh map. // messages flow; this is the mesh map.

View File

@ -1120,20 +1120,14 @@ func TestGossipsubStarTopologyWithSignedPeerRecords(t *testing.T) {
} }
func TestGossipsubDirectPeers(t *testing.T) { func TestGossipsubDirectPeers(t *testing.T) {
originalGossipSubDirectConnectTicks := GossipSubDirectConnectTicks
GossipSubDirectConnectTicks = 2
defer func() {
GossipSubDirectConnectTicks = originalGossipSubDirectConnectTicks
}()
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
h := getNetHosts(t, ctx, 3) h := getNetHosts(t, ctx, 3)
psubs := []*PubSub{ psubs := []*PubSub{
getGossipsub(ctx, h[0]), getGossipsub(ctx, h[0], WithDirectConnectTicks(2)),
getGossipsub(ctx, h[1], WithDirectPeers([]peer.AddrInfo{peer.AddrInfo{h[2].ID(), h[2].Addrs()}})), getGossipsub(ctx, h[1], WithDirectPeers([]peer.AddrInfo{peer.AddrInfo{h[2].ID(), h[2].Addrs()}}), WithDirectConnectTicks(2)),
getGossipsub(ctx, h[2], WithDirectPeers([]peer.AddrInfo{peer.AddrInfo{h[1].ID(), h[1].Addrs()}})), getGossipsub(ctx, h[2], WithDirectPeers([]peer.AddrInfo{peer.AddrInfo{h[1].ID(), h[1].Addrs()}}), WithDirectConnectTicks(2)),
} }
connect(t, h[0], h[1]) connect(t, h[0], h[1])