forked from mirrors/statsd_exporter
Add a signal handler to allow clean up of unixgram socket file
Signed-off-by: Wangchong Zhou <fffonion@gmail.com>
This commit is contained in:
parent
9ed6d59151
commit
383ee9bd3b
2 changed files with 30 additions and 3 deletions
18
exporter.go
18
exporter.go
|
@ -652,7 +652,13 @@ func (l *StatsDUDPListener) Listen(e chan<- Events) {
|
||||||
for {
|
for {
|
||||||
n, _, err := l.conn.ReadFromUDP(buf)
|
n, _, err := l.conn.ReadFromUDP(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
// https://github.com/golang/go/issues/4373
|
||||||
|
// ignore net: errClosing error as it will occur during shutdown
|
||||||
|
if strings.HasSuffix(err.Error(), "use of closed network connection") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Error(err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
l.handlePacket(buf[0:n], e)
|
l.handlePacket(buf[0:n], e)
|
||||||
}
|
}
|
||||||
|
@ -677,6 +683,11 @@ func (l *StatsDTCPListener) Listen(e chan<- Events) {
|
||||||
for {
|
for {
|
||||||
c, err := l.conn.AcceptTCP()
|
c, err := l.conn.AcceptTCP()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
// https://github.com/golang/go/issues/4373
|
||||||
|
// ignore net: errClosing error as it will occur during shutdown
|
||||||
|
if strings.HasSuffix(err.Error(), "use of closed network connection") {
|
||||||
|
return
|
||||||
|
}
|
||||||
log.Fatalf("AcceptTCP failed: %v", err)
|
log.Fatalf("AcceptTCP failed: %v", err)
|
||||||
}
|
}
|
||||||
go l.handleConn(c, e)
|
go l.handleConn(c, e)
|
||||||
|
@ -717,6 +728,11 @@ func (l *StatsDUnixgramListener) Listen(e chan<- Events) {
|
||||||
for {
|
for {
|
||||||
n, _, err := l.conn.ReadFromUnix(buf)
|
n, _, err := l.conn.ReadFromUnix(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
// https://github.com/golang/go/issues/4373
|
||||||
|
// ignore net: errClosing error as it will occur during shutdown
|
||||||
|
if strings.HasSuffix(err.Error(), "use of closed network connection") {
|
||||||
|
return
|
||||||
|
}
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
l.handlePacket(buf[:n], e)
|
l.handlePacket(buf[:n], e)
|
||||||
|
|
15
main.go
15
main.go
|
@ -18,7 +18,9 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
"github.com/howeyc/fsnotify"
|
"github.com/howeyc/fsnotify"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
@ -199,7 +201,8 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if *statsdListenUnixgram != "" {
|
if *statsdListenUnixgram != "" {
|
||||||
if _, err := os.Stat(*statsdListenUnixgram); !os.IsNotExist(err) {
|
var err error
|
||||||
|
if _, err = os.Stat(*statsdListenUnixgram); !os.IsNotExist(err) {
|
||||||
log.Fatalf("Unixgram socket \"%s\" already exists", *statsdListenUnixgram)
|
log.Fatalf("Unixgram socket \"%s\" already exists", *statsdListenUnixgram)
|
||||||
}
|
}
|
||||||
uxgconn, err := net.ListenUnixgram("unixgram", &net.UnixAddr{
|
uxgconn, err := net.ListenUnixgram("unixgram", &net.UnixAddr{
|
||||||
|
@ -225,6 +228,8 @@ func main() {
|
||||||
// if it's an abstract unix domain socket, it won't exist on fs
|
// if it's an abstract unix domain socket, it won't exist on fs
|
||||||
// so we can't chmod it either
|
// so we can't chmod it either
|
||||||
if _, err := os.Stat(*statsdListenUnixgram); !os.IsNotExist(err) {
|
if _, err := os.Stat(*statsdListenUnixgram); !os.IsNotExist(err) {
|
||||||
|
defer os.Remove(*statsdListenUnixgram)
|
||||||
|
|
||||||
// convert the string to octet
|
// convert the string to octet
|
||||||
perm, err := strconv.ParseInt("0"+string(*statsdUnixSocketMode), 8, 32)
|
perm, err := strconv.ParseInt("0"+string(*statsdUnixSocketMode), 8, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -254,5 +259,11 @@ func main() {
|
||||||
go watchConfig(*mappingConfig, mapper)
|
go watchConfig(*mappingConfig, mapper)
|
||||||
}
|
}
|
||||||
exporter := NewExporter(mapper)
|
exporter := NewExporter(mapper)
|
||||||
exporter.Listen(events)
|
|
||||||
|
signals := make(chan os.Signal)
|
||||||
|
signal.Notify(signals, os.Interrupt, syscall.SIGTERM)
|
||||||
|
|
||||||
|
go exporter.Listen(events)
|
||||||
|
|
||||||
|
<-signals
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue