Skip to content

Commit 0532e1c

Browse files
migrate providers package (#1094)
1 parent dc0dee4 commit 0532e1c

File tree

10 files changed

+848
-18
lines changed

10 files changed

+848
-18
lines changed

dht.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"github.com/libp2p/go-libp2p-kad-dht/internal/metrics"
2424
"github.com/libp2p/go-libp2p-kad-dht/netsize"
2525
pb "github.com/libp2p/go-libp2p-kad-dht/pb"
26-
"github.com/libp2p/go-libp2p-kad-dht/providers"
26+
"github.com/libp2p/go-libp2p-kad-dht/records"
2727
"github.com/libp2p/go-libp2p-kad-dht/rtrefresh"
2828
kb "github.com/libp2p/go-libp2p-kbucket"
2929
"github.com/libp2p/go-libp2p-kbucket/peerdiversity"
@@ -86,7 +86,7 @@ type IpfsDHT struct {
8686

8787
routingTable *kb.RoutingTable // Array of routing tables for differently distanced nodes
8888
// providerStore stores & manages the provider records for this Dht peer.
89-
providerStore providers.ProviderStore
89+
providerStore records.ProviderStore
9090

9191
// manages Routing Table refresh
9292
rtRefreshManager *rtrefresh.RtRefreshManager
@@ -365,7 +365,7 @@ func makeDHT(ctx context.Context, h host.Host, cfg dhtcfg.Config) (*IpfsDHT, err
365365
if cfg.ProviderStore != nil {
366366
dht.providerStore = cfg.ProviderStore
367367
} else {
368-
dht.providerStore, err = providers.NewProviderManager(dht.ctx, h.ID(), dht.peerstore, cfg.Datastore)
368+
dht.providerStore, err = records.NewProviderManager(dht.ctx, h.ID(), dht.peerstore, cfg.Datastore)
369369
if err != nil {
370370
return nil, fmt.Errorf("initializing default provider manager (%v)", err)
371371
}
@@ -456,7 +456,7 @@ func makeRoutingTable(dht *IpfsDHT, cfg dhtcfg.Config, maxLastSuccessfulOutbound
456456
}
457457

458458
// ProviderStore returns the provider storage object for storing and retrieving provider records.
459-
func (dht *IpfsDHT) ProviderStore() providers.ProviderStore {
459+
func (dht *IpfsDHT) ProviderStore() records.ProviderStore {
460460
return dht.providerStore
461461
}
462462

dht_options.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/libp2p/go-libp2p-kad-dht/amino"
1010
dhtcfg "github.com/libp2p/go-libp2p-kad-dht/internal/config"
1111
pb "github.com/libp2p/go-libp2p-kad-dht/pb"
12-
"github.com/libp2p/go-libp2p-kad-dht/providers"
12+
"github.com/libp2p/go-libp2p-kad-dht/records"
1313
"github.com/libp2p/go-libp2p-kbucket/peerdiversity"
1414
record "github.com/libp2p/go-libp2p-record"
1515
"github.com/libp2p/go-libp2p/core/host"
@@ -42,7 +42,7 @@ const DefaultPrefix protocol.ID = amino.ProtocolPrefix
4242
type Option = dhtcfg.Option
4343

4444
// ProviderStore sets the provider storage manager.
45-
func ProviderStore(ps providers.ProviderStore) Option {
45+
func ProviderStore(ps records.ProviderStore) Option {
4646
return func(c *dhtcfg.Config) error {
4747
c.ProviderStore = ps
4848
return nil

dht_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616

1717
"github.com/libp2p/go-libp2p-kad-dht/internal"
1818
"github.com/libp2p/go-libp2p-kad-dht/internal/net"
19-
"github.com/libp2p/go-libp2p-kad-dht/providers"
19+
"github.com/libp2p/go-libp2p-kad-dht/records"
2020
"github.com/libp2p/go-libp2p/core/crypto"
2121
"github.com/libp2p/go-libp2p/core/event"
2222
"github.com/libp2p/go-libp2p/core/network"
@@ -635,7 +635,7 @@ type testProviderManager struct {
635635
close func() error
636636
}
637637

638-
var _ providers.ProviderStore = (*testProviderManager)(nil)
638+
var _ records.ProviderStore = (*testProviderManager)(nil)
639639

640640
func (t *testProviderManager) AddProvider(ctx context.Context, key []byte, prov peer.AddrInfo) error {
641641
return t.addProvider(ctx, key, prov)

fullrt/dht.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import (
4141
internalConfig "github.com/libp2p/go-libp2p-kad-dht/internal/config"
4242
"github.com/libp2p/go-libp2p-kad-dht/internal/net"
4343
dht_pb "github.com/libp2p/go-libp2p-kad-dht/pb"
44-
"github.com/libp2p/go-libp2p-kad-dht/providers"
44+
"github.com/libp2p/go-libp2p-kad-dht/records"
4545
kb "github.com/libp2p/go-libp2p-kbucket"
4646

4747
record "github.com/libp2p/go-libp2p-record"
@@ -82,7 +82,7 @@ type FullRT struct {
8282

8383
enableValues, enableProviders bool
8484
Validator record.Validator
85-
ProviderManager *providers.ProviderManager
85+
ProviderManager *records.ProviderManager
8686
datastore ds.Datastore
8787
h host.Host
8888

@@ -184,7 +184,7 @@ func NewFullRT(h host.Host, protocolPrefix protocol.ID, options ...Option) (*Ful
184184
ctx, cancel := context.WithCancel(context.Background())
185185

186186
self := h.ID()
187-
pm, err := providers.NewProviderManager(ctx, self, h.Peerstore(), dhtcfg.Datastore, fullrtcfg.pmOpts...)
187+
pm, err := records.NewProviderManager(ctx, self, h.Peerstore(), dhtcfg.Datastore, fullrtcfg.pmOpts...)
188188
if err != nil {
189189
cancel()
190190
return nil, err

fullrt/options.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66

77
kaddht "github.com/libp2p/go-libp2p-kad-dht"
88
"github.com/libp2p/go-libp2p-kad-dht/crawler"
9-
"github.com/libp2p/go-libp2p-kad-dht/providers"
9+
"github.com/libp2p/go-libp2p-kad-dht/records"
1010
)
1111

1212
type config struct {
@@ -17,7 +17,7 @@ type config struct {
1717
bulkSendParallelism int
1818
timeoutPerOp time.Duration
1919
crawler crawler.Crawler
20-
pmOpts []providers.Option
20+
pmOpts []records.Option
2121
ipDiversityFilterLimit int
2222
}
2323

@@ -94,8 +94,8 @@ func WithTimeoutPerOperation(t time.Duration) Option {
9494
}
9595

9696
// WithProviderManagerOptions sets the options to use when instantiating
97-
// providers.ProviderManager.
98-
func WithProviderManagerOptions(pmOpts ...providers.Option) Option {
97+
// records.ProviderManager.
98+
func WithProviderManagerOptions(pmOpts ...records.Option) Option {
9999
return func(opt *config) error {
100100
opt.pmOpts = pmOpts
101101
return nil

internal/config/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/libp2p/go-libp2p-kad-dht/amino"
1313
"github.com/libp2p/go-libp2p-kad-dht/internal/net"
1414
pb "github.com/libp2p/go-libp2p-kad-dht/pb"
15-
"github.com/libp2p/go-libp2p-kad-dht/providers"
15+
"github.com/libp2p/go-libp2p-kad-dht/records"
1616
"github.com/libp2p/go-libp2p-kbucket/peerdiversity"
1717
record "github.com/libp2p/go-libp2p-record"
1818
"github.com/libp2p/go-libp2p/core/host"
@@ -49,7 +49,7 @@ type Config struct {
4949
MaxRecordAge time.Duration
5050
EnableProviders bool
5151
EnableValues bool
52-
ProviderStore providers.ProviderStore
52+
ProviderStore records.ProviderStore
5353
QueryPeerFilter QueryFilterFunc
5454
LookupCheckConcurrency int
5555
MsgSenderBuilder func(h host.Host, protos []protocol.ID) pb.MessageSenderWithDisconnect
@@ -128,7 +128,7 @@ var Defaults = func(o *Config) error {
128128
o.RoutingTable.AutoRefresh = true
129129
o.RoutingTable.PeerFilter = EmptyRTFilter
130130

131-
o.MaxRecordAge = providers.ProvideValidity
131+
o.MaxRecordAge = records.ProvideValidity
132132

133133
o.BucketSize = amino.DefaultBucketSize
134134
o.Concurrency = amino.DefaultConcurrency

providers/providers_manager.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,23 @@ import (
2626
const (
2727
// ProvidersKeyPrefix is the prefix/namespace for ALL provider record
2828
// keys stored in the data store.
29+
//
30+
// Deprecated: use records.ProvidersKeyPrefix
2931
ProvidersKeyPrefix = "/providers/"
3032

3133
// ProviderAddrTTL is the TTL to keep the multi addresses of provider
3234
// peers around. Those addresses are returned alongside provider. After
3335
// it expires, the returned records will require an extra lookup, to
3436
// find the multiaddress associated with the returned peer id.
37+
//
38+
// Deprecated: use records.ProviderAddrTTL
3539
ProviderAddrTTL = amino.DefaultProviderAddrTTL
3640
)
3741

3842
// ProvideValidity is the default time that a Provider Record should last on DHT
3943
// This value is also known as Provider Record Expiration Interval.
4044
var (
45+
// Deprecated: use records.ProvideValidity
4146
ProvideValidity = amino.DefaultProvideValidity
4247
defaultCleanupInterval = time.Hour
4348
lruCacheSize = 256
@@ -46,6 +51,8 @@ var (
4651
)
4752

4853
// ProviderStore represents a store that associates peers and their addresses to keys.
54+
//
55+
// Deprecated: use records.ProviderStore
4956
type ProviderStore interface {
5057
AddProvider(ctx context.Context, key []byte, prov peer.AddrInfo) error
5158
GetProviders(ctx context.Context, key []byte) ([]peer.AddrInfo, error)
@@ -54,6 +61,8 @@ type ProviderStore interface {
5461

5562
// ProviderManager adds and pulls providers out of the datastore,
5663
// caching them in between
64+
//
65+
// Deprecated: use records.ProviderManager
5766
type ProviderManager struct {
5867
self peer.ID
5968
// all non channel fields are meant to be accessed only within
@@ -75,6 +84,8 @@ type ProviderManager struct {
7584
var _ ProviderStore = (*ProviderManager)(nil)
7685

7786
// Option is a function that sets a provider manager option.
87+
//
88+
// Deprecated: use records.Option
7889
type Option func(*ProviderManager) error
7990

8091
func (pm *ProviderManager) applyOptions(opts ...Option) error {
@@ -88,6 +99,8 @@ func (pm *ProviderManager) applyOptions(opts ...Option) error {
8899

89100
// CleanupInterval sets the time between GC runs.
90101
// Defaults to 1h.
102+
//
103+
// Deprecated: use records package instead
91104
func CleanupInterval(d time.Duration) Option {
92105
return func(pm *ProviderManager) error {
93106
pm.cleanupInterval = d
@@ -97,6 +110,8 @@ func CleanupInterval(d time.Duration) Option {
97110

98111
// Cache sets the LRU cache implementation.
99112
// Defaults to a simple LRU cache.
113+
//
114+
// Deprecated: use records package instead
100115
func Cache(c lru.LRUCache) Option {
101116
return func(pm *ProviderManager) error {
102117
pm.cache = c
@@ -117,6 +132,8 @@ type getProv struct {
117132
}
118133

119134
// NewProviderManager constructor
135+
//
136+
// Deprecated: use records.NewProviderManager
120137
func NewProviderManager(ctx context.Context, local peer.ID, ps peerstore.Peerstore, dstore ds.Batching, opts ...Option) (*ProviderManager, error) {
121138
pm := new(ProviderManager)
122139
pm.self = local

records/provider_set.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package records
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)