Skip to content

Commit

Permalink
Enables injectable GossipSub router (#503)
Browse files Browse the repository at this point in the history
* adds with gossipsub tracker

* renames and add godoc
  • Loading branch information
thep2p authored Nov 2, 2022
1 parent 01ab84a commit 1e16100
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions gossipsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,22 @@ type GossipSubParams struct {
IWantFollowupTime time.Duration
}

// NewGossipSub returns a new PubSub object using GossipSubRouter as the router.
// NewGossipSub returns a new PubSub object using the default GossipSubRouter as the router.
func NewGossipSub(ctx context.Context, h host.Host, opts ...Option) (*PubSub, error) {
rt := DefaultGossipSubRouter(h)
opts = append(opts, WithRawTracer(rt.tagTracer))
return NewGossipSubWithRouter(ctx, h, rt, opts...)
}

// NewGossipSubWithRouter returns a new PubSub object using the given router.
func NewGossipSubWithRouter(ctx context.Context, h host.Host, rt PubSubRouter, opts ...Option) (*PubSub, error) {
return NewPubSub(ctx, h, rt, opts...)
}

// DefaultGossipSubRouter returns a new GossipSubRouter with default parameters.
func DefaultGossipSubRouter(h host.Host) *GossipSubRouter {
params := DefaultGossipSubParams()
rt := &GossipSubRouter{
return &GossipSubRouter{
peers: make(map[peer.ID]protocol.ID),
mesh: make(map[string]map[peer.ID]struct{}),
fanout: make(map[string]map[peer.ID]struct{}),
Expand All @@ -225,10 +237,6 @@ func NewGossipSub(ctx context.Context, h host.Host, opts ...Option) (*PubSub, er
tagTracer: newTagTracer(h.ConnManager()),
params: params,
}

// hook the tag tracer
opts = append(opts, WithRawTracer(rt.tagTracer))
return NewPubSub(ctx, h, rt, opts...)
}

// DefaultGossipSubParams returns the default gossip sub parameters
Expand Down Expand Up @@ -1909,6 +1917,14 @@ func (gs *GossipSubRouter) getPeers(topic string, count int, filter func(peer.ID
return peers
}

// WithDefaultTagTracer returns the tag tracer of the GossipSubRouter as a PubSub option.
// This is useful for cases where the GossipSubRouter is instantiated externally, and is
// injected into the GossipSub constructor as a dependency. This allows the tag tracer to be
// also injected into the GossipSub constructor as a PubSub option dependency.
func (gs *GossipSubRouter) WithDefaultTagTracer() Option {
return WithRawTracer(gs.tagTracer)
}

func peerListToMap(peers []peer.ID) map[peer.ID]struct{} {
pmap := make(map[peer.ID]struct{})
for _, p := range peers {
Expand Down

0 comments on commit 1e16100

Please sign in to comment.