Skip to content

Commit 023672e

Browse files
committed
Add doc.go
1 parent 4db04ca commit 023672e

File tree

3 files changed

+53
-41
lines changed

3 files changed

+53
-41
lines changed

README.md

+22-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# ants
1+
<h1 align='center'>ants</h1>
22
<p align="center">
33
<img src="https://raw.githubusercontent.com/panjf2000/logos/master/ants/logo.png"/>
44
<b>A goroutine pool for Go</b>
@@ -14,11 +14,13 @@
1414
<a title="Mentioned in Awesome Go" target="_blank" href="https://github.com/avelino/awesome-go"><img src="https://awesome.re/mentioned-badge-flat.svg"></a>
1515
</p>
1616

17-
# [[中文](README_ZH.md)]
17+
English | [🇨🇳中文](README_ZH.md)
18+
19+
## 📖 Introduction
1820

1921
Library `ants` implements a goroutine pool with fixed capacity, managing and recycling a massive number of goroutines, allowing developers to limit the number of goroutines in your concurrent programs.
2022

21-
## Features:
23+
## 🚀 Features:
2224

2325
- Managing and recycling a massive number of goroutines automatically
2426
- Purging overdue goroutines periodically
@@ -27,7 +29,7 @@ Library `ants` implements a goroutine pool with fixed capacity, managing and rec
2729
- Efficient in memory usage and it even achieves higher performance than unlimited goroutines in Golang
2830
- Nonblocking mechanism
2931

30-
## Tested in the following Golang versions:
32+
## ⚔️ Tested in the following Golang versions:
3133

3234
- 1.8.x
3335
- 1.9.x
@@ -36,13 +38,13 @@ Library `ants` implements a goroutine pool with fixed capacity, managing and rec
3638
- 1.12.x
3739
- 1.13.x
3840

39-
## `ants` works as the flowing flowchart
41+
## 💡 `ants` works as the flowing flowchart
4042

4143
<p align="center">
4244
<img width="1011" alt="ants-flowchart-en" src="https://user-images.githubusercontent.com/7496278/66396509-7b42e700-ea0c-11e9-8612-b71a4b734683.png">
4345
</p>
4446

45-
## How to install
47+
## 🧰 How to install
4648

4749
### For `ants` v1
4850

@@ -56,7 +58,7 @@ go get -u github.com/panjf2000/ants
5658
go get -u github.com/panjf2000/ants/v2
5759
```
5860

59-
## How to use
61+
## 🛠 How to use
6062
Just take a imagination that your program starts a massive number of goroutines, resulting in a huge consumption of memory. To mitigate that kind of situation, all you need to do is to import `ants` package and submit all your tasks to a default pool with fixed capacity, activated when package `ants` is imported:
6163

6264
``` go
@@ -121,7 +123,7 @@ func main() {
121123
}
122124
```
123125

124-
## Integrate with http server
126+
### Integrate with http server
125127
```go
126128
package main
127129

