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

Fix a bug that doesn't release lock #79

Merged
merged 2 commits into from
Feb 26, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ func (p *Pool) revertWorker(worker *goWorker) bool {
}
worker.recycleTime = time.Now()
p.lock.Lock()
defer p.lock.Unlock()
Copy link
Owner

Choose a reason for hiding this comment

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

在这种性能极度敏感的库代码里尽量不要用 defer,它在汇编层面会有很多连锁调用导致性能损耗。直接在下面的 if err != nil {} 里面加一个解锁的步骤就行了。

Copy link
Contributor

Choose a reason for hiding this comment

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

defer 在 1.14 基本已经没啥损耗了。另外即使是在老版本,大概率池子里运行的函数本身远重于 defer。这个池子本身的性能瓶颈也不会在这里。反倒是因为不用 defer,这已经是第二次碰到异常路径上没 unlock 的问题了。

Copy link
Owner

Choose a reason for hiding this comment

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

能看出具体是哪一步没 unlock 吗?


err := p.workers.insert(worker)
if err != nil {
Expand All @@ -255,6 +256,5 @@ func (p *Pool) revertWorker(worker *goWorker) bool {

// Notify the invoker stuck in 'retrieveWorker()' of there is an available worker in the worker queue.
p.cond.Signal()
p.lock.Unlock()
return true
}