Skip to content

Commit 79b6991

Browse files
Merge pull request ipfs#44 from vyzo/vyzo-fix-ticker-leak
fix ticker leak in BootstrapWithConfig
2 parents 6823833 + 7a4eb0f commit 79b6991

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

dht_bootstrap.go

+16-7
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,14 @@ func (dht *IpfsDHT) Bootstrap(ctx context.Context) error {
7373
// These parameters are configurable.
7474
//
7575
// BootstrapWithConfig returns a process, so the user can stop it.
76-
func (dht *IpfsDHT) BootstrapWithConfig(config BootstrapConfig) (goprocess.Process, error) {
77-
sig := time.Tick(config.Period)
78-
return dht.BootstrapOnSignal(config, sig)
76+
func (dht *IpfsDHT) BootstrapWithConfig(cfg BootstrapConfig) (goprocess.Process, error) {
77+
if cfg.Queries <= 0 {
78+
return nil, fmt.Errorf("invalid number of queries: %d", cfg.Queries)
79+
}
80+
81+
proc := periodicproc.Tick(cfg.Period, dht.bootstrapWorker(cfg))
82+
83+
return proc, nil
7984
}
8085

8186
// SignalBootstrap ensures the dht routing table remains healthy as peers come and go.
@@ -93,7 +98,13 @@ func (dht *IpfsDHT) BootstrapOnSignal(cfg BootstrapConfig, signal <-chan time.Ti
9398
return nil, fmt.Errorf("invalid signal: %v", signal)
9499
}
95100

96-
proc := periodicproc.Ticker(signal, func(worker goprocess.Process) {
101+
proc := periodicproc.Ticker(signal, dht.bootstrapWorker(cfg))
102+
103+
return proc, nil
104+
}
105+
106+
func (dht *IpfsDHT) bootstrapWorker(cfg BootstrapConfig) func(worker goprocess.Process) {
107+
return func(worker goprocess.Process) {
97108
// it would be useful to be able to send out signals of when we bootstrap, too...
98109
// maybe this is a good case for whole module event pub/sub?
99110

@@ -102,9 +113,7 @@ func (dht *IpfsDHT) BootstrapOnSignal(cfg BootstrapConfig, signal <-chan time.Ti
102113
log.Warning(err)
103114
// A bootstrapping error is important to notice but not fatal.
104115
}
105-
})
106-
107-
return proc, nil
116+
}
108117
}
109118

110119
// runBootstrap builds up list of peers by requesting random peer IDs

0 commit comments

Comments
 (0)