Skip to content

Commit ab6da24

Browse files
committed
Request more p2p addrs after peer checks
If the height or required services is deemed insufficient after the connection handshake, do not request more addresses right away, waiting for its response before killing the TCP connection. This stops ConnectOutbound from returning early with error while the TCP connection is still active, and causing the SPV syncer to begin connecting to more remote peers. While the SPV syncer remains limited to 8 total outbound managed peers, the total count of TCP connections can easily exceed this, and has been observed to max out the circuit limit on Tor proxies. Although this appears to move the address requesting to the foreground of ConnectOutbound, it only writes the getaddr message. addr message replies are handled internally by the RemotePeer.
1 parent 3f77f1b commit ab6da24

File tree

1 file changed

+13
-24
lines changed

1 file changed

+13
-24
lines changed

p2p/peering.go

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -256,29 +256,15 @@ func (lp *LocalPeer) ConnectOutbound(ctx context.Context, addr string, reqSvcs w
256256

257257
go lp.serveUntilError(ctx, rp)
258258

259-
var waitForAddrs <-chan time.Time
260-
if lp.amgr.NeedMoreAddresses() {
261-
waitForAddrs = time.After(stallTimeout)
262-
err = rp.Addrs(ctx)
263-
if err != nil {
264-
op := errors.Opf(opf, rp.raddr)
265-
return nil, errors.E(op, err)
266-
}
267-
}
268-
269259
// Disconnect from the peer if it does not specify all required services.
270260
if rp.services&reqSvcs != reqSvcs {
271261
op := errors.Opf(opf, rp.raddr)
272262
reason := errors.Errorf("missing required service flags %v", reqSvcs&^rp.services)
263+
reject := wire.NewMsgReject(wire.CmdVersion, wire.RejectNonstandard, reason.Error())
264+
rp.sendMessageAck(ctx, reject)
265+
273266
err := errors.E(op, reason)
274-
go func() {
275-
if waitForAddrs != nil {
276-
<-waitForAddrs
277-
}
278-
reject := wire.NewMsgReject(wire.CmdVersion, wire.RejectNonstandard, reason.Error())
279-
rp.sendMessageAck(ctx, reject)
280-
rp.Disconnect(err)
281-
}()
267+
rp.Disconnect(err)
282268
return nil, err
283269
}
284270

@@ -288,18 +274,21 @@ func (lp *LocalPeer) ConnectOutbound(ctx context.Context, addr string, reqSvcs w
288274
if rp.initHeight < reqHeight {
289275
op := errors.Opf(opf, rp.raddr)
290276
err := errors.E(op, "peer is not synced")
291-
go func() {
292-
if waitForAddrs != nil {
293-
<-waitForAddrs
294-
}
295-
rp.Disconnect(err)
296-
}()
277+
rp.Disconnect(err)
297278
return nil, err
298279
}
299280

300281
// Mark this as a good address.
301282
lp.amgr.Good(na)
302283

284+
if lp.amgr.NeedMoreAddresses() {
285+
err = rp.Addrs(ctx)
286+
if err != nil {
287+
rp.Disconnect(err)
288+
return nil, err
289+
}
290+
}
291+
303292
return rp, nil
304293
}
305294

0 commit comments

Comments
 (0)