协程池是调用端并发请求的缓释胶囊

昨天"朝花夕拾"栏目给出了 一款具有请求排队功能的并发受限服务器。

演示了互联网高并发请求,服务端遇到的现实情况(服务器高负载、cpu打满、sql并发受限)。

文章的重点在于给服务提供方削峰填谷, 今天咱们把视角移到服务调用方。


如果是一个调用端程序同时发出这么多并发请求:

(在服务器处理单次请求耗时50ms的前提下)

并发客户端请求数concurrencyClients 服务器请求队列queueLength 服务器限流阈值 Maxoutstanding 总耗时ms
1000 100 10 5014

动图显示:

虽然1000个并发请求在5014ms全部处理完, 但是至少有一半的请求耗时超过3s, 那这又会有什么问题呢?

一般端到端的请求:为防止服务器处理过慢,长时间占用客户端到服务器的请求链路,客户端都会设置超时时间。

eg:

  • 前端ajax工具库设置超时时间:axios.defaults.timeout = 12000;
  • golang httpclient 设置超时时间: client := &http.Client{ Timeout: 10 * time.Second, }
  • etcd grpc请求超时时间:eClient.Get(ctx, "/foo")

很明显,这个例子中如果clientTimeout=3s, 就会导致大量请求失败。


既要让所有客户端请求都能被处理,又要保证不超过客户端自设的超时配置。

协程池这个缓释胶囊就可以上场了。

Package ants implements an efficient and reliable goroutine pool for Go.

With ants, Go applications are able to limit the number of active goroutines, recycle goroutines efficiently, and reduce the memory footprint significantly. Package ants is extremely useful in the scenarios where a massive number of goroutines are created and destroyed frequently, such as highly-concurrent batch processing systems, HTTP servers, services of asynchronous tasks, etc.

总体而言, ants是golang中用于将高并发的Goroutine削峰填谷, 起到缓释胶囊的作用。

将原clients()函数中无脑迅速启动1000个并发协程, 替换为ants库。

go 复制代码
func antsClients() {
	wg1.Add(concurrencyClients)
	pool, _ := ants.NewPool(50)
	defer pool.Release()
	for i := 1; i <= concurrencyClients; i++ {
		r := &Request{
			args:       []int{i},
			resultChan: make(chan int),
		}
		_ = pool.Submit(func() {
			ClientReq(r)
		})
	}
	wg1.Wait()
}

动图显示,整体耗时相比于 不用协程池无差, 但是每个请求的耗时都得到了很好的控制, 整个客户端程序批量发起1000个请求显得轻快又高效。

源代码还是在github.com/zwbdzb/go_s... 欢迎关注。

That's All, 本文是自己在中厂的一个生产实践复盘:

调用方某些场景下突发批量请求,一开始也是自然启动协程发起请求,大量请求因为服务端限制而超时失败, 启动ants这个缓释胶囊后问题得到解决。

生活就是这样,只要你愿意倒腾,总有新发现。

相关推荐
To_OC2 小时前
Next.js + Redis 构建笔记系统:Redis Hash 实战与性能优化
redis·后端·next.js
北斗落凡尘4 小时前
LangGraph 入门实战(11)--输出模式
后端·python·langchain
demo007x5 小时前
Hermes-Agent 技术架构
前端·后端·agent
__zRainy__7 小时前
Node系列 · Node基础:net 模块
后端·node.js
Diligently_7 小时前
Anolis 系统更新与密码设置&磁盘扩容
java·后端·spring
kyson_7 小时前
大文件切片上传:从“能传”到“可靠”的 Vue 3 + Java 全栈实现
后端
张龙6877 小时前
uv 全面指南:比 pip 快 100 倍的 Python 包管理器,从安装到团队落地
后端·python
用户40966601317518 小时前
Guava 不止有 Lists 和 Maps:Cache、EventBus、Multimap 实战
java·后端
一只叫煤球的猫9 小时前
从 Java 21 到 Java 25:ThreadLocal 以外的选择—— ScopedValue
java·后端·架构