gin-gonic是否并行处理请求?

huangapple go评论101阅读模式
英文:

Does gin-gonic process requests in parallel?

问题

我们有一个使用gin-gonic编写的go语言API服务器。我们注意到一些奇怪的现象,导致我们相信它是按顺序处理请求而不是预期的并行操作。请考虑以下日志文件:

[GIN] 2016/04/05 - 17:24:37 | 200 |    5.738742ms | 64.... |   POST    /api/v2/d/
[GIN] 2016/04/05 - 17:24:40 | 200 |  3.262816256s | 64.... |   POST    /api/v2/d/
[GIN] 2016/04/05 - 17:24:42 | 200 |    3.563779ms | 64.... |   POST    /api/v2/d/
[GIN] 2016/04/05 - 17:24:43 | 200 |     105.429μs | 64.... |   POST    /api/v2/d/
[GIN] 2016/04/05 - 17:24:43 | 200 |     808.824μs | 64.... |   POST    /api/v2/d/

实时观察日志,只有在第二个调用完成后,最后3个条目才会显示出来。这五个调用在5毫秒内被发送到API。我们期望这些调用应该并行处理。这意味着所有的调用应该在17:24:40之前完成,而不是在17:24:43。也就是说,服务器在建立连接时会生成一个新的线程/协程来处理请求。如果不是这种情况,是否有人对一个能够按照我们期望方式工作的包有任何建议?

这是我们使用gin-gonic的第一个项目,我想知道是否有一些需要设置的配置参数。欢迎任何想法/建议。

英文:

We have a API server written in go that is based on gin-gonic. We've noticed something odd that has led us to believe that it is processing requests serially rather than the expected parallel operation. Consider this log file:

[GIN] 2016/04/05 - 17:24:37 | 200 |    5.738742ms | 64.... |   POST    /api/v2/d/
[GIN] 2016/04/05 - 17:24:40 | 200 |  3.262816256s | 64.... |   POST    /api/v2/d/
[GIN] 2016/04/05 - 17:24:42 | 200 |    3.563779ms | 64.... |   POST    /api/v2/d/
[GIN] 2016/04/05 - 17:24:43 | 200 |     105.429µs | 64.... |   POST    /api/v2/d/
[GIN] 2016/04/05 - 17:24:43 | 200 |     808.824µs | 64.... |   POST    /api/v2/d/

Watching the log in real time, the last 3 entries are not displayed until the second call finishes. These five calls are made to the API within 5 milliseconds of each other. We expect that the calls should be processed in parallel. This implies that all the calls should complete by 17:24:40, not 17:24:43. IE: That the server spawns a new thread/goroutine when the connection is made to process the request. If that is not the case does anyone have any suggestions for a package that does work that way.

This is our first project with gin-gonic and I'm wondering if there is some configuration parameter that needs to be set. Any ideas/suggestions are appreciated.

答案1

得分: 7

回答你的根问题:stdlib http.Serve (doc) 函数在初始连接接受和一些连接工作之后将请求分配给一个 goroutine。

概念性的思考:

Go 语言具有旨在提供强大并发能力的原语,但并发并不等同于并行。

如果你有多个处理器核心,并且你的 GOMAXPROCS 环境变量设置为大于 1,那么在适当的 goroutine 的情况下,除了并发,你可能还会看到一些并行性。

从 Go 1.5 开始,GOMAXPROCS 的默认设置是 CPU 核心数。之前的 Go 版本将 GOMAXPROCS 的设置默认为 1。

William Kennedy 在几年前对这些差异进行了很好的阐述:http://www.goinggo.net/2014/01/concurrency-goroutines-and-gomaxprocs.html

英文:

To answer your root question; The stdlib http.Serve (doc) func farms the request out to a goroutine after the initial connection accept and some connection work.

Conceptual Rambling:

Go has primitives that are designed to provide strong concurrency capability, but concurrency is not the same as parallelism.

If you have more than one processor core, and if your GOMAXPROCS environment is set to be more than 1, then you may see some parallelism in addition to concurrency, assuming the appropriate goroutines.

As of Go 1.5 the default setting for GOMAXPROCS is the number of CPU cores. Prior versions of Go default the GOMAXPROCS setting to 1.

William Kennedy had a good write up about the differences a couple years ago: http://www.goinggo.net/2014/01/concurrency-goroutines-and-gomaxprocs.html

huangapple
  • 本文由 发表于 2016年4月6日 02:33:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/36434332.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定