mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-25 20:37:53 +08:00
this is a major refactor of the entire codebase it changes the monolithic peer.Peer into using a peer.ID and a peer.Peerstore. Other changes: - removed handshake3. - testutil vastly simplified peer - secio bugfix + debugging logs - testutil: RandKeyPair - backpressure bugfix: w.o.w. - peer: added hex enc/dec - peer: added a PeerInfo struct PeerInfo is a small struct used to pass around a peer with a set of addresses and keys. This is not meant to be a complete view of the system, but rather to model updates to the peerstore. It is used by things like the routing system. - updated peer/queue + peerset - latency metrics - testutil: use crand for PeerID gen RandPeerID generates random "valid" peer IDs. it does not NEED to generate keys because it is as if we lost the key right away. fine to read some randomness and hash it. to generate proper keys and an ID, use: sk, pk, _ := testutil.RandKeyPair() id, _ := peer.IDFromPublicKey(pk) Also added RandPeerIDFatal helper - removed old spipe - updated seccat - core: cleanup initIdentity - removed old getFromPeerList
85 lines
2.0 KiB
Go
85 lines
2.0 KiB
Go
package decision
|
|
|
|
import (
|
|
"sync"
|
|
|
|
wantlist "github.com/jbenet/go-ipfs/exchange/bitswap/wantlist"
|
|
peer "github.com/jbenet/go-ipfs/peer"
|
|
u "github.com/jbenet/go-ipfs/util"
|
|
)
|
|
|
|
// TODO: at some point, the strategy needs to plug in here
|
|
// to help decide how to sort tasks (on add) and how to select
|
|
// tasks (on getnext). For now, we are assuming a dumb/nice strategy.
|
|
type taskQueue struct {
|
|
// TODO: make this into a priority queue
|
|
lock sync.Mutex
|
|
tasks []*task
|
|
taskmap map[string]*task
|
|
}
|
|
|
|
func newTaskQueue() *taskQueue {
|
|
return &taskQueue{
|
|
taskmap: make(map[string]*task),
|
|
}
|
|
}
|
|
|
|
type task struct {
|
|
Entry wantlist.Entry
|
|
Target peer.ID
|
|
Trash bool
|
|
}
|
|
|
|
// Push currently adds a new task to the end of the list
|
|
func (tl *taskQueue) Push(entry wantlist.Entry, to peer.ID) {
|
|
tl.lock.Lock()
|
|
defer tl.lock.Unlock()
|
|
if task, ok := tl.taskmap[taskKey(to, entry.Key)]; ok {
|
|
// TODO: when priority queue is implemented,
|
|
// rearrange this task
|
|
task.Entry.Priority = entry.Priority
|
|
return
|
|
}
|
|
task := &task{
|
|
Entry: entry,
|
|
Target: to,
|
|
}
|
|
tl.tasks = append(tl.tasks, task)
|
|
tl.taskmap[taskKey(to, entry.Key)] = task
|
|
}
|
|
|
|
// Pop 'pops' the next task to be performed. Returns nil no task exists.
|
|
func (tl *taskQueue) Pop() *task {
|
|
tl.lock.Lock()
|
|
defer tl.lock.Unlock()
|
|
var out *task
|
|
for len(tl.tasks) > 0 {
|
|
// TODO: instead of zero, use exponential distribution
|
|
// it will help reduce the chance of receiving
|
|
// the same block from multiple peers
|
|
out = tl.tasks[0]
|
|
tl.tasks = tl.tasks[1:]
|
|
delete(tl.taskmap, taskKey(out.Target, out.Entry.Key))
|
|
if out.Trash {
|
|
continue // discarding tasks that have been removed
|
|
}
|
|
break // and return |out|
|
|
}
|
|
return out
|
|
}
|
|
|
|
// Remove lazily removes a task from the queue
|
|
func (tl *taskQueue) Remove(k u.Key, p peer.ID) {
|
|
tl.lock.Lock()
|
|
t, ok := tl.taskmap[taskKey(p, k)]
|
|
if ok {
|
|
t.Trash = true
|
|
}
|
|
tl.lock.Unlock()
|
|
}
|
|
|
|
// taskKey returns a key that uniquely identifies a task.
|
|
func taskKey(p peer.ID, k u.Key) string {
|
|
return string(p) + string(k)
|
|
}
|