@@ -176,7 +178,7 @@ func main() {
176178
}
177179
```
178180

179-
## Functional options for ants pool
181+
### Functional options for ants pool
180182

181183
```go
182184
// Option represents the optional function.
@@ -249,7 +251,7 @@ func WithPanicHandler(panicHandler func(interface{})) Option {
249251

250252
`ants.Options`contains all optional configurations of ants pool, which allows you to customize the goroutine pool by invoking option functions to set up each configuration in `NewPool`/`NewPoolWithFunc`method.
251253

252-
## Customize limited pool
254+
### Customize limited pool
253255

254256
`ants` also supports customizing the capacity of pool. You can invoke the `NewPool` method to instantiate a pool with a given capacity, as following:
255257

@@ -258,13 +260,13 @@ func WithPanicHandler(panicHandler func(interface{})) Option {
258260
p, _ := ants.NewPool(10000)
259261
```
260262

261-
## Submit tasks
263+
### Submit tasks
262264
Tasks can be submitted by calling `ants.Submit(func())`
263265
```go
264266
ants.Submit(func(){})
265267
```
266268

267-
## Tune pool capacity in runtime
269+
### Tune pool capacity in runtime
268270
You can tune the capacity of `ants` pool in runtime with `Tune(int)`:
269271

270272
``` go
@@ -274,7 +276,7 @@ pool.Tune(100000) // Tune its capacity to 100000
274276

275277
Don't worry about the synchronous problems in this case, the method here is thread-safe (or should be called goroutine-safe).
276278

277-
## Pre-malloc goroutine queue in pool
279+
### Pre-malloc goroutine queue in pool
278280

279281
`ants` allows you to pre-allocate memory of goroutine queue in pool, which may get a performance enhancement under some special certain circumstances such as the scenario that requires a pool with ultra-large capacity, meanwhile each task in goroutine lasts for a long time, in this case, pre-mallocing will reduce a lot of costs when re-slicing goroutine queue.
280282

@@ -283,16 +285,16 @@ Don't worry about the synchronous problems in this case, the method here is thre
283285
p, _ := ants.NewPool(100000, ants.WithPreAlloc(true))
284286
```
285287

286-
## Release Pool
288+
### Release Pool
287289

288290
```go
289291
pool.Release()
290292
```
291293

292-
## About sequence
294+
## ⚙️ About sequence
293295
All tasks submitted to `ants` pool will not be guaranteed to be addressed in order, because those tasks scatter among a series of concurrent workers, thus those tasks would be executed concurrently.
294296

295-
## Benchmarks
297+
## 🧲 Benchmarks
296298

297299
<div align="center"><img src="https://user-images.githubusercontent.com/7496278/51515466-c7ce9e00-1e4e-11e9-89c4-bd3785b3c667.png"/></div>
298300
In this benchmark result, the first and second benchmarks performed test cases with 1M tasks and the rest of benchmarks performed test cases with 10M tasks, both in unlimited goroutines and `ants` pool, and the capacity of this `ants` goroutine-pool was limited to 50K.
@@ -327,20 +329,20 @@ In above benchmark result, the first and second benchmarks performed test cases
327329

328330
![](https://user-images.githubusercontent.com/7496278/52987732-537c2000-3437-11e9-86a6-177f00d7a1d6.png)
329331

330-
### Performance Summary
332+
## 📊 Performance Summary
331333

332334
![](https://user-images.githubusercontent.com/7496278/63449727-3ae6d400-c473-11e9-81e3-8b3280d8288a.gif)
333335

334336
**In conclusion, `ants` performs 2~6 times faster than goroutines without a pool and the memory consumption is reduced by 10 to 20 times.**
335337

336-
# License
338+
## 📄 License
337339

338340
Source code in `gnet` is available under the MIT [License](/LICENSE).
339341

340-
# Relevant Articles
342+
## 📚 Relevant Articles
341343

342344
- [Goroutine 并发调度模型深度解析之手撸一个高性能协程池](https://taohuawu.club/high-performance-implementation-of-goroutine-pool)
343345

344-
# Users of ants (please feel free to add your projects here ~~)
346+
## 👨‍👨‍👧‍👦 Users of ants (please feel free to add your projects here ~~)
345347

346348
[![](https://raw.githubusercontent.com/panjf2000/logos/master/gnet/logo.png)](https://github.com/panjf2000/gnet)

README_ZH.md

+21-21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# ants
1+
<h1 align='center'>ants</h1>
22
<p align="center">
33
<img src="https://raw.githubusercontent.com/panjf2000/logos/master/ants/logo.png"/>
44
<b>A goroutine pool for Go</b>
@@ -14,11 +14,13 @@
1414
<a title="Mentioned in Awesome Go" target="_blank" href="https://github.com/avelino/awesome-go"><img src="https://awesome.re/mentioned-badge-flat.svg"></a>
1515
</p>
1616

17-
# [[英文](README.md)]
17+
[英文](README.md) | 🇨🇳中文
18+
19+
## 📖 简介
1820

1921
`ants`是一个高性能的协程池,实现了对大规模 goroutine 的调度管理、goroutine 复用,允许使用者在开发并发程序的时候限制协程数量,复用资源,达到更高效执行任务的效果。
2022

21-
## 功能:
23+
## 🚀 功能:
2224

2325
- 自动调度海量的 goroutines,复用 goroutines
2426
- 定时清理过期的 goroutines,进一步节省资源
@@ -27,7 +29,7 @@
2729
- 资源复用,极大节省内存使用量;在大规模批量并发任务场景下比原生 goroutine 并发具有更高的性能
2830
- 非阻塞机制
2931

30-
## 目前测试通过的Golang版本:
32+
## ⚔️ 目前测试通过的Golang版本:
3133

3234
- 1.8.x
3335
- 1.9.x
@@ -36,13 +38,13 @@
3638
- 1.12.x
3739
- 1.13.x
3840

39-
## `ants` 运行时的流程图如下
41+
## 💡 `ants` 运行时的流程图如下
4042

4143
<p align="center">
4244
<img width="845" alt="ants-flowchart-cn" src="https://user-images.githubusercontent.com/7496278/66396519-7ed66e00-ea0c-11e9-9c1a-5ca54bbd61eb.png">
4345
</p>
4446

45-
## 安装
47+
## 🧰 安装
4648

4749
### 使用 `ants` v1 版本:
4850

@@ -56,7 +58,7 @@ go get -u github.com/panjf2000/ants
5658
go get -u github.com/panjf2000/ants/v2
5759
```
5860

59-
## 使用
61+
## 🛠 使用
6062
写 go 并发程序的时候如果程序会启动大量的 goroutine ,势必会消耗大量的系统资源(内存,CPU),通过使用 `ants`,可以实例化一个协程池,复用 goroutine ,节省资源,提升性能:
6163

6264
``` go
@@ -121,7 +123,7 @@ func main() {
121123
}
122124
```
123125

124-
## 与 http server 集成
126+
### 与 http server 集成
125127
```go
126128
package main
127129

@@ -176,7 +178,7 @@ func main() {
176178
}
177179
```
178180

179-
## Pool 配置
181+
### Pool 配置
180182

181183
```go
182184
// Option represents the optional function.
@@ -250,22 +252,22 @@ func WithPanicHandler(panicHandler func(interface{})) Option {
250252
通过在调用`NewPool`/`NewPoolWithFunc`之时使用各种 optional function,可以设置`ants.Options`中各个配置项的值,然后用它来定制化 goroutine pool.
251253

252254

253-
## 自定义池
255+
### 自定义池
254256
`ants`支持实例化使用者自己的一个 Pool ,指定具体的池容量;通过调用 `NewPool` 方法可以实例化一个新的带有指定容量的 Pool ,如下:
255257

256258
``` go
257259
// Set 10000 the size of goroutine pool
258260
p, _ := ants.NewPool(10000)
259261
```
260262

261-
## 任务提交
263+
### 任务提交
262264

263265
提交任务通过调用 `ants.Submit(func())`方法:
264266
```go
265267
ants.Submit(func(){})
266268
```
267269

268-
## 动态调整协程池容量
270+
### 动态调整协程池容量
269271
需要动态调整协程池容量可以通过调用`Tune(int)`
270272

271273
``` go
@@ -275,7 +277,7 @@ pool.Tune(100000) // Tune its capacity to 100000
275277

276278
该方法是线程安全的。
277279

278-
## 预先分配 goroutine 队列内存
280+
### 预先分配 goroutine 队列内存
279281

280282
`ants`允许你预先把整个池的容量分配内存, 这个功能可以在某些特定的场景下提高协程池的性能。比如, 有一个场景需要一个超大容量的池,而且每个 goroutine 里面的任务都是耗时任务,这种情况下,预先分配 goroutine 队列内存将会减少 re-slice 时的复制内存损耗。
281283

@@ -284,15 +286,13 @@ pool.Tune(100000) // Tune its capacity to 100000
284286
p, _ := ants.NewPool(100000, ants.WithPreAlloc(true))
285287
```
286288

287-
288-
289-
## 销毁协程池
289+
### 销毁协程池
290290

291291
```go
292292
pool.Release()
293293
```
294294

295-
## Benchmarks
295+
## 🧲 Benchmarks
296296

297297
<div align="center"><img src="https://user-images.githubusercontent.com/7496278/51515466-c7ce9e00-1e4e-11e9-89c4-bd3785b3c667.png"/></div>
298298
上图中的前两个 benchmark 测试结果是基于100w 任务量的条件,剩下的几个是基于 1000w 任务量的测试结果,`ants` 的默认池容量是 5w。
@@ -327,20 +327,20 @@ pool.Release()
327327

328328
![](https://user-images.githubusercontent.com/7496278/52987732-537c2000-3437-11e9-86a6-177f00d7a1d6.png)
329329

330-
### 性能小结
330+
## 📊 性能小结
331331

332332
![](https://user-images.githubusercontent.com/7496278/63449727-3ae6d400-c473-11e9-81e3-8b3280d8288a.gif)
333333

334334
**从该 demo 测试吞吐性能对比可以看出,使用`ants`的吞吐性能相较于原生 goroutine 可以保持在 2-6 倍的性能压制,而内存消耗则可以达到 10-20 倍的节省优势。**
335335

336-
# 证书
336+
## 📄 证书
337337

338338
`gnet` 的源码允许用户在遵循 MIT [开源证书](/LICENSE) 规则的前提下使用。
339339

340-
# 相关文章
340+
## 📚 相关文章
341341

342342
- [Goroutine 并发调度模型深度解析之手撸一个高性能协程池](https://taohuawu.club/high-performance-implementation-of-goroutine-pool)
343343

344-
# 谁在使用 ants(欢迎补充 ~~
344+
## 👨‍👨‍👧‍👦 谁在使用 ants(欢迎补充 ~~
345345

346346
[![](https://raw.githubusercontent.com/panjf2000/logos/master/gnet/logo.png)](https://github.com/panjf2000/gnet)

doc.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright 2019 Andy Pan. All rights reserved.
2+
// Use of this source code is governed by an MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
/*
6+
Library ants implements a goroutine pool with fixed capacity, managing and recycling a massive number of goroutines,
7+
allowing developers to limit the number of goroutines in your concurrent programs.
8+
*/
9+
10+
package ants

0 commit comments

Comments
 (0)