惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
宝玉的分享
宝玉的分享
Apple Machine Learning Research
Apple Machine Learning Research
阮一峰的网络日志
阮一峰的网络日志
博客园 - 司徒正美
酷 壳 – CoolShell
酷 壳 – CoolShell
IT之家
IT之家
H
Help Net Security
雷峰网
雷峰网
M
MIT News - Artificial intelligence
Microsoft Security Blog
Microsoft Security Blog
美团技术团队
GbyAI
GbyAI
博客园_首页
博客园 - 三生石上(FineUI控件)
T
Tailwind CSS Blog
I
InfoQ
小众软件
小众软件
Google DeepMind News
Google DeepMind News
D
Docker
Engineering at Meta
Engineering at Meta
WordPress大学
WordPress大学
博客园 - Franky
Hugging Face - Blog
Hugging Face - Blog

博客园 - calochCN

using webpack5 ubuntu 如何将任何app run为服务 这里想配置一下mysql主从,并归档,以备使用 hotkey resizer, rect win small app using C, tool utils Using ES6 Module In Browser. reprint, Use of logrotate go copy object deep深拷贝 手动数据库分库分片策略 记录一下ubuntu 挂载一下raid1 硬盘的过程 好久不弄css导航条,这里再布一下 sapui5 说说备案问题,说说发现的腾讯云和gitee这些国内服务商,使用过程中发现的一些猫腻 我最近觉得自己的收藏夹很乱,因为没有事情做,就想闲着给自己做个网址导航的软件【一】 学日语看到一个笑话 抽几分钟,写个文档系统的前端页面,先排版一下. 文字竖排,从上到下排列,仿古文的写法 从前做前端的时候, 联通的好多html5活动, 页面布局如何整齐,回顾一个常用的 我依然没有用quest2串流过一次... 这次还是决定好好用hugo写一下模板系统
go get net/http connections count, using middleware
calochCN · 2025-10-06 · via 博客园 - calochCN

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)
    }
}