Skip to content

Instantly share code, notes, and snippets.

@technovangelist
Last active March 30, 2019 01:21
Show Gist options
  • Save technovangelist/b48e95d995bc420eeab0 to your computer and use it in GitHub Desktop.
Save technovangelist/b48e95d995bc420eeab0 to your computer and use it in GitHub Desktop.
Trivial Expvar Sample Code
package main
import (
"expvar"
"github.com/paulbellamy/ratecounter"
"io"
"net/http"
"strconv"
"time"
)
var (
counter *ratecounter.RateCounter
hitsperminute = expvar.NewInt("hits_per_minute")
)
func increment(w http.ResponseWriter, r *http.Request) {
counter.Incr(1)
hitsperminute.Set(counter.Rate())
io.WriteString(w, strconv.FormatInt(counter.Rate(), 10))
}
func main() {
counter = ratecounter.NewRateCounter(1 * time.Minute)
http.HandleFunc("/increment", increment)
http.ListenAndServe(":8000", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment