我是否需要生成多个 Go Web 服务器实例才能充分利用我的 CPU?
Do I need to spawn multiple Go web server instances to fully utilize my CPU?
我不太确定如何问这个问题,但根据我使用 NodeJS 的经验,它有一个线程和一个进程队列来管理异步函数,你需要 运行 你的 web 实例服务器在每个 CPU 线程的单独进程上 - 然后在每个实例之间进行负载平衡。
您最终可以得到同一软件的 4 个实例 运行ning,服务于 4 个独立的端口,通过负载平衡器暴露在一个端口上。
通常,您会使用像 PM2 这样的服务来为您管理这个过程。
根据我的(基本)理解,goroutines 不是线程,所以我的自然思维想知道这是否意味着 Go 需要以相同的方式 运行,每个 CPU 线程跨越多个进程。
是这样吗?或者,如果我使用 Gin 之类的东西编写 REST API,Go 会随着需求的增加自动扩展 CPU 个线程吗?
几乎每个 Go 网络服务器都基于 net/http
并调用(最终)http.ListenAndServe()
on a http.Server
. (In gin, router.Run()
包装它。)http.Server
的文档解释说:
calls Serve to handle requests on incoming connections.
http.Serve()
完成接受连接的实际工作,
creating a new service goroutine for each.
Goroutines 比 OS 线程更轻;每个线程可以 运行 许多 goroutines。因此,您的应用可能会为数十个(或数百个或更多?)
单个进程上的同时连接数。
默认情况下,Go 将使用所有可用的处理器内核;如果您有一些迫切需要 而不是 使用所有处理器核心,您可以设置 GOMAXPROCS
environment variable。
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.
所以你不需要和像 PM2 或 supervisor 这样奇怪的流程管理器打交道;你可以从一个普通的 systemd 单元启动你的服务器,或者作为 Docker 容器中的 PID 1,然后享受(但是收获你的 children)。
我不太确定如何问这个问题,但根据我使用 NodeJS 的经验,它有一个线程和一个进程队列来管理异步函数,你需要 运行 你的 web 实例服务器在每个 CPU 线程的单独进程上 - 然后在每个实例之间进行负载平衡。
您最终可以得到同一软件的 4 个实例 运行ning,服务于 4 个独立的端口,通过负载平衡器暴露在一个端口上。
通常,您会使用像 PM2 这样的服务来为您管理这个过程。
根据我的(基本)理解,goroutines 不是线程,所以我的自然思维想知道这是否意味着 Go 需要以相同的方式 运行,每个 CPU 线程跨越多个进程。
是这样吗?或者,如果我使用 Gin 之类的东西编写 REST API,Go 会随着需求的增加自动扩展 CPU 个线程吗?
几乎每个 Go 网络服务器都基于 net/http
并调用(最终)http.ListenAndServe()
on a http.Server
. (In gin, router.Run()
包装它。)http.Server
的文档解释说:
calls Serve to handle requests on incoming connections.
http.Serve()
完成接受连接的实际工作,
creating a new service goroutine for each.
Goroutines 比 OS 线程更轻;每个线程可以 运行 许多 goroutines。因此,您的应用可能会为数十个(或数百个或更多?) 单个进程上的同时连接数。
默认情况下,Go 将使用所有可用的处理器内核;如果您有一些迫切需要 而不是 使用所有处理器核心,您可以设置 GOMAXPROCS
environment variable。
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.
所以你不需要和像 PM2 或 supervisor 这样奇怪的流程管理器打交道;你可以从一个普通的 systemd 单元启动你的服务器,或者作为 Docker 容器中的 PID 1,然后享受(但是收获你的 children)。