Skip to content

Commit

Permalink
Interrupt cleanups for all ports
Browse files Browse the repository at this point in the history
* Be it Domain Socket or TCP Socket. Atach the cleanup listener.
* Add functiionality for attaching exit callbacks.
  • Loading branch information
meson10 committed Feb 24, 2015
1 parent 13d46e3 commit 86f62b0
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions local_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ func cleanAddr(addr string) {
}
}

func interrupt_cleanup(addr string) {
if strings.Index(addr, "/") != 0 {
return
}
var cleanupFuncs = []func(){}

func OnSysExit(cleanup func()) {
cleanupFuncs = append(cleanupFuncs, cleanup)
}

func interrupt_cleanup(addr string) {
sigchan := make(chan os.Signal, 10)
signal.Notify(sigchan, os.Interrupt, syscall.SIGTERM)
//NOTE: Capture every Signal right now.
Expand All @@ -35,8 +37,19 @@ func interrupt_cleanup(addr string) {
s := <-sigchan
log.Println("Exiting Program. Got Signal: ", s)

// do last actions and wait for all write operations to end
cleanAddr(addr)
if strings.Index(addr, "/") == 0 {
// do last actions and wait for all write operations to end
cleanAddr(addr)
}

if len(cleanupFuncs) > 0 {
log.Println("Performing Cleanup routines")

for i := range cleanupFuncs {
cleanupFuncs[i]()
}
}

os.Exit(0)
}

Expand Down Expand Up @@ -103,6 +116,8 @@ func makeServer() {
log.Println("Listening on " + addr)
}

go interrupt_cleanup(addr)

if strings.Index(addr, "/") == 0 {
listener, err := net.Listen("unix", addr)
if err != nil {
Expand All @@ -122,7 +137,6 @@ func makeServer() {
}
}

go interrupt_cleanup(addr)
os.Chmod(addr, 0770)
serverError = http.Serve(listener, nil)
} else {
Expand Down

0 comments on commit 86f62b0

Please sign in to comment.