How can I throttle number of connections/sec to a tcp/http server?

polaris · · 462 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I am writing a a SockJS server in go using the SockJS-go package. I want to be able to have a connection throttling mechanism which will ensure that I not get more new tcp connections than I can handle at a time.</p> <p>Presently, I have code that looks like this.</p> <pre><code>listener, err := net.Listen(&#34;tcp&#34;, endpoint) http.Serve(listener, nil) </code></pre> <p>How can I ensure that the server accepts atmost say 200 new connections/second and the rest are rejected.</p> <p>I know that there are packages to rate limit the number of http requests/sec. But what I am looking for is rate limiting number of new connections coming in.</p> <hr/>**评论:**<br/><br/>zeiko_is_back: <pre><pre><code>package llimit import ( &#34;context&#34; &#34;net&#34; &#34;golang.org/x/time/rate&#34; ) type throttledListener struct { net.Listener *rate.Limiter } func NewThrottledListener(l net.Listener, r *rate.Limiter) net.Listener { return &amp;throttledListener{l, r} } func (l *throttledListener) Accept() (net.Conn, error) { l.Wait(context.Background()) return l.Listener.Accept() } </code></pre> <p>.</p> <pre><code>l, _ := net.Listen(&#34;tcp&#34;, endpoint) r := rate.NewLimiter(rate.Limit(200), 1) l = llimit.NewThrottledListener(l, r) http.Serve(l, nil) </code></pre></pre>pmjtoca: <pre><p>You should investigate the rate limiter by juju (see: <a href="https://github.com/juju/ratelimit" rel="nofollow">https://github.com/juju/ratelimit</a>) based on the token bucket algorithm. In particular in the &#39;ratelimit_test.go&#39; you have an: &#39;about: &#34;concurrent requests&#34;....&#39;. </p> <p>The ratelimit provided by Roger Peppe is not tied to data transfer limiting, I.e. you can limit arbitrary things with it.</p> <p>And there is also: <a href="https://godoc.org/astuart.co/limio" rel="nofollow">https://godoc.org/astuart.co/limio</a> with its positioning towards juju/ratelimiter here: <a href="https://groups.google.com/forum/#!topic/go-kit/gWjYUyH4dY8" rel="nofollow">https://groups.google.com/forum/#!topic/go-kit/gWjYUyH4dY8</a></p></pre>Robonia: <pre><p>Your link leads to a 404. </p> <p>Edit: Correct link <a href="https://github.com/juju/ratelimit" rel="nofollow">https://github.com/juju/ratelimit</a></p></pre>pmjtoca: <pre><p>Sorry. It is corrected.</p></pre>faster: <pre><p>Can Throttled (<a href="https://github.com/throttled/throttled" rel="nofollow">https://github.com/throttled/throttled</a>) do that?</p></pre>eviltofu: <pre><p>I&#39;m quite new to Go but I&#39;ve given this a stab. <a href="https://gist.github.com/eviltofu/c3441eef2ea58d16725f7ef0b2ba1adf" rel="nofollow">https://gist.github.com/eviltofu/c3441eef2ea58d16725f7ef0b2ba1adf</a></p> <p>edit: <em>oops</em> I made the mistake in assuming that it was a socket listener but I think the same principles can apply?</p></pre>tgulacsi: <pre><p>You can use a buffered channel to limit concurrent connections, but you need to put it into the for loop before the accept. (Insert a struct{}{} into the chan before calling accept, and recv from the chan after you&#39;ve closed the connection).</p></pre>lesmond: <pre><p>You could use a channel to pipeline them across to?</p> <p>Make it buffered and check the length before adding to it. </p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

462 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传