mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-22 19:07:48 +08:00
refactor: re-use wantlist.Entry type wherever it makes sense
it seems to make sense since, in each place, the Key and Priority represent the same information b/c you know the saying... "It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures." License: MIT Signed-off-by: Brian Tiger Chow <brian@perfmode.com>
This commit is contained in:
parent
1d715956f5
commit
fedcebf67a
@ -172,7 +172,7 @@ func (bs *bitswap) sendWantListTo(ctx context.Context, peers <-chan peer.Peer) e
|
||||
}
|
||||
message := bsmsg.New()
|
||||
for _, wanted := range bs.wantlist.Entries() {
|
||||
message.AddEntry(wanted.Value, wanted.Priority)
|
||||
message.AddEntry(wanted.Key, wanted.Priority)
|
||||
}
|
||||
wg := sync.WaitGroup{}
|
||||
for peerToQuery := range peers {
|
||||
@ -210,7 +210,7 @@ func (bs *bitswap) sendWantlistToProviders(ctx context.Context, wantlist *wl.Wan
|
||||
message := bsmsg.New()
|
||||
message.SetFull(true)
|
||||
for _, e := range bs.wantlist.Entries() {
|
||||
message.AddEntry(e.Value, e.Priority)
|
||||
message.AddEntry(e.Key, e.Priority)
|
||||
}
|
||||
|
||||
ps := pset.NewPeerSet()
|
||||
@ -229,7 +229,7 @@ func (bs *bitswap) sendWantlistToProviders(ctx context.Context, wantlist *wl.Wan
|
||||
bs.send(ctx, prov, message)
|
||||
}
|
||||
}
|
||||
}(e.Value)
|
||||
}(e.Key)
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
@ -5,6 +5,7 @@ import (
|
||||
|
||||
blocks "github.com/jbenet/go-ipfs/blocks"
|
||||
pb "github.com/jbenet/go-ipfs/exchange/bitswap/message/internal/pb"
|
||||
wantlist "github.com/jbenet/go-ipfs/exchange/bitswap/wantlist"
|
||||
inet "github.com/jbenet/go-ipfs/net"
|
||||
u "github.com/jbenet/go-ipfs/util"
|
||||
|
||||
@ -64,9 +65,8 @@ func newMsg() *impl {
|
||||
}
|
||||
|
||||
type Entry struct {
|
||||
Key u.Key
|
||||
Priority int
|
||||
Cancel bool
|
||||
wantlist.Entry
|
||||
Cancel bool
|
||||
}
|
||||
|
||||
func newMessageFromProto(pbm pb.Message) BitSwapMessage {
|
||||
@ -121,9 +121,11 @@ func (m *impl) addEntry(k u.Key, priority int, cancel bool) {
|
||||
e.Cancel = cancel
|
||||
} else {
|
||||
m.wantlist[k] = &Entry{
|
||||
Key: k,
|
||||
Priority: priority,
|
||||
Cancel: cancel,
|
||||
Entry: wantlist.Entry{
|
||||
Key: k,
|
||||
Priority: priority,
|
||||
},
|
||||
Cancel: cancel,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -61,7 +61,7 @@ func (lm *LedgerManager) taskWorker(ctx context.Context) {
|
||||
}
|
||||
continue
|
||||
}
|
||||
block, err := lm.bs.Get(nextTask.Key)
|
||||
block, err := lm.bs.Get(nextTask.Entry.Key)
|
||||
if err != nil {
|
||||
continue // TODO maybe return an error
|
||||
}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package strategy
|
||||
|
||||
import (
|
||||
wantlist "github.com/jbenet/go-ipfs/exchange/bitswap/wantlist"
|
||||
peer "github.com/jbenet/go-ipfs/peer"
|
||||
u "github.com/jbenet/go-ipfs/util"
|
||||
)
|
||||
@ -20,9 +21,8 @@ func newTaskQueue() *taskQueue {
|
||||
}
|
||||
|
||||
type task struct {
|
||||
Key u.Key
|
||||
Target peer.Peer
|
||||
theirPriority int
|
||||
Entry wantlist.Entry
|
||||
Target peer.Peer
|
||||
}
|
||||
|
||||
// Push currently adds a new task to the end of the list
|
||||
@ -31,13 +31,15 @@ func (tl *taskQueue) Push(block u.Key, priority int, to peer.Peer) {
|
||||
if task, ok := tl.taskmap[taskKey(to, block)]; ok {
|
||||
// TODO: when priority queue is implemented,
|
||||
// rearrange this task
|
||||
task.theirPriority = priority
|
||||
task.Entry.Priority = priority
|
||||
return
|
||||
}
|
||||
task := &task{
|
||||
Key: block,
|
||||
Target: to,
|
||||
theirPriority: priority,
|
||||
Entry: wantlist.Entry{
|
||||
Key: block,
|
||||
Priority: priority,
|
||||
},
|
||||
Target: to,
|
||||
}
|
||||
tl.tasks = append(tl.tasks, task)
|
||||
tl.taskmap[taskKey(to, block)] = task
|
||||
@ -52,9 +54,9 @@ func (tl *taskQueue) Pop() *task {
|
||||
// the same block from multiple peers
|
||||
out = tl.tasks[0]
|
||||
tl.tasks = tl.tasks[1:]
|
||||
delete(tl.taskmap, taskKey(out.Target, out.Key))
|
||||
delete(tl.taskmap, taskKey(out.Target, out.Entry.Key))
|
||||
// Filter out blocks that have been cancelled
|
||||
if out.theirPriority >= 0 {
|
||||
if out.Entry.Priority >= 0 { // FIXME separate the "cancel" signal from priority
|
||||
break
|
||||
}
|
||||
}
|
||||
@ -66,7 +68,7 @@ func (tl *taskQueue) Pop() *task {
|
||||
func (tl *taskQueue) Remove(k u.Key, p peer.Peer) {
|
||||
t, ok := tl.taskmap[taskKey(p, k)]
|
||||
if ok {
|
||||
t.theirPriority = -1
|
||||
t.Entry.Priority = -1
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -18,7 +18,7 @@ func New() *Wantlist {
|
||||
}
|
||||
|
||||
type Entry struct {
|
||||
Value u.Key
|
||||
Key u.Key
|
||||
Priority int
|
||||
}
|
||||
|
||||
@ -29,7 +29,7 @@ func (w *Wantlist) Add(k u.Key, priority int) {
|
||||
return
|
||||
}
|
||||
w.set[k] = &Entry{
|
||||
Value: k,
|
||||
Key: k,
|
||||
Priority: priority,
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user