Last active
March 16, 2021 16:56
-
-
Save stuartnelson3/07be2813157920a356d79499bd0e126f to your computer and use it in GitHub Desktop.
sample file for logging incoming requests and timing between requests
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 ( | |
"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