Skip to content

Commit 79c3c1b

Browse files
refactor: apply suggestions in records (#1113)
1 parent 3c81035 commit 79c3c1b

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

records/providers_manager.go

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"fmt"
88
"io"
99
"strings"
10-
"sync"
1110
"time"
1211

1312
lru "github.com/hashicorp/golang-lru/simplelru"
@@ -67,9 +66,8 @@ type ProviderManager struct {
6766

6867
cleanupInterval time.Duration
6968

70-
ctx context.Context
7169
cancel context.CancelFunc
72-
wg sync.WaitGroup
70+
closed chan struct{}
7371
}
7472

7573
var _ ProviderStore = (*ProviderManager)(nil)
@@ -118,30 +116,31 @@ type getProv struct {
118116

119117
// NewProviderManager constructor
120118
func NewProviderManager(ctx context.Context, local peer.ID, ps peerstore.Peerstore, dstore ds.Batching, opts ...Option) (*ProviderManager, error) {
121-
pm := new(ProviderManager)
122-
pm.self = local
123-
pm.getprovs = make(chan *getProv)
124-
pm.newprovs = make(chan *addProv)
125-
pm.pstore = ps
126-
pm.dstore = autobatch.NewAutoBatching(dstore, batchBufferSize)
127119
cache, err := lru.NewLRU(lruCacheSize, nil)
128120
if err != nil {
129121
return nil, err
130122
}
131-
pm.cache = cache
132-
pm.cleanupInterval = defaultCleanupInterval
123+
pm := &ProviderManager{
124+
self: local,
125+
getprovs: make(chan *getProv),
126+
newprovs: make(chan *addProv),
127+
closed: make(chan struct{}),
128+
pstore: ps,
129+
dstore: autobatch.NewAutoBatching(dstore, batchBufferSize),
130+
cache: cache,
131+
cleanupInterval: defaultCleanupInterval,
132+
}
133133
if err := pm.applyOptions(opts...); err != nil {
134134
return nil, err
135135
}
136-
pm.ctx, pm.cancel = context.WithCancel(ctx)
137-
pm.run()
136+
ctx, pm.cancel = context.WithCancel(ctx)
137+
pm.run(ctx)
138138
return pm, nil
139139
}
140140

141-
func (pm *ProviderManager) run() {
142-
pm.wg.Add(1)
141+
func (pm *ProviderManager) run(ctx context.Context) {
143142
go func() {
144-
defer pm.wg.Done()
143+
defer close(pm.closed)
145144

146145
var gcQuery dsq.Results
147146
gcTimer := time.NewTimer(pm.cleanupInterval)
@@ -164,17 +163,17 @@ func (pm *ProviderManager) run() {
164163
case np := <-pm.newprovs:
165164
err := pm.addProv(np.ctx, np.key, np.val)
166165
if err != nil {
167-
log.Error("error adding new providers: ", err)
166+
log.Error("error adding new provider: ", err)
168167
continue
169168
}
170169
if gcSkip != nil {
171-
// we have an gc, tell it to skip this provider
170+
// gc in progress, tell it to skip this provider
172171
// as we've updated it since the GC started.
173172
gcSkip[mkProvKeyFor(np.key, np.val)] = struct{}{}
174173
}
175174
case gp := <-pm.getprovs:
176175
provs, err := pm.getProvidersForKey(gp.ctx, gp.key)
177-
if err != nil && err != ds.ErrNotFound {
176+
if err != nil && !errors.Is(err, ds.ErrNotFound) {
178177
log.Error("error reading providers: ", err)
179178
}
180179

@@ -210,7 +209,7 @@ func (pm *ProviderManager) run() {
210209
fallthrough
211210
case gcTime.Sub(t) > ProvideValidity:
212211
// or expired
213-
err = pm.dstore.Delete(pm.ctx, ds.RawKey(res.Key))
212+
err = pm.dstore.Delete(ctx, ds.RawKey(res.Key))
214213
if err != nil && err != ds.ErrNotFound {
215214
log.Error("failed to remove provider record from disk: ", err)
216215
}
@@ -224,7 +223,7 @@ func (pm *ProviderManager) run() {
224223
pm.cache.Purge()
225224

226225
// Now, kick off a GC of the datastore.
227-
q, err := pm.dstore.Query(pm.ctx, dsq.Query{
226+
q, err := pm.dstore.Query(ctx, dsq.Query{
228227
Prefix: ProvidersKeyPrefix,
229228
})
230229
if err != nil {
@@ -234,7 +233,7 @@ func (pm *ProviderManager) run() {
234233
gcQuery = q
235234
gcQueryRes = q.Next()
236235
gcSkip = make(map[string]struct{})
237-
case <-pm.ctx.Done():
236+
case <-ctx.Done():
238237
return
239238
}
240239
}
@@ -243,7 +242,7 @@ func (pm *ProviderManager) run() {
243242

244243
func (pm *ProviderManager) Close() error {
245244
pm.cancel()
246-
pm.wg.Wait()
245+
<-pm.closed
247246
return nil
248247
}
249248

@@ -377,7 +376,7 @@ func loadProviderSet(ctx context.Context, dstore ds.Datastore, k []byte) (*provi
377376
case now.Sub(t) > ProvideValidity:
378377
// or just expired
379378
err = dstore.Delete(ctx, ds.RawKey(e.Key))
380-
if err != nil && err != ds.ErrNotFound {
379+
if err != nil && errors.Is(err, ds.ErrNotFound) {
381380
log.Error("failed to remove provider record from disk: ", err)
382381
}
383382
continue
@@ -389,7 +388,7 @@ func loadProviderSet(ctx context.Context, dstore ds.Datastore, k []byte) (*provi
389388
if err != nil {
390389
log.Error("base32 decoding error: ", err)
391390
err = dstore.Delete(ctx, ds.RawKey(e.Key))
392-
if err != nil && err != ds.ErrNotFound {
391+
if err != nil && errors.Is(err, ds.ErrNotFound) {
393392
log.Error("failed to remove provider record from disk: ", err)
394393
}
395394
continue

0 commit comments

Comments
 (0)