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

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

问题描述



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

解决方案

您可以使用 netutil.LimitListener 函数环绕 net.Listener if你不想实现自己的包装: -

  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,无))


I am trying to implement an HTTP Server in golang.

My problem is, I have to limit the maximum active connection 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天全站免登陆