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:
Wangchong Zhou 2019-04-22 16:31:24 -07:00
parent 9ed6d59151
commit 383ee9bd3b
No known key found for this signature in database
GPG key ID: B607274584E8D5E5
2 changed files with 30 additions and 3 deletions

View file

@ -652,7 +652,13 @@ func (l *StatsDUDPListener) Listen(e chan<- Events) {
for {
n, _, err := l.conn.ReadFromUDP(buf)
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)
}
@ -677,6 +683,11 @@ func (l *StatsDTCPListener) Listen(e chan<- Events) {
for {
c, err := l.conn.AcceptTCP()
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)
}
go l.handleConn(c, e)
@ -717,6 +728,11 @@ func (l *StatsDUnixgramListener) Listen(e chan<- Events) {
for {
n, _, err := l.conn.ReadFromUnix(buf)
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)
}
l.handlePacket(buf[:n], e)

15
main.go
View file

@ -18,7 +18,9 @@ import (
"net"
"net/http"
"os"
"os/signal"
"strconv"
"syscall"
"github.com/howeyc/fsnotify"
"github.com/prometheus/client_golang/prometheus"
@ -199,7 +201,8 @@ func main() {
}
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)
}
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
// so we can't chmod it either
if _, err := os.Stat(*statsdListenUnixgram); !os.IsNotExist(err) {
defer os.Remove(*statsdListenUnixgram)
// convert the string to octet
perm, err := strconv.ParseInt("0"+string(*statsdUnixSocketMode), 8, 32)
if err != nil {
@ -254,5 +259,11 @@ func main() {
go watchConfig(*mappingConfig, mapper)
}
exporter := NewExporter(mapper)
exporter.Listen(events)
signals := make(chan os.Signal)
signal.Notify(signals, os.Interrupt, syscall.SIGTERM)
go exporter.Listen(events)
<-signals
}