<p>Hi, I was wondering why the standard <em>net/http.Server</em> does not support pooled connection serving. It seems to be a good alternative for the current solution, in which every request is served by a seperate goroutine:</p>
<pre><code>func (srv *Server) Serve(l net.Listener) error {
defer l.Close()
var tempDelay time.Duration // how long to sleep on accept failure
for {
rw, e := l.Accept()
if e != nil {
if ne, ok := e.(net.Error); ok && ne.Temporary() {
if tempDelay == 0 {
tempDelay = 5 * time.Millisecond
} else {
tempDelay *= 2
}
if max := 1 * time.Second; tempDelay > max {
tempDelay = max
}
srv.logf("http: Accept error: %v; retrying in %v", e, tempDelay)
time.Sleep(tempDelay)
continue
}
return e
}
tempDelay = 0
c, err := srv.newConn(rw)
if err != nil {
continue
}
c.setState(c.rwc, StateNew) // before Serve can return
go c.serve()
}
}
</code></pre>
<p><em>c.serve()</em> can be executed by a pooled, created earlier worker. Do the goroutine creation time is so small that it can be skipped? Even though, having a huge amount of goroutines the application becomes unstable and scheduling is a pain.</p>
<hr/>**评论:**<br/><br/>thepciet: <pre><blockquote>
<p>Do the goroutine creation time is so small that it can be skipped?</p>
</blockquote>
<p>This is the goal, where goroutines are NOT threads but a finer lighter construct meant to simplify real world concurrent programming (like just starting a goroutine instead of managing a pool).</p>
<p>The official golang blog covers a ton, as does the official documentation. e.g.: <a href="https://blog.golang.org/pipelines" rel="nofollow">https://blog.golang.org/pipelines</a></p></pre>sinatosk: <pre><p>Just implement the net.Listener interface to do pooling... I think that's what other people do... But if you want to respond to the client in http protocol then you just implement that in your handlers but that means accepting a connection which you want to pool... Either way I don't see the need of the net/http implementing that</p>
<p>Not really an answer to why as you asked</p></pre>m173314: <pre><p>The need comes from a simple fact - the cost of goroutine creation.</p></pre>kron4eg: <pre><p>goroutine creation is very cheap, you're trying to solve unexacting problem.</p></pre>m173314: <pre><p>So you try to tell me that having spawned 300k+ goroutines seems legit? I mean spawned by HTTP server, not business logic workers.</p></pre>xena-warrior-prince: <pre><p>Yes.</p></pre>TheMerovius: <pre><p>I tested it out, on my system I can spawn about 2 million goroutines without any significant scheduling problems. Go routines are dead cheap, that's the whole point.</p>
<p>Look at it this way: Effectively, the go runtime does the connection pooling you wish for: There is a finite number of workers (threads in this case) that handle units of work (goroutines in this case).</p></pre>kron4eg: <pre><p>yep, managing incoming connections pool is work for HAproxy/nginx and similar software.</p></pre>hayzeus: <pre><p>That is absolutely legit. I do an intro to go talk where I demo a program that spawns a couple of million on a dual core mbp. Takes around 7-8 microseconds per routine. Not zero overhead, but very little.</p></pre>SportingSnow21: <pre><p>If you're looking for savings on this fine-grain of a level, take a look at <a href="https://github.com/valyala/fasthttp" rel="nofollow">fasthttp</a> library. It takes care of the resource pooling to squeeze out more performance.</p></pre>valyala: <pre><p>While <a href="https://github.com/valyala/fasthttp/blob/master/workerpool.go" rel="nofollow">worker pool used in fasthttp</a> gives measurable performance gain comparing to creating a goroutine per request, it may be completely eliminated in the future if golang runtime is optimized properly, i.e. if it will keep per-cpu stack of stopped goroutines (metadata + stack) for subsequent reuse.</p>
<p>I don't know how golang runtime manages stopped goroutines at the moment, but it looks like there is a room for further optimizations.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传