提交 8450a8d4 作者: Jeromy

address comments from CR

上级 76e879c9
...@@ -55,6 +55,9 @@ type Envelope struct { ...@@ -55,6 +55,9 @@ type Envelope struct {
Peer peer.ID Peer peer.ID
// Message is the payload // Message is the payload
Message bsmsg.BitSwapMessage Message bsmsg.BitSwapMessage
// A callback to notify the decision queue that the task is complete
Sent func()
} }
type Engine struct { type Engine struct {
...@@ -132,6 +135,9 @@ func (e *Engine) nextEnvelope(ctx context.Context) (*Envelope, error) { ...@@ -132,6 +135,9 @@ func (e *Engine) nextEnvelope(ctx context.Context) (*Envelope, error) {
block, err := e.bs.Get(nextTask.Entry.Key) block, err := e.bs.Get(nextTask.Entry.Key)
if err != nil { if err != nil {
// If we don't have the block, don't hold that against the peer
// make sure to update that the task has been 'completed'
nextTask.Done()
continue continue
} }
...@@ -140,6 +146,7 @@ func (e *Engine) nextEnvelope(ctx context.Context) (*Envelope, error) { ...@@ -140,6 +146,7 @@ func (e *Engine) nextEnvelope(ctx context.Context) (*Envelope, error) {
return &Envelope{ return &Envelope{
Peer: nextTask.Target, Peer: nextTask.Target,
Message: m, Message: m,
Sent: nextTask.Done,
}, nil }, nil
} }
} }
......
...@@ -173,10 +173,10 @@ func wrapCmp(f func(a, b *peerRequestTask) bool) func(a, b pq.Elem) bool { ...@@ -173,10 +173,10 @@ func wrapCmp(f func(a, b *peerRequestTask) bool) func(a, b pq.Elem) bool {
} }
type activePartner struct { type activePartner struct {
lk sync.Mutex
// Active is the number of blocks this peer is currently being sent // Active is the number of blocks this peer is currently being sent
// active must be locked around as it will be updated externally // active must be locked around as it will be updated externally
activelk sync.Mutex
active int active int
// requests is the number of blocks this peer is currently requesting // requests is the number of blocks this peer is currently requesting
...@@ -197,6 +197,7 @@ func partnerCompare(a, b pq.Elem) bool { ...@@ -197,6 +197,7 @@ func partnerCompare(a, b pq.Elem) bool {
pb := b.(*activePartner) pb := b.(*activePartner)
// having no blocks in their wantlist means lowest priority // having no blocks in their wantlist means lowest priority
// having both of these checks ensures stability of the sort
if pa.requests == 0 { if pa.requests == 0 {
return false return false
} }
...@@ -208,19 +209,19 @@ func partnerCompare(a, b pq.Elem) bool { ...@@ -208,19 +209,19 @@ func partnerCompare(a, b pq.Elem) bool {
// StartTask signals that a task was started for this partner // StartTask signals that a task was started for this partner
func (p *activePartner) StartTask() { func (p *activePartner) StartTask() {
p.lk.Lock() p.activelk.Lock()
p.active++ p.active++
p.lk.Unlock() p.activelk.Unlock()
} }
// TaskDone signals that a task was completed for this partner // TaskDone signals that a task was completed for this partner
func (p *activePartner) TaskDone() { func (p *activePartner) TaskDone() {
p.lk.Lock() p.activelk.Lock()
p.active-- p.active--
if p.active < 0 { if p.active < 0 {
panic("more tasks finished than started!") panic("more tasks finished than started!")
} }
p.lk.Unlock() p.activelk.Unlock()
} }
// Index implements pq.Elem // Index implements pq.Elem
......
...@@ -105,10 +105,15 @@ func TestPeerRepeats(t *testing.T) { ...@@ -105,10 +105,15 @@ func TestPeerRepeats(t *testing.T) {
// Now, if one of the tasks gets finished, the next task off the queue should // Now, if one of the tasks gets finished, the next task off the queue should
// be for the same peer // be for the same peer
tasks[0].Done() for blockI := 0; blockI < 4; blockI++ {
for i := 0; i < 4; i++ {
// its okay to mark the same task done multiple times here (JUST FOR TESTING)
tasks[i].Done()
ntask := prq.Pop() ntask := prq.Pop()
if ntask.Target != tasks[0].Target { if ntask.Target != tasks[i].Target {
t.Fatal("Expected task from peer with lowest active count") t.Fatal("Expected task from peer with lowest active count")
} }
}
}
} }
...@@ -51,6 +51,7 @@ func (bs *Bitswap) taskWorker(ctx context.Context) { ...@@ -51,6 +51,7 @@ func (bs *Bitswap) taskWorker(ctx context.Context) {
} }
log.Event(ctx, "deliverBlocks", envelope.Message, envelope.Peer) log.Event(ctx, "deliverBlocks", envelope.Message, envelope.Peer)
bs.send(ctx, envelope.Peer, envelope.Message) bs.send(ctx, envelope.Peer, envelope.Message)
envelope.Sent()
case <-ctx.Done(): case <-ctx.Done():
return return
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论