mirror of
https://github.com/ipfs/kubo.git
synced 2025-08-06 11:31:54 +08:00
90 lines
2.1 KiB
Go
90 lines
2.1 KiB
Go
package mockrouting
|
|
|
|
import (
|
|
"math/rand"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/ipfs/go-ipfs/thirdparty/testutil"
|
|
ds "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore"
|
|
dssync "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore/sync"
|
|
key "gx/ipfs/Qmce4Y4zg3sYr7xKM5UueS67vhNni6EeWgCRnb7MbLJMew/go-key"
|
|
|
|
pstore "gx/ipfs/QmSZi9ygLohBUGyHMqE5N6eToPwqcg7bZQTULeVLFu7Q6d/go-libp2p-peerstore"
|
|
peer "gx/ipfs/QmWtbQU15LaB5B1JC2F7TV9P4K88vD3PpA4AJrwfCjhML8/go-libp2p-peer"
|
|
context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
|
|
)
|
|
|
|
// server is the mockrouting.Client's private interface to the routing server
|
|
type server interface {
|
|
Announce(pstore.PeerInfo, key.Key) error
|
|
Providers(key.Key) []pstore.PeerInfo
|
|
|
|
Server
|
|
}
|
|
|
|
// s is an implementation of the private server interface
|
|
type s struct {
|
|
delayConf DelayConfig
|
|
|
|
lock sync.RWMutex
|
|
providers map[key.Key]map[peer.ID]providerRecord
|
|
}
|
|
|
|
type providerRecord struct {
|
|
Peer pstore.PeerInfo
|
|
Created time.Time
|
|
}
|
|
|
|
func (rs *s) Announce(p pstore.PeerInfo, k key.Key) error {
|
|
rs.lock.Lock()
|
|
defer rs.lock.Unlock()
|
|
|
|
_, ok := rs.providers[k]
|
|
if !ok {
|
|
rs.providers[k] = make(map[peer.ID]providerRecord)
|
|
}
|
|
rs.providers[k][p.ID] = providerRecord{
|
|
Created: time.Now(),
|
|
Peer: p,
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (rs *s) Providers(k key.Key) []pstore.PeerInfo {
|
|
rs.delayConf.Query.Wait() // before locking
|
|
|
|
rs.lock.RLock()
|
|
defer rs.lock.RUnlock()
|
|
|
|
var ret []pstore.PeerInfo
|
|
records, ok := rs.providers[k]
|
|
if !ok {
|
|
return ret
|
|
}
|
|
for _, r := range records {
|
|
if time.Now().Sub(r.Created) > rs.delayConf.ValueVisibility.Get() {
|
|
ret = append(ret, r.Peer)
|
|
}
|
|
}
|
|
|
|
for i := range ret {
|
|
j := rand.Intn(i + 1)
|
|
ret[i], ret[j] = ret[j], ret[i]
|
|
}
|
|
|
|
return ret
|
|
}
|
|
|
|
func (rs *s) Client(p testutil.Identity) Client {
|
|
return rs.ClientWithDatastore(context.Background(), p, dssync.MutexWrap(ds.NewMapDatastore()))
|
|
}
|
|
|
|
func (rs *s) ClientWithDatastore(_ context.Context, p testutil.Identity, datastore ds.Datastore) Client {
|
|
return &client{
|
|
peer: p,
|
|
datastore: datastore,
|
|
server: rs,
|
|
}
|
|
}
|