昨天"朝花夕拾"栏目给出了 一款具有请求排队功能的并发受限服务器。
演示了互联网高并发请求,服务端遇到的现实情况(服务器高负载、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
这个缓释胶囊后问题得到解决。
生活就是这样,只要你愿意倒腾,总有新发现。