9
9
"sync"
10
10
"time"
11
11
12
+ "github.com/libp2p/go-libp2p/p2p/host/pstoremanager"
12
13
"github.com/libp2p/go-libp2p/p2p/host/relaysvc"
13
14
relayv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
14
15
"github.com/libp2p/go-libp2p/p2p/protocol/holepunch"
@@ -74,6 +75,7 @@ type BasicHost struct {
74
75
refCount sync.WaitGroup
75
76
76
77
network network.Network
78
+ psManager * pstoremanager.PeerstoreManager
77
79
mux * msmux.MultistreamMuxer
78
80
ids identify.IDService
79
81
hps * holepunch.Service
@@ -156,18 +158,24 @@ type HostOpts struct {
156
158
157
159
// NewHost constructs a new *BasicHost and activates it by attaching its stream and connection handlers to the given inet.Network.
158
160
func NewHost (n network.Network , opts * HostOpts ) (* BasicHost , error ) {
161
+ eventBus := eventbus .NewBus ()
162
+ psManager , err := pstoremanager .NewPeerstoreManager (n .Peerstore (), eventBus )
163
+ if err != nil {
164
+ return nil , err
165
+ }
159
166
hostCtx , cancel := context .WithCancel (context .Background ())
160
167
if opts == nil {
161
168
opts = & HostOpts {}
162
169
}
163
170
164
171
h := & BasicHost {
165
172
network : n ,
173
+ psManager : psManager ,
166
174
mux : msmux .NewMultistreamMuxer (),
167
175
negtimeout : DefaultNegotiationTimeout ,
168
176
AddrsFactory : DefaultAddrsFactory ,
169
177
maResolver : madns .DefaultResolver ,
170
- eventbus : eventbus . NewBus () ,
178
+ eventbus : eventBus ,
171
179
addrChangeChan : make (chan struct {}, 1 ),
172
180
ctx : hostCtx ,
173
181
ctxCancel : cancel ,
@@ -176,7 +184,6 @@ func NewHost(n network.Network, opts *HostOpts) (*BasicHost, error) {
176
184
177
185
h .updateLocalIpAddr ()
178
186
179
- var err error
180
187
if h .emitters .evtLocalProtocolsUpdated , err = h .eventbus .Emitter (& event.EvtLocalProtocolsUpdated {}); err != nil {
181
188
return nil , err
182
189
}
@@ -352,6 +359,7 @@ func (h *BasicHost) updateLocalIpAddr() {
352
359
353
360
// Start starts background tasks in the host
354
361
func (h * BasicHost ) Start () {
362
+ h .psManager .Start ()
355
363
h .refCount .Add (1 )
356
364
go h .background ()
357
365
}
@@ -1036,6 +1044,7 @@ func (h *BasicHost) Close() error {
1036
1044
_ = h .emitters .evtLocalAddrsUpdated .Close ()
1037
1045
h .Network ().Close ()
1038
1046
1047
+ h .psManager .Close ()
1039
1048
if h .Peerstore () != nil {
1040
1049
h .Peerstore ().Close ()
1041
1050
}
0 commit comments