Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

非阻塞在PoolOverload时进入等待队列 #56

Closed
HaoKunT opened this issue Oct 28, 2019 · 6 comments
Closed

非阻塞在PoolOverload时进入等待队列 #56

HaoKunT opened this issue Oct 28, 2019 · 6 comments
Assignees
Labels
help wanted Extra attention is needed proposal Proposal for this repo waiting for response waiting for the response from commenter

Comments

@HaoKunT
Copy link

HaoKunT commented Oct 28, 2019

Is your feature request related to a problem? Please describe.
submit的函数如果是递归函数,则如果是阻塞模式,在递归深度较大的时候,会因为worker用光,程序直接被阻塞了,如果调整为非阻塞模式,则会在worker用光之后,后面的任务直接返回ErrPoolOverload而被舍弃。我是在遍历目录的时候遇到的这个问题。

Describe the solution you'd like
增加一个配置项,非阻塞模式下,可以选择增加一个等待队列,而不是舍弃。

Describe alternatives you've considered
增加一个channel,用于task完成后通知等待队列

@HaoKunT HaoKunT added the proposal Proposal for this repo label Oct 28, 2019
@panjf2000
Copy link
Owner

panjf2000 commented Oct 28, 2019

为什么递归会用完 worker ?你是每一层递归都会调用 pool.Submit

@HaoKunT
Copy link
Author

HaoKunT commented Oct 28, 2019

对,就是我遍历目录的时候,每一个子目录就再pool.Submit,就像这样子

               if !cextinfo.IsDir() {
			// ...
		} else {
			wg.Add(1)
			err = p.Submit(func() {
				buildTree(&cextinfo, errs, wg, p)
				wg.Done()
			})
			if err != nil {
				errs <- err
		}

@panjf2000

@panjf2000
Copy link
Owner

panjf2000 commented Oct 28, 2019

你这个需求和 #55 大同小异,都是想把业务特性加到 ants 里,不太妥当。是不是你的 pool size 设置得太小了?设置个几十万难道还不能满足?遍历的目录是有多大?

再说了,这个需求也不合理,加入一个等待队列,这个队列长度又要怎么设置,超过了长度要怎么办?阻塞?丢弃?这不又和现在的情况一样了吗?

如果是长度无限制的话,那你完全可以直接使用 ants 的默认 pool,它的 size 是 math.MaxInt32,我相信足够大了,个人觉得加一个等待队列没什么意义了,因为你完全可以把 pool size 设置得足够大。

@panjf2000 panjf2000 added help wanted Extra attention is needed waiting for response waiting for the response from commenter labels Oct 28, 2019
@HaoKunT
Copy link
Author

HaoKunT commented Oct 28, 2019

你说的有道理,但是如果长度无限制的话,那和自带的go关键字似乎没有区别?实际上我使用ants的原因就是在遍历很大的目录的时候,会出现内存占用过多的情况,因此希望能复用go程,那个ExpiryDuration配置项具体起到什么作用啊?是不是这个会隔一段时间就清理一次?是什么原理?

@panjf2000
Copy link
Owner

panjf2000 commented Oct 28, 2019

但是如果长度无限制的话,那和自带的go关键字似乎没有区别?实际上我使用ants的原因就是在遍历很大的目录的时候,会出现内存占用过多的情况,因此希望能复用go程

有区别啊,ants 虽然长度无限,但是会有复用,一旦 worker 完成任务就可以给下一个任务用,所以虽然你设置的是很大的 size,但是实际上整个流程跑完,最终创建的 go 程的数量可能是很少的,你可以试试。

那个ExpiryDuration配置项具体起到什么作用啊?是不是这个会隔一段时间就清理一次?是什么原理?

ExpiryDuration 就是一个定时的周期,就表示每隔 ExpiryDuration 时长执行一次清理,清理掉那些放置在队列里超过 ExpiryDuration 时长没有被使用过的 worker,以此类推,这个特性同样是为了节省资源。

@HaoKunT
Copy link
Author

HaoKunT commented Oct 28, 2019

OK懂了,谢谢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed proposal Proposal for this repo waiting for response waiting for the response from commenter
Projects
None yet
Development

No branches or pull requests

2 participants