mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-23 11:27:42 +08:00
- updated go-ctxgroup and goprocess ctxgroup: AddChildGroup was changed to AddChild. Used in two files: - p2p/net/mock/mock_net.go - routing/dht/dht.go - updated context from hg repo to git prev. commit in hg was ad01a6fcc8a19d3a4478c836895ffe883bd2ceab. (context: make parentCancelCtx iterative) represents commit 84f8955a887232b6308d79c68b8db44f64df455c in git repo - updated context to master (b6fdb7d8a4ccefede406f8fe0f017fb58265054c) Aaron Jacobs (2): net/context: Don't accept a context in the DoSomethingSlow example. context: Be clear that users must cancel the result of WithCancel. Andrew Gerrand (1): go.net: use golang.org/x/... import paths Bryan C. Mills (1): net/context: Don't leak goroutines in Done example. Damien Neil (1): context: fix removal of cancelled timer contexts from parent David Symonds (2): context: Fix WithValue example code. net: add import comments. Sameer Ajmani (1): context: fix TestAllocs to account for ints in interfaces
87 lines
1.8 KiB
Go
87 lines
1.8 KiB
Go
package mockrouting
|
|
|
|
import (
|
|
"math/rand"
|
|
"sync"
|
|
"time"
|
|
|
|
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
|
|
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
|
|
peer "github.com/jbenet/go-ipfs/p2p/peer"
|
|
u "github.com/jbenet/go-ipfs/util"
|
|
"github.com/jbenet/go-ipfs/util/testutil"
|
|
)
|
|
|
|
// server is the mockrouting.Client's private interface to the routing server
|
|
type server interface {
|
|
Announce(peer.PeerInfo, u.Key) error
|
|
Providers(u.Key) []peer.PeerInfo
|
|
|
|
Server
|
|
}
|
|
|
|
// s is an implementation of the private server interface
|
|
type s struct {
|
|
delayConf DelayConfig
|
|
|
|
lock sync.RWMutex
|
|
providers map[u.Key]map[peer.ID]providerRecord
|
|
}
|
|
|
|
type providerRecord struct {
|
|
Peer peer.PeerInfo
|
|
Created time.Time
|
|
}
|
|
|
|
func (rs *s) Announce(p peer.PeerInfo, k u.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 u.Key) []peer.PeerInfo {
|
|
rs.delayConf.Query.Wait() // before locking
|
|
|
|
rs.lock.RLock()
|
|
defer rs.lock.RUnlock()
|
|
|
|
var ret []peer.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, ds.NewMapDatastore())
|
|
}
|
|
|
|
func (rs *s) ClientWithDatastore(_ context.Context, p testutil.Identity, datastore ds.Datastore) Client {
|
|
return &client{
|
|
peer: p,
|
|
datastore: ds.NewMapDatastore(),
|
|
server: rs,
|
|
}
|
|
}
|