dongsisui7562
2016-04-05 18:33
浏览 137
已采纳

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.

图片转代码服务由CSDN问答提供 功能建议

我们在go中编写了一个基于gin-gonic的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毫秒内进行。 我们希望这些呼叫应并行处理。 这意味着所有呼叫应在17:24:40之前完成,而不是17:24:43。 IE:建立连接以处理请求时,服务器会生成新的线程/ goroutine。 如果不是这种情况,那么有人对以这种方式工作的软件包有任何建议吗?

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanshanduo3363 2016-04-05 19:03
    已采纳

    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

    点赞 打赏 评论
  • 老子学不动了 2020-09-15 00:44

    直接一张图解决你的问题,http请求最终是交给net/http包处理的,你仔细读一下http包里的这个代码的注释就懂了:
    图片说明

    点赞 打赏 评论

相关推荐 更多相似问题