如何限制Go中实现的HTTP Server的连接数? [英] How to limit the connections count of an HTTP Server implemented in Go?

查看:471
本文介绍了如何限制Go中实现的HTTP Server的连接数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Golang中实现HTTP服务器.

I am trying to implement an HTTP Server in Golang.

我的问题是,我必须在任何特定时间将最大活动连接数限制为20.

My problem is, I have to limit the maximum active connections count at any particular time to 20.

推荐答案

您可以使用

You can use the netutil.LimitListener function to wrap around net.Listener if you don't want to implement your own wrapper:-

connectionCount := 20

l, err := net.Listen("tcp", ":8000")

if err != nil {
    log.Fatalf("Listen: %v", err)
}

defer l.Close()

l = netutil.LimitListener(l, connectionCount)

log.Fatal(http.Serve(l, nil))

这篇关于如何限制Go中实现的HTTP Server的连接数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