Code RoomRequest counts corrupt under load
MediumPrep Room Coding #2200

Request counts corrupt under load

Code reviewConcurrencyMid–Senior~25 min

Review this Go HTTP handler that counts requests per endpoint.

What a strong answer looks like

Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix, specific and kind.

0:00 of about 25 min
Mark a line and say what kind of problem it is.0 findings
1var hits = map[string]int{}
2 
3func handler(w http.ResponseWriter, r *http.Request) {
4 path := r.URL.Path
5 hits[path]++
6 fmt.Fprintf(w, "path %s seen %d times\n", path, hits[path])
7}
8 
9func statsHandler(w http.ResponseWriter, r *http.Request) {
10 for p, n := range hits {
11 fmt.Fprintf(w, "%s = %d\n", p, n)
12 }
13}
14 
15func main() {
16 http.HandleFunc("/", handler)
17 http.HandleFunc("/stats", statsHandler)
18 http.ListenAndServe(":8080", nil)
19}
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.