defer client.Close()
needsShutdown := make(chan os.Signal)
+ overallStatusCancel := make(chan struct{})
+ txStatsDumperCancel := make(chan struct{})
signal.Notify(needsShutdown, syscall.SIGTERM, syscall.SIGINT)
go func() {
<-needsShutdown
- txStatsDumpAll(client)
+ overallStatusCancel <- struct{}{}
+ txStatsDumperCancel <- struct{}{}
client.Close()
}()
os.MkdirAll(path.Join(FIFOsDir, PeersDir), 0777)
os.MkdirAll(path.Join(FIFOsDir, FilesDir), 0777)
log.Println("started", client.PublicIPs())
- go overallStatus(client)
+ go overallStatus(client, overallStatusCancel)
go fifoList(client)
go fifoDHTList(client)
go fifoAdd(client)
go fifoDel(client)
- go txStatsDumper(client)
+ go txStatsDumper(client, txStatsDumperCancel)
<-client.Closed()
+ <-overallStatusCancel
+ <-txStatsDumperCancel
log.Println("finished")
}
"github.com/dustin/go-humanize"
)
-func overallStatus(c *torrent.Client) {
+func overallStatus(c *torrent.Client, cancel chan struct{}) {
+ tick := time.Tick(time.Second)
var prev torrent.ConnStats
- for range time.Tick(time.Second) {
+ for {
+ select {
+ case <-cancel:
+ close(cancel)
+ return
+ case <-tick:
+ }
stats := c.ConnStats()
var peers int
for _, t := range c.Torrents() {
}
}
-func txStatsDumper(c *torrent.Client) {
- for range time.Tick(10 * time.Second) {
+func txStatsDumper(c *torrent.Client, cancel chan struct{}) {
+ tick := time.Tick(10 * time.Second)
+ for {
txStatsDumpAll(c)
+ select {
+ case <-cancel:
+ close(cancel)
+ return
+ case <-tick:
+ }
}
}