Skip to content

Commit

Permalink
refactor: ProviderManager (#492)
Browse files Browse the repository at this point in the history
* test: augment TestProviderManager test, add notes of future tests
* refactor(provider-manager): order funcs, update names for consistency, add code docs
  • Loading branch information
daviddias committed Mar 13, 2020
1 parent ad27ebc commit dffa2f8
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 153 deletions.
34 changes: 34 additions & 0 deletions providers/provider_set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package providers

import (
"time"

"github.com/libp2p/go-libp2p-core/peer"
)

// A providerSet has the list of providers and the time that they were added
// It is used as an intermediary data struct between what is stored in the datastore
// and the list of providers that get passed to the consumer of a .GetProviders call
type providerSet struct {
providers []peer.ID
set map[peer.ID]time.Time
}

func newProviderSet() *providerSet {
return &providerSet{
set: make(map[peer.ID]time.Time),
}
}

func (ps *providerSet) Add(p peer.ID) {
ps.setVal(p, time.Now())
}

func (ps *providerSet) setVal(p peer.ID, t time.Time) {
_, found := ps.set[p]
if !found {
ps.providers = append(ps.providers, p)
}

ps.set[p] = t
}

0 comments on commit dffa2f8

Please sign in to comment.