Last active
January 22, 2016 19:36
-
-
Save frou/580b5e519fc3d45f5b63 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
package main | |
import ( | |
"fmt" | |
"net/http" | |
"syscall" | |
"github.com/frou/stdext" | |
) | |
const ( | |
addr = ":8113" | |
tickle = syscall.SIGUSR1 // $ kill -USR1 pid | |
) | |
func main() { | |
mutc := make(chan int) | |
getc := make(chan chan int) | |
go func() { | |
var n int | |
for { | |
select { | |
case i := <-mutc: | |
n += i | |
case c := <-getc: | |
c <- n | |
} | |
} | |
}() | |
stdext.HandleSignal(tickle, true, func() { mutc <- 1 }) | |
http.HandleFunc("/", | |
func(w http.ResponseWriter, r *http.Request) { | |
c := make(chan int) | |
getc <- c | |
fmt.Fprintln(w, <-c) | |
}) | |
stdext.Exit(http.ListenAndServe(addr, nil)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment