











go, get net/http connection count
var activeConnections int32 func handler(w http.ResponseWriter, r *http.Request) { atomic.AddInt32(&activeConnections, 1) defer atomic.AddInt32(&activeConnections, -1) // 处理请求逻辑 } func main() { http.HandleFunc("/", handler) http.HandleFunc("/stats", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Active connections: %d", atomic.LoadInt32(&activeConnections)) }) http.ListenAndServe(":8080", nil) }
using middleware:
func wrapHandler(handler http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { atomic.AddInt32(&activeConnections, 1) defer atomic.AddInt32(&activeConnections, -1) handler(w, r) } }
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。