PEX: integrate with send throttling

This commit is contained in:
Yaroslav Kolomiiets 2020-04-15 13:02:34 +01:00 committed by Matt Joiner
parent 3d87c20766
commit 26071eaad0
2 changed files with 8 additions and 4 deletions

View File

@ -564,7 +564,9 @@ func (cn *PeerConn) fillWriteBuffer(msg func(pp.Message) bool) {
cn.requestsLowWater = len(cn.requests) / 2 cn.requestsLowWater = len(cn.requests) / 2
} }
if cn.pex.IsEnabled() { if cn.pex.IsEnabled() {
cn.pex.Share(msg) // gated internally if flow := cn.pex.Share(msg); !flow {
return
}
} }
cn.upload(msg) cn.upload(msg)
} }

View File

@ -68,20 +68,22 @@ func (s *pexConnState) genmsg() *pp.PexMsg {
} }
// Share is called from the writer goroutine if when it is woken up with the write buffers empty // Share is called from the writer goroutine if when it is woken up with the write buffers empty
func (s *pexConnState) Share(postfn messageWriter) { // Returns whether there's more room on the send buffer to write to.
func (s *pexConnState) Share(postfn messageWriter) bool {
select { select {
case <-s.gate: case <-s.gate:
if tx := s.genmsg(); tx != nil { if tx := s.genmsg(); tx != nil {
s.dbg.Print("sending PEX message: ", tx) s.dbg.Print("sending PEX message: ", tx)
postfn(tx.Message(s.xid)) flow := postfn(tx.Message(s.xid))
s.sched(pexInterval) s.sched(pexInterval)
return flow
} else { } else {
// no PEX to send this time - try again shortly // no PEX to send this time - try again shortly
s.sched(pexRetryDelay) s.sched(pexRetryDelay)
} }
default: default:
return
} }
return true
} }
// Recv is called from the reader goroutine // Recv is called from the reader goroutine