mirror of
https://github.com/fluencelabs/go-libp2p-kad-dht
synced 2025-05-31 15:01:19 +00: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
132 lines
2.6 KiB
Go
132 lines
2.6 KiB
Go
package dht
|
|
|
|
import (
|
|
"time"
|
|
|
|
ctxgroup "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-ctxgroup"
|
|
peer "github.com/jbenet/go-ipfs/p2p/peer"
|
|
u "github.com/jbenet/go-ipfs/util"
|
|
|
|
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
|
|
)
|
|
|
|
type providerInfo struct {
|
|
Creation time.Time
|
|
Value peer.ID
|
|
}
|
|
|
|
type ProviderManager struct {
|
|
providers map[u.Key][]*providerInfo
|
|
local map[u.Key]struct{}
|
|
lpeer peer.ID
|
|
getlocal chan chan []u.Key
|
|
newprovs chan *addProv
|
|
getprovs chan *getProv
|
|
period time.Duration
|
|
ctxgroup.ContextGroup
|
|
}
|
|
|
|
type addProv struct {
|
|
k u.Key
|
|
val peer.ID
|
|
}
|
|
|
|
type getProv struct {
|
|
k u.Key
|
|
resp chan []peer.ID
|
|
}
|
|
|
|
func NewProviderManager(ctx context.Context, local peer.ID) *ProviderManager {
|
|
pm := new(ProviderManager)
|
|
pm.getprovs = make(chan *getProv)
|
|
pm.newprovs = make(chan *addProv)
|
|
pm.providers = make(map[u.Key][]*providerInfo)
|
|
pm.getlocal = make(chan chan []u.Key)
|
|
pm.local = make(map[u.Key]struct{})
|
|
pm.ContextGroup = ctxgroup.WithContext(ctx)
|
|
|
|
pm.Children().Add(1)
|
|
go pm.run()
|
|
|
|
return pm
|
|
}
|
|
|
|
func (pm *ProviderManager) run() {
|
|
defer pm.Children().Done()
|
|
|
|
tick := time.NewTicker(time.Hour)
|
|
for {
|
|
select {
|
|
case np := <-pm.newprovs:
|
|
if np.val == pm.lpeer {
|
|
pm.local[np.k] = struct{}{}
|
|
}
|
|
pi := new(providerInfo)
|
|
pi.Creation = time.Now()
|
|
pi.Value = np.val
|
|
arr := pm.providers[np.k]
|
|
pm.providers[np.k] = append(arr, pi)
|
|
|
|
case gp := <-pm.getprovs:
|
|
var parr []peer.ID
|
|
provs := pm.providers[gp.k]
|
|
for _, p := range provs {
|
|
parr = append(parr, p.Value)
|
|
}
|
|
gp.resp <- parr
|
|
|
|
case lc := <-pm.getlocal:
|
|
var keys []u.Key
|
|
for k, _ := range pm.local {
|
|
keys = append(keys, k)
|
|
}
|
|
lc <- keys
|
|
|
|
case <-tick.C:
|
|
for k, provs := range pm.providers {
|
|
var filtered []*providerInfo
|
|
for _, p := range provs {
|
|
if time.Now().Sub(p.Creation) < time.Hour*24 {
|
|
filtered = append(filtered, p)
|
|
}
|
|
}
|
|
pm.providers[k] = filtered
|
|
}
|
|
|
|
case <-pm.Closing():
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func (pm *ProviderManager) AddProvider(k u.Key, val peer.ID) {
|
|
pm.newprovs <- &addProv{
|
|
k: k,
|
|
val: val,
|
|
}
|
|
}
|
|
|
|
func (pm *ProviderManager) GetProviders(ctx context.Context, k u.Key) []peer.ID {
|
|
gp := &getProv{
|
|
k: k,
|
|
resp: make(chan []peer.ID, 1), // buffered to prevent sender from blocking
|
|
}
|
|
select {
|
|
case <-ctx.Done():
|
|
return nil
|
|
case pm.getprovs <- gp:
|
|
}
|
|
select {
|
|
case <-ctx.Done():
|
|
return nil
|
|
case peers := <-gp.resp:
|
|
return peers
|
|
}
|
|
}
|
|
|
|
func (pm *ProviderManager) GetLocal() []u.Key {
|
|
resp := make(chan []u.Key)
|
|
pm.getlocal <- resp
|
|
return <-resp
|
|
}
|