doulu3865 2018-02-22 23:50
浏览 53
已采纳

我是否需要产生多个Go Web服务器实例以充分利用我的CPU?

I'm not exactly sure how to ask this question but in my experience with NodeJS, which has a single thread and a process queue to manage async functions, you need to run an instance of your web server on seperate processes for each CPU thread - then load balance between each instance.

You can end up with 4 instances of the same software running, serving on 4 separate ports, being exposed on one port via the load balancer.

Typically, you'd use a service like PM2 to manage this process for you.

From my (basic) understanding, goroutines are not threads so my natural thinking wonders if that means Go needs to be run in the same way, multiple processes spanning for each CPU thread.

Is that the case? Or if I write a REST API using something like Gin, will Go automatically scale across CPU threads as the demand increases?

  • 写回答

1条回答 默认 最新

  • douyan4958 2018-02-23 04:22
    关注

    Virtually every Go web server is based on net/http and calls (eventually) http.ListenAndServe() on a http.Server. (In gin, router.Run() wraps this.) The documentation for http.Server explains that it:

    calls Serve to handle requests on incoming connections.

    http.Serve() does the real work of accepting connections,

    creating a new service goroutine for each.

    Goroutines are lighter than OS threads; it's possible to run many goroutines per thread. So, your app could potentially serve dozens (or hundreds or more?) of simultaneous connections on a single process.

    And by default Go will use all available processor cores; you can set the GOMAXPROCS environment variable if you have some pressing need to not use all processor cores.

    The GOMAXPROCS variable limits the number of operating system threads that can execute user-level Go code simultaneously. There is no limit to the number of threads that can be blocked in system calls on behalf of Go code; those do not count against the GOMAXPROCS limit. This package's GOMAXPROCS function queries and changes the limit.

    So you don't need to mess with weird process managers like PM2 or supervisor; you can just start your server from a normal systemd unit, or as PID 1 in a Docker container, and enjoy (but reap your children).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题