Skip to content

Instantly share code, notes, and snippets.

@stuartnelson3
Last active March 16, 2021 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stuartnelson3/07be2813157920a356d79499bd0e126f to your computer and use it in GitHub Desktop.
Save stuartnelson3/07be2813157920a356d79499bd0e126f to your computer and use it in GitHub Desktop.
sample file for logging incoming requests and timing between requests
package main
import (
"log"
"net/http"
"time"
)
func main() {
// Map to record timings between different paths
type prev struct {
t0, tLast time.Time
}
m := make(map[string]*prev)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
p := r.URL.Path
q, prs := m[p]
if !prs {
q = &prev{
t0: time.Now(),
tLast: time.Now(),
}
}
// Calculate total time and time since last request.
// Update q.tLast for next calculation.
t := time.Now()
total := t.Sub(q.t0)
d := t.Sub(q.tLast)
q.tLast = t
m[p] = q
log.Printf("total=%v delta=%v path=%s\n", total, d, r.URL.Path)
// see if we can return various responses and see how that
// effects the retry timers and things.
// http.StatusTooManyRequests, eg.
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("error"))
})
log.Println("listening on port :9200")
log.Fatal(http.ListenAndServe(":9200", nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment