Skip to content

Commit dffa2f8

Browse files
authored
refactor: ProviderManager (#492)
* test: augment TestProviderManager test, add notes of future tests * refactor(provider-manager): order funcs, update names for consistency, add code docs
1 parent ad27ebc commit dffa2f8

File tree

3 files changed

+181
-153
lines changed

3 files changed

+181
-153
lines changed

providers/provider_set.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package providers
2+
3+
import (
4+
"time"
5+
6+
"github.com/libp2p/go-libp2p-core/peer"
7+
)
8+
9+
// A providerSet has the list of providers and the time that they were added
10+
// It is used as an intermediary data struct between what is stored in the datastore
11+
// and the list of providers that get passed to the consumer of a .GetProviders call
12+
type providerSet struct {
13+
providers []peer.ID
14+
set map[peer.ID]time.Time
15+
}
16+
17+
func newProviderSet() *providerSet {
18+
return &providerSet{
19+
set: make(map[peer.ID]time.Time),
20+
}
21+
}
22+
23+
func (ps *providerSet) Add(p peer.ID) {
24+
ps.setVal(p, time.Now())
25+
}
26+
27+
func (ps *providerSet) setVal(p peer.ID, t time.Time) {
28+
_, found := ps.set[p]
29+
if !found {
30+
ps.providers = append(ps.providers, p)
31+
}
32+
33+
ps.set[p] = t
34+
}

0 commit comments

Comments
 (0)