Skip to content

Commit 1e8b1e8

Browse files
authored
Merge pull request #7 from ipfs/fix/no-extra-freeze
fix(peertaskqueue): don't freeze for node that does not exist
2 parents 98cd87e + 013dc0b commit 1e8b1e8

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

peertaskqueue.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -169,19 +169,20 @@ func (ptq *PeerTaskQueue) Remove(identifier peertask.Identifier, p peer.ID) {
169169
ptq.lock.Lock()
170170
peerTracker, ok := ptq.peerTrackers[p]
171171
if ok {
172-
peerTracker.Remove(identifier)
173-
// we now also 'freeze' that partner. If they sent us a cancel for a
174-
// block we were about to send them, we should wait a short period of time
175-
// to make sure we receive any other in-flight cancels before sending
176-
// them a block they already potentially have
177-
if !ptq.ignoreFreezing {
178-
if !peerTracker.IsFrozen() {
179-
ptq.frozenPeers[p] = struct{}{}
172+
if peerTracker.Remove(identifier) {
173+
// we now also 'freeze' that partner. If they sent us a cancel for a
174+
// block we were about to send them, we should wait a short period of time
175+
// to make sure we receive any other in-flight cancels before sending
176+
// them a block they already potentially have
177+
if !ptq.ignoreFreezing {
178+
if !peerTracker.IsFrozen() {
179+
ptq.frozenPeers[p] = struct{}{}
180+
}
181+
182+
peerTracker.Freeze()
180183
}
181-
182-
peerTracker.Freeze()
184+
ptq.pQueue.Update(peerTracker.Index())
183185
}
184-
ptq.pQueue.Update(peerTracker.Index())
185186
}
186187
ptq.lock.Unlock()
187188
}

peertaskqueue_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func TestFreezeUnfreeze(t *testing.T) {
9292
// now, pop off four tasks, there should be one from each
9393
matchNTasks(t, ptq, 4, a.Pretty(), b.Pretty(), c.Pretty(), d.Pretty())
9494

95-
ptq.Remove(peertask.Task{Identifier: "1"}, b)
95+
ptq.Remove("1", b)
9696

9797
// b should be frozen, causing it to get skipped in the rotation
9898
matchNTasks(t, ptq, 3, a.Pretty(), c.Pretty(), d.Pretty())
@@ -101,6 +101,12 @@ func TestFreezeUnfreeze(t *testing.T) {
101101

102102
matchNTasks(t, ptq, 1, b.Pretty())
103103

104+
// remove none existent task
105+
ptq.Remove("-1", b)
106+
107+
// b should not be frozen
108+
matchNTasks(t, ptq, 4, a.Pretty(), b.Pretty(), c.Pretty(), d.Pretty())
109+
104110
}
105111

106112
func TestFreezeUnfreezeNoFreezingOption(t *testing.T) {
@@ -124,7 +130,7 @@ func TestFreezeUnfreezeNoFreezingOption(t *testing.T) {
124130
// now, pop off four tasks, there should be one from each
125131
matchNTasks(t, ptq, 4, a.Pretty(), b.Pretty(), c.Pretty(), d.Pretty())
126132

127-
ptq.Remove(peertask.Task{Identifier: "1"}, b)
133+
ptq.Remove("1", b)
128134

129135
// b should be frozen, causing it to get skipped in the rotation
130136
matchNTasks(t, ptq, 4, a.Pretty(), b.Pretty(), c.Pretty(), d.Pretty())

peertracker/peertracker.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,13 @@ func (p *PeerTracker) PopBlock() *peertask.TaskBlock {
179179
}
180180

181181
// Remove removes the task with the given identifier from this peers queue
182-
func (p *PeerTracker) Remove(identifier peertask.Identifier) {
182+
func (p *PeerTracker) Remove(identifier peertask.Identifier) bool {
183183
taskBlock, ok := p.taskMap[identifier]
184184
if ok {
185185
taskBlock.MarkPrunable(identifier)
186186
p.numTasks--
187187
}
188+
return ok
188189
}
189190

190191
// Freeze increments the freeze value for this peer. While a peer is frozen

0 commit comments

Comments
 (0)