-
Notifications
You must be signed in to change notification settings - Fork 42
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
reduce unnecessary context.Background() #384
Conversation
c306c8a
to
d305750
Compare
utils/gopool.go
Outdated
// there won't be error once we use background ctx | ||
p.sem.Acquire(context.Background(), 1) // nolint:errcheck | ||
p.sem.Acquire(ctx, 1) // nolint:errcheck |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里用了非 background ctx 就要处理 error 了... 这个函数的类型就开始变得不美好了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
当时使用 background 的考虑是, 如果只是考虑这是 sync.WaitGroup 的强化版本的话, wg 本身也没有提供被 ctx 打断的能力, 所以我就偷懒做了一个等价语义.....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Acquire acquires the semaphore with a weight of n, blocking until resources are available or ctx is done. On success, returns nil. On failure, returns ctx.Err() and leaves the semaphore unchanged.
If ctx is already done, Acquire may still succeed without blocking.
根据这个描述,其实你不处理 error 也不是不行
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
笑哭
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
所以实际上你看 ctx 导致失败的话,semaphore 不会变化,ctx 错误忽略就行了,还能有啥错误呢对吧……
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
哦实际还是不行,得阻断后面的执行……
d305750
to
7b2d437
Compare
7b2d437
to
22411b7
Compare
seems redis lock should return wrapped context like etcd lock @tonicbupt