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

推荐订阅源

腾讯CDC
Microsoft Azure Blog
Microsoft Azure Blog
L
LangChain Blog
Y
Y Combinator Blog
Microsoft Security Blog
Microsoft Security Blog
宝玉的分享
宝玉的分享
B
Blog RSS Feed
MongoDB | Blog
MongoDB | Blog
Jina AI
Jina AI
D
Docker
B
Blog
Engineering at Meta
Engineering at Meta
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
I
InfoQ
G
Google Developers Blog
博客园 - Franky
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The GitHub Blog
The GitHub Blog
T
The Blog of Author Tim Ferriss
大猫的无限游戏
大猫的无限游戏
阮一峰的网络日志
阮一峰的网络日志
U
Unit 42

博客园 - 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)
    }
}