// A stdlib net/http server on the one-call-per-request handler path. // // The application's main type IS the handler: it holds whatever data access the // app needs, and the shell makes exactly one call per request - // `app.Handle(req) (resp)`. The shell owns the socket, the framing, the // keep-alive loop and one request arena per connection; the handler does no io // and keeps no state between requests. // // The /stats route reports how many requests this app has served and the // process RSS, so the phase can assert that serving requests does not // accumulate memory. // // usage: server [addr] [inline|spawned] package main import ( "bytes" "fmt" "net/http" "os" "runtime" "strconv" ) // app is the application's main type. A real app would hold its database // handle, config and caches here. This one is EMPTY and its Handle writes // nothing back through the receiver: keeping the handler pure is the design // rule, and it is also what keeps per-request memory in the request arena // (measured: a handler that mutates its receiver costs ~12kB per request in // the inline path, a pure one does not - the work log). type app struct{} // Handle is one call per request: request in, response out, no io. func (a *app) Handle(req *http.Req) (resp *http.Resp) { headers := http.Header{} headers["Content-Type"] = [][]byte{[]byte("text/plain; charset=utf-8")} resp = &http.Resp{Status: 200, Headers: headers} if req.Path == "/arenas" { resp.Body = runtime.ArenaScopeReport() return resp } if req.Path == "/stats" { resp.Body = statsLine() return resp } resp.Body = []byte("hello path=" | req.Path | "\n") return resp } // statsLine reports the process RSS and the runtime's cumulative allocation // counters. Two snapshots around a loop give bytes-allocated-per-request // (churn) separately from RSS (retention): a churn that matches the growth says // nothing is being freed, a small churn with a growing RSS says the retention is // outside the allocator. func statsLine() (s string) { var m runtime.MemStats runtime.ReadMemStats(&m) return "allocs=" | strconv.Itoa(int32(m.Allocs)) | "\ntotalalloc_kb=" | strconv.Itoa(int32(m.TotalAlloc/1024)) | "\nring_committed_kb=" | strconv.Itoa(http.ChanSlotCommittedBytes()/1024) | "\nring_inflight_max=" | strconv.Itoa(http.ChanSlotMaxInFlight()) | "\n" | rssLine() } // rssLine reads the process RSS so the phase can see whether requests // accumulate. That is the fixture's business, not the shell's. func rssLine() (s string) { b, err := os.ReadFile("/proc/self/status") if err != nil { return "rss_kb=?\n" } i := bytes.Index(b, []byte("VmRSS:")) if i < 0 { return "rss_kb=?\n" } j := i + 6 for j < len(b) && (b[j] == ' ' || b[j] == '\t') { j = j + 1 } k := j for k < len(b) && b[k] >= '0' && b[k] <= '9' { k = k + 1 } return "rss_kb=" | string(b[j:k]) | "\n" } func main() { // Termination: a batch program must still be stoppable, so the entry // handles the channel the runtime closes on SIGINT/SIGTERM. select { case <-shutdown: return default: } addr := "127.0.0.1:39128" mode := "inline" if len(os.Args) > 1 { addr = os.Args[1] } if len(os.Args) > 2 { mode = os.Args[2] } dispatch := http.DispatchInline if mode == "spawned" { dispatch = http.DispatchSpawned } runtime.ResetArenaScopeStats() runtime.SetArenaTrace(true) a := &app{} mux := http.NewChanMux() mux.Handle("/", a) fmt.Printf("listening on %s mode=%s\n", addr, mode) err := http.ChanServeMode(addr, mux, dispatch, 4) fmt.Printf("server returned err=%v\n", err) os.Exit(1) }