Combine server into single 'git-style' executable

This commit is contained in:
2013-09-02 09:36:03 -04:00
parent a6c296e0fd
commit 2599717d09
9 changed files with 43 additions and 38 deletions

43
asinkd/exit.go Normal file
View File

@ -0,0 +1,43 @@
package main
import (
"os"
"os/signal"
"sync/atomic"
)
var exitWaiterCount int32
var exitCalled chan int
var exitWaiterChan chan int
func init() {
exitWaiterCount = 0
exitWaiterChan = make(chan int)
go setupCleanExitOnSignals()
}
func setupCleanExitOnSignals() {
//wait to properly close the socket when we're exiting
exitCode := 0
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
defer signal.Stop(sig)
select {
case <-sig:
case exitCode = <-exitCalled:
}
for c := atomic.AddInt32(&exitWaiterCount, -1); c >= 0; c = atomic.AddInt32(&exitWaiterCount, -1) {
exitWaiterChan <- exitCode
}
}
func Exit(exitCode int) {
exitCalled <- exitCode
}
func WaitOnExit() int {
atomic.AddInt32(&exitWaiterCount, 1)
return <-exitWaiterChan
}