负载下降后线程会粘在周围
go threads sticking around after the load goes down
我有基于 go 的反向代理服务器。当我监控应用程序的指标时(使用普罗米修斯),我注意到当应用程序的负载上升时,应用程序上的线程(go_threads
)从大约 20 增加到大约 55。然后在负载消失后,这些线程仍然存在,即使在许多小时之后。
但是我可以看到 go_goroutines
和内存使用率下降,但线程没有下降。
我有几个问题
- go中线程池的默认大小是多少?
- 空闲线程会停留多长时间?
对于你的第一个问题,我找不到线程池的默认大小,但是 runtime/debug.SetMaxThreads 的文档似乎表明只要现有的 OS 线程就会创建一个新线程被封锁。可能 Go 运行时仅从一个 OS 线程开始并根据需要创建更多线程,但这可能因 Go 版本而异。
至于你的第二个问题,闲置的 OS 线程目前一直存在。有一个未解决的问题 https://github.com/golang/go/issues/14592 处理关闭空闲线程,但是从最新的 Go 版本 (1.14) 开始,几乎没有任何工作要做。
What is the default size of the thread pool in go?
How long do idle threads stick around?
通知:
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.
这意味着保持低线程数的最佳机会是减少阻塞系统调用。
我有基于 go 的反向代理服务器。当我监控应用程序的指标时(使用普罗米修斯),我注意到当应用程序的负载上升时,应用程序上的线程(go_threads
)从大约 20 增加到大约 55。然后在负载消失后,这些线程仍然存在,即使在许多小时之后。
但是我可以看到 go_goroutines
和内存使用率下降,但线程没有下降。
我有几个问题
- go中线程池的默认大小是多少?
- 空闲线程会停留多长时间?
对于你的第一个问题,我找不到线程池的默认大小,但是 runtime/debug.SetMaxThreads 的文档似乎表明只要现有的 OS 线程就会创建一个新线程被封锁。可能 Go 运行时仅从一个 OS 线程开始并根据需要创建更多线程,但这可能因 Go 版本而异。
至于你的第二个问题,闲置的 OS 线程目前一直存在。有一个未解决的问题 https://github.com/golang/go/issues/14592 处理关闭空闲线程,但是从最新的 Go 版本 (1.14) 开始,几乎没有任何工作要做。
What is the default size of the thread pool in go?
How long do idle threads stick around?
通知:
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.
这意味着保持低线程数的最佳机会是减少阻塞系统调用。