refactor: taskKey := p.Key() + block.Key()
for clarity and to avoid errors, define a function License: MIT Signed-off-by: Brian Tiger Chow <brian@perfmode.com>
Brian Tiger Chow committed
Dec 16, 2014 at 21:01 UTC
93fde86d18071549d3b5251eb16991fe0a2b66d7
2 files changed
+11
-7
exchange/bitswap/strategy/ledgermanager.go
-1
@@ -17,7 +17,6 @@ var log = u.Logger("strategy")
17
// LedgerMap lists Ledgers by their Partner key.
18
type ledgerMap map[peerKey]*ledger
19
20
-// FIXME share this externally
20
type peerKey u.Key
21
22
type Envelope struct {
exchange/bitswap/strategy/tasklist.go
+11
-6
@@ -10,12 +10,12 @@ import (
10
// tasks (on getnext). For now, we are assuming a dumb/nice strategy.
11
type TaskList struct {
12
tasks []*Task
13
- taskmap map[u.Key]*Task
13
+ taskmap map[string]*Task
14
}
15
16
func NewTaskList() *TaskList {
17
return &TaskList{
18
- taskmap: make(map[u.Key]*Task),
18
+ taskmap: make(map[string]*Task),
19
}
20
}
21
@@ -28,7 +28,7 @@ type Task struct {
28
// Push currently adds a new task to the end of the list
29
// TODO: make this into a priority queue
30
func (tl *TaskList) Push(block u.Key, priority int, to peer.Peer) {
31
- if task, ok := tl.taskmap[to.Key()+block]; ok {
31
+ if task, ok := tl.taskmap[taskKey(to, block)]; ok {
32
// TODO: when priority queue is implemented,
33
// rearrange this Task
34
task.theirPriority = priority
@@ -40,7 +40,7 @@ func (tl *TaskList) Push(block u.Key, priority int, to peer.Peer) {
40
theirPriority: priority,
41
}
42
tl.tasks = append(tl.tasks, task)
43
- tl.taskmap[to.Key()+block] = task
43
+ tl.taskmap[taskKey(to, block)] = task
44
}
45
46
// Pop 'pops' the next task to be performed. Returns nil no task exists.
@@ -52,7 +52,7 @@ func (tl *TaskList) Pop() *Task {
52
// the same block from multiple peers
53
out = tl.tasks[0]
54
tl.tasks = tl.tasks[1:]
55
- delete(tl.taskmap, out.Target.Key()+out.Key)
55
+ delete(tl.taskmap, taskKey(out.Target, out.Key))
56
// Filter out blocks that have been cancelled
57
if out.theirPriority >= 0 {
58
break
@@ -64,8 +64,13 @@ func (tl *TaskList) Pop() *Task {
64
65
// Cancel lazily cancels the sending of a block to a given peer
66
func (tl *TaskList) Cancel(k u.Key, p peer.Peer) {
67
- t, ok := tl.taskmap[p.Key()+k]
67
+ t, ok := tl.taskmap[taskKey(p, k)]
68
if ok {
69
t.theirPriority = -1
70
}
71
}
72
+
73
+// taskKey returns a key that uniquely identifies a task.
74
+func taskKey(p peer.Peer, k u.Key) string {
75
+ return string(p.Key() + k)
76
+}