reprovide: fix ipfs bitswap reprovide when interval set to 0
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Aug 20, 2017 at 18:23 UTC
ffc9abb8b8ad74ab43e7804edc86553c37b42251
3 files changed
+43
-15
core/core.go
+8
-10
@@ -283,20 +283,18 @@ func (n *IpfsNode) startLateOnlineServices(ctx context.Context) error {
283
}
284
n.Reprovider = rp.NewReprovider(ctx, n.Routing, keyProvider)
285
286
- if cfg.Reprovider.Interval != "0" {
287
- interval := kReprovideFrequency
288
- if cfg.Reprovider.Interval != "" {
289
- dur, err := time.ParseDuration(cfg.Reprovider.Interval)
290
- if err != nil {
291
- return err
292
- }
293
-
294
- interval = dur
286
+ reproviderInterval := kReprovideFrequency
287
+ if cfg.Reprovider.Interval != "" {
288
+ dur, err := time.ParseDuration(cfg.Reprovider.Interval)
289
+ if err != nil {
290
+ return err
291
}
292
297
- go n.Reprovider.ProvideEvery(interval)
293
+ reproviderInterval = dur
294
}
295
296
+ go n.Reprovider.Run(reproviderInterval)
297
+
298
return nil
299
}
300
exchange/reprovide/reprovide.go
+7
-3
@@ -38,14 +38,18 @@ func NewReprovider(ctx context.Context, rsys routing.ContentRouting, keyProvider
38
}
39
}
40
41
-// ProvideEvery re-provides keys with 'tick' interval
42
-func (rp *Reprovider) ProvideEvery(tick time.Duration) {
41
+// Run re-provides keys with 'tick' interval or when triggered
42
+func (rp *Reprovider) Run(tick time.Duration) {
43
// dont reprovide immediately.
44
// may have just started the daemon and shutting it down immediately.
45
// probability( up another minute | uptime ) increases with uptime.
46
after := time.After(time.Minute)
47
var done doneFunc
48
for {
49
+ if tick == 0 {
50
+ after = make(chan time.Time)
51
+ }
52
+
53
select {
54
case <-rp.ctx.Done():
55
return
@@ -98,7 +102,7 @@ func (rp *Reprovider) Reprovide() error {
102
return nil
103
}
104
101
-// Trigger starts reprovision process in rp.ProvideEvery and waits for it
105
+// Trigger starts reprovision process in rp.Run and waits for it
106
func (rp *Reprovider) Trigger(ctx context.Context) error {
107
progressCtx, done := context.WithCancel(ctx)
108
test/sharness/t0175-reprovider.sh
+28
-2
@@ -4,8 +4,9 @@ test_description="Test reprovider"
4
5
. lib/test-lib.sh
6
7
+NUM_NODES=6
8
+
9
init_strategy() {
8
- NUM_NODES=6
10
test_expect_success 'init iptb' '
11
iptb init -f -n $NUM_NODES --bootstrap=none --port=0
12
'
@@ -19,7 +20,7 @@ init_strategy() {
20
ipfsi 0 config Reprovider.Strategy '$1'
21
'
22
22
- startup_cluster 6 --debug
23
+ startup_cluster ${NUM_NODES}
24
}
25
26
findprovs_empty() {
@@ -124,4 +125,29 @@ test_expect_success 'stop peer 1' '
125
iptb stop 1
126
'
127
128
+# Test reprovider working with ticking disabled
129
+test_expect_success 'init iptb' '
130
+ iptb init -f -n $NUM_NODES --bootstrap=none --port=0
131
+'
132
+
133
+test_expect_success 'peer ids' '
134
+ PEERID_0=$(iptb get id 0) &&
135
+ PEERID_1=$(iptb get id 1)
136
+'
137
+
138
+test_expect_success 'Disable reprovider ticking' '
139
+ ipfsi 0 config Reprovider.Interval 0
140
+'
141
+
142
+startup_cluster ${NUM_NODES}
143
+
144
+test_expect_success 'add test object' '
145
+ HASH_0=$(echo "foo" | ipfsi 0 add -q --local)
146
+'
147
+
148
+findprovs_empty '$HASH_0'
149
+reprovide
150
+findprovs_expect '$HASH_0' '$PEERID_0'
151
+
152
+
153
test_done