/
routing.go
61 lines (52 loc) · 1.23 KB
/
routing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com
import (
"time"
"github.com/ipfs/kubo/core/node/helpers"
libp2p "github.com/ipfs/kubo/core/node/libp2p"
pubsub "github.com/libp2p/go-libp2p-pubsub"
namesys "github.com/libp2p/go-libp2p-pubsub-router"
record "github.com/libp2p/go-libp2p-record"
routinghelpers "github.com/libp2p/go-libp2p-routing-helpers"
"github.com/libp2p/go-libp2p/core/host"
"go.uber.org/fx"
)
var (
neverDuration = 100 * 365 * 24 * time.Hour
)
type p2pPSRoutingIn struct {
fx.In
Validator record.Validator
Host host.Host
PubSub *pubsub.PubSub
}
type p2pRouterOut struct {
fx.Out
Router libp2p.Router `group:"routers"`
}
func PubsubRouter(
mctx helpers.MetricsCtx,
lc fx.Lifecycle,
in p2pPSRoutingIn,
) (p2pRouterOut, *namesys.PubsubValueStore, error) {
psRouter, err := namesys.NewPubsubValueStore(
helpers.LifecycleCtx(mctx, lc),
in.Host,
in.PubSub,
in.Validator,
namesys.WithRebroadcastInitialDelay(neverDuration),
)
if err != nil {
return p2pRouterOut{}, nil, err
}
return p2pRouterOut{
Router: libp2p.Router{
Routing: &routinghelpers.Compose{
ValueStore: &routinghelpers.LimitedValueStore{
ValueStore: psRouter,
Namespaces: []string{"ipns"},
},
},
Priority: 100,
},
}, psRouter, nil
}