提交 c5b40b3b 作者: Jeromy

prevent wantmanager from leaking goroutines (and memory)

License: MIT
Signed-off-by: 's avatarJeromy <jeromyj@gmail.com>
上级 feba3e1d
...@@ -137,34 +137,47 @@ func (mq *msgQueue) runQueue(ctx context.Context) { ...@@ -137,34 +137,47 @@ func (mq *msgQueue) runQueue(ctx context.Context) {
for { for {
select { select {
case <-mq.work: // there is work to be done case <-mq.work: // there is work to be done
mq.doWork(ctx)
case <-mq.done:
return
}
}
}
err := mq.network.ConnectTo(ctx, mq.p) func (mq *msgQueue) doWork(ctx context.Context) {
// allow a minute for connections
// this includes looking them up in the dht
// dialing them, and handshaking
conctx, cancel := context.WithTimeout(ctx, time.Minute)
defer cancel()
err := mq.network.ConnectTo(conctx, mq.p)
if err != nil { if err != nil {
log.Noticef("cant connect to peer %s: %s", mq.p, err) log.Noticef("cant connect to peer %s: %s", mq.p, err)
// TODO: cant connect, what now? // TODO: cant connect, what now?
continue return
} }
// grab outgoing message // grab outgoing message
mq.outlk.Lock() mq.outlk.Lock()
wlm := mq.out wlm := mq.out
if wlm == nil || wlm.Empty() {
mq.outlk.Unlock()
continue
}
mq.out = nil mq.out = nil
mq.outlk.Unlock() mq.outlk.Unlock()
if wlm == nil || wlm.Empty() {
return
}
sendctx, cancel := context.WithTimeout(ctx, time.Second*30)
defer cancel()
// send wantlist updates // send wantlist updates
err = mq.network.SendMessage(ctx, mq.p, wlm) err = mq.network.SendMessage(sendctx, mq.p, wlm)
if err != nil { if err != nil {
log.Noticef("bitswap send error: %s", err) log.Noticef("bitswap send error: %s", err)
// TODO: what do we do if this fails? // TODO: what do we do if this fails?
}
case <-mq.done:
return return
} }
}
} }
func (pm *WantManager) Connected(p peer.ID) { func (pm *WantManager) Connected(p peer.ID) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论