-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathtrap_posix.go
67 lines (58 loc) · 1.54 KB
/
trap_posix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//go:build !windows && !plan9 && !nacl && !js
package p2pd
import (
"fmt"
"os"
"os/signal"
"syscall"
)
func (d *Daemon) trapSignals() {
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGUSR1, syscall.SIGINT, syscall.SIGTERM)
for {
select {
case s := <-ch:
switch s {
case syscall.SIGUSR1:
d.handleSIGUSR1()
case syscall.SIGINT, syscall.SIGTERM:
d.Close()
os.Exit(0x80 + int(s.(syscall.Signal)))
default:
log.Warnw("uncaught signal", "signal", s)
}
case <-d.ctx.Done():
return
}
}
}
func (d *Daemon) handleSIGUSR1() {
// this is our signal to dump diagnostics info.
if d.dht != nil {
fmt.Println("DHT Routing Table:")
d.dht.RoutingTable().Print()
fmt.Println()
fmt.Println()
}
conns := d.host.Network().Conns()
fmt.Printf("Connections and streams (%d):\n", len(conns))
for _, c := range conns {
protos, _ := d.host.Peerstore().GetProtocols(c.RemotePeer()) // error value here is useless
protoVersion, err := d.host.Peerstore().Get(c.RemotePeer(), "ProtocolVersion")
if err != nil {
protoVersion = "(unknown)"
}
agent, err := d.host.Peerstore().Get(c.RemotePeer(), "AgentVersion")
if err != nil {
agent = "(unknown)"
}
streams := c.GetStreams()
fmt.Printf("peer: %s, multiaddr: %s\n", c.RemotePeer().String(), c.RemoteMultiaddr())
fmt.Printf("\tprotoVersion: %s, agent: %s\n", protoVersion, agent)
fmt.Printf("\tprotocols: %v\n", protos)
fmt.Printf("\tstreams (%d):\n", len(streams))
for _, s := range streams {
fmt.Println("\t\tprotocol: ", s.Protocol())
}
}
}