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

refactor: eventPool get atomicity #230

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

lyp256
Copy link
Contributor

@lyp256 lyp256 commented May 31, 2022

Proposed Changes:

当前 eventPool 实现在并发调用 GetN 时会导致死锁问题见 #228

重构了 eventPool 实现主要变更:

  • 解决并发调用 GetN 导致死锁的问题
  • eventPool 所有 Get API 支持 context.Context 支持由调用方终止阻塞等待

Copy link
Collaborator

@mmaxiaolei mmaxiaolei left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

整体看实现有点复杂。

把event直接用chan作为存储,然后getN()用lock+for select eventChan是不是会简洁一点?类似以下伪代码:

func (p *Pool) GetNContext(ctx context.Context, n int) ([]api.Event, bool) {
	p.lock.Lock()
	defer p.lock.Unlock()

	events := make([]api.Event, 0, n)
	for {
		select {
		case e := <-p.events:
			events = append(events, e)
			if len(events) == n {
				return events, true
			}
		case <-ctx.Done():
			return nil, false
		}
	}
}

}
for el := p.requestQueue.Front(); el != nil; el = el.Next() {
notify := el.Value.(*request)
if p.free >= notify.N {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有足够的数量了,直接close(notifty.C)就可以了吧

@lyp256
Copy link
Contributor Author

lyp256 commented Jul 1, 2022

@lyp256 lyp256 closed this Jul 1, 2022
@lyp256 lyp256 reopened this Jul 1, 2022
@lyp256
Copy link
Contributor Author

lyp256 commented Jul 1, 2022

整体看实现有点复杂。

把event直接用chan作为存储,然后getN()用lock+for select eventChan是不是会简洁一点?类似以下伪代码:

func (p *Pool) GetNContext(ctx context.Context, n int) ([]api.Event, bool) {
	p.lock.Lock()
	defer p.lock.Unlock()

	events := make([]api.Event, 0, n)
	for {
		select {
		case e := <-p.events:
			events = append(events, e)
			if len(events) == n {
				return events, true
			}
		case <-ctx.Done():
			return nil, false
		}
	}
}

这样实现的话,并发时后续 GetNContext 会被阻塞在 p.lock.Lock() 无法相应 context.Done()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants