Skip to content

Commit

Permalink
Merge pull request #69 from libp2p/feat/signal-dump
Browse files Browse the repository at this point in the history
handle SIGUSR1 as a signal to dump state
  • Loading branch information
vyzo authored Feb 14, 2019
2 parents f6d7dcd + cd5481c commit 67f225c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func NewDaemon(ctx context.Context, maddr ma.Multiaddr, dhtEnabled bool, dhtClie
d.listener = l

go d.listen()
go d.trapSignals()

return d, nil
}
Expand Down
8 changes: 8 additions & 0 deletions trap_nonposix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// +build windows plan9 nacl js

package p2pd

func (d *Daemon) trapSignals() {
// TODO: define signals we want to trap on Windows, if any.
return
}
34 changes: 34 additions & 0 deletions trap_posix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// +build !windows,!plan9,!nacl,!js

package p2pd

import (
"os"
"os/signal"
"syscall"
)

func (d *Daemon) trapSignals() {
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGUSR1)
for {
select {
case s := <-ch:
switch s {
case syscall.SIGUSR1:
d.handleSIGUSR1()
default:
log.Warningf("unexpected signal %d", s)
}
case <-d.ctx.Done():
return
}
}
}

func (d *Daemon) handleSIGUSR1() {
// this is the state dump signal; for now just dht routing table if present
if d.dht != nil {
d.dht.RoutingTable().Print()
}
}

0 comments on commit 67f225c

Please sign in to comment.