Skip to content

Instantly share code, notes, and snippets.

@frou
Last active January 22, 2016 19:36
Show Gist options
  • Save frou/580b5e519fc3d45f5b63 to your computer and use it in GitHub Desktop.
Save frou/580b5e519fc3d45f5b63 to your computer and use it in GitHub Desktop.
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