Skip to content

Commit

Permalink
cmd/dht-get-peers: Fix lockup on SIGINT
Browse files Browse the repository at this point in the history
  • Loading branch information
anacrolix committed Nov 17, 2014
1 parent 727aac2 commit c1049d0
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions cmd/dht-get-peers/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ var (
serveAddr = flag.String("serveAddr", ":0", "local UDP address")
infoHash = flag.String("infoHash", "", "torrent infohash")

s *dht.Server
s *dht.Server
quitting = make(chan struct{})
)

func loadTable() error {
Expand Down Expand Up @@ -94,7 +95,7 @@ func saveTable() error {
goodNodes := s.Nodes()
if *tableFileName == "" {
if len(goodNodes) != 0 {
log.Printf("discarding %d good nodes!", len(goodNodes))
log.Print("good nodes were discarded because you didn't specify a table file")
}
return nil
}
Expand Down Expand Up @@ -123,12 +124,14 @@ func setupSignals() {
signal.Notify(ch, os.Interrupt)
go func() {
<-ch
close(quitting)
s.Close()
}()
}

func main() {
seen := make(map[util.CompactPeer]struct{})
getPeers:
for {
ps, err := s.GetPeers(*infoHash)
if err != nil {
Expand All @@ -148,7 +151,11 @@ func main() {
}
}
}()
time.Sleep(15 * time.Second)
select {
case <-time.After(15 * time.Second):
case <-quitting:
break getPeers
}
}
if err := saveTable(); err != nil {
log.Printf("error saving node table: %s", err)
Expand Down

0 comments on commit c1049d0

Please sign in to comment.