Last active
January 8, 2016 17:11
-
-
Save dokipen/e647594461e891afaf4a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func bindListener(port string) net.Listener { | |
for { | |
l, err := net.Listen("tcp", fmt.Sprintf(":%s", port)) | |
if err != nil { | |
dlog("%+v", err) | |
time.Sleep(time.Duration(1) * time.Second) | |
} else { | |
return l | |
} | |
} | |
} | |
func main() { | |
dlog("Starting HTTP") | |
hostname, err := os.Hostname() | |
if err != nil { | |
dlog("%+v", err) | |
hostname = "stormdrain-hostname-err" | |
} | |
dlog("Stormdrain starting up %v %v", hostname, os.Getpid()) | |
runtime.GOMAXPROCS(runtime.NumCPU()) | |
dlog("Using %v of %v logical CPUs", runtime.GOMAXPROCS(0), runtime.NumCPU()) | |
http.HandleFunc("/1/stats", stormdrain.StatsHandler) | |
http.HandleFunc("/1/e", stormdrain.EventHandler) | |
http.HandleFunc("/2/e", stormdrain.MultiEventHandler) | |
listener := bindListener(PORT) | |
dlog("Listening on %s", PORT) | |
c := make(chan os.Signal, 1) | |
signal.Notify(c, os.Interrupt, os.Kill, syscall.SIGINT, syscall.SIGTERM) | |
server := new(http.Server) | |
go func() { | |
<-c | |
listener.Close() | |
dlog("Listener Closed") | |
time.Sleep(time.Duration(10) * time.Second) | |
stormdrain.StreamEnd() | |
dlog("Done") | |
}() | |
if pidfile != "" { | |
var err error | |
pid := []byte(fmt.Sprintf("%d", os.Getpid())) | |
if err = ioutil.WriteFile(pidfile, pid, 0644); err != nil { | |
panic(fmt.Sprintf("Failed to write pidfile \"%s\"\n", pidfile)) | |
} | |
} | |
log.Fatal(server.Serve(listener)) | |
} | |
/* | |
stormdrain:main main.go:48 Starting HTTP +156.987951ms | |
stormdrain:main main.go:56 Stormdrain starting up bootsy 8553 +68.529µs | |
stormdrain:main main.go:58 Using 8 of 8 logical CPUs +83.697µs | |
stormdrain:main main.go:65 Listening on 13978 +28.603µs | |
^C stormdrain:main main.go:73 Listener Closed +772.581639ms | |
2016/01/08 12:08:01 accept tcp [::]:13978: use of closed network connection | |
exit status 1 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment