-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change providers map and lock over to an agent based approach for man…
…aging providers
- Loading branch information
1 parent
334ea87
commit 914f052
Showing
2 changed files
with
95 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package dht | ||
|
||
import ( | ||
"time" | ||
|
||
u "github.com/jbenet/go-ipfs/util" | ||
peer "github.com/jbenet/go-ipfs/peer" | ||
) | ||
|
||
type ProviderManager struct { | ||
providers map[u.Key][]*providerInfo | ||
newprovs chan *addProv | ||
getprovs chan *getProv | ||
halt chan struct{} | ||
} | ||
|
||
type addProv struct { | ||
k u.Key | ||
val *peer.Peer | ||
} | ||
|
||
type getProv struct { | ||
k u.Key | ||
resp chan []*peer.Peer | ||
} | ||
|
||
func NewProviderManager() *ProviderManager { | ||
pm := new(ProviderManager) | ||
pm.getprovs = make(chan *getProv) | ||
pm.newprovs = make(chan *addProv) | ||
pm.providers = make(map[u.Key][]*providerInfo) | ||
pm.halt = make(chan struct{}) | ||
go pm.run() | ||
return pm | ||
} | ||
|
||
func (pm *ProviderManager) run() { | ||
tick := time.NewTicker(time.Hour) | ||
for { | ||
select { | ||
case np := <-pm.newprovs: | ||
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.Peer | ||
provs := pm.providers[gp.k] | ||
for _, p := range provs { | ||
parr = append(parr, p.Value) | ||
} | ||
gp.resp <- parr | ||
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.halt: | ||
return | ||
} | ||
} | ||
} | ||
|
||
func (pm *ProviderManager) AddProvider(k u.Key, val *peer.Peer) { | ||
pm.newprovs <- &addProv{ | ||
k: k, | ||
val: val, | ||
} | ||
} | ||
|
||
func (pm *ProviderManager) GetProviders(k u.Key) []*peer.Peer { | ||
gp := new(getProv) | ||
gp.k = k | ||
gp.resp = make(chan []*peer.Peer) | ||
pm.getprovs <- gp | ||
return <-gp.resp | ||
} |