forked from ipfs/kubo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
reprovide.go
73 lines (64 loc) · 1.81 KB
/
reprovide.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package reprovide
import (
"context"
"fmt"
"time"
blocks "github.com/ipfs/go-ipfs/blocks/blockstore"
routing "gx/ipfs/QmNdaQ8itUU9jEZUwTsG4gHMaPmRfi6FEe89QjQAFbep3M/go-libp2p-routing"
backoff "gx/ipfs/QmPJUtEJsm5YLUWhF6imvyCH8KZXRJa9Wup7FDMwTy5Ufz/backoff"
logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
)
var log = logging.Logger("reprovider")
type Reprovider struct {
// The routing system to provide values through
rsys routing.ContentRouting
// The backing store for blocks to be provided
bstore blocks.Blockstore
}
func NewReprovider(rsys routing.ContentRouting, bstore blocks.Blockstore) *Reprovider {
return &Reprovider{
rsys: rsys,
bstore: bstore,
}
}
func (rp *Reprovider) ProvideEvery(ctx context.Context, tick time.Duration) {
// dont reprovide immediately.
// may have just started the daemon and shutting it down immediately.
// probability( up another minute | uptime ) increases with uptime.
after := time.After(time.Minute)
for {
select {
case <-ctx.Done():
return
case <-after:
err := rp.Reprovide(ctx)
if err != nil {
log.Debug(err)
}
after = time.After(tick)
}
}
}
func (rp *Reprovider) Reprovide(ctx context.Context) error {
keychan, err := rp.bstore.AllKeysChan(ctx)
if err != nil {
return fmt.Errorf("Failed to get key chan from blockstore: %s", err)
}
for c := range keychan {
op := func() error {
err := rp.rsys.Provide(ctx, c, true)
if err != nil {
log.Debugf("Failed to provide key: %s", err)
}
return err
}
// TODO: this backoff library does not respect our context, we should
// eventually work contexts into it. low priority.
err := backoff.Retry(op, backoff.NewExponentialBackOff())
if err != nil {
log.Debugf("Providing failed after number of retries: %s", err)
return err
}
}
return nil
}