-
Notifications
You must be signed in to change notification settings - Fork 225
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
增加上下文时限管理能力 #54
base: develop
Are you sure you want to change the base?
增加上下文时限管理能力 #54
Conversation
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.
首先,非常感谢你的贡献!
我感觉不太适合直接在 gopool 中提供这么个额外的方法,因为这个需求相对比较小众,而且大多数人看到这个新方法会比较懵,不知道该用哪个,不如就提供一个 CtxGo,通过传入的 ctx 提前用 hcontext(contextx)包装一次来控制。这样设计比较符合 Go 的哲学~
另外还有一些小的改动,可以完善一下~
util/hcontext/README.md
Outdated
@@ -0,0 +1,15 @@ | |||
# hcontext |
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.
感觉像是对标准库 context 的增强,不如放在 lang 下,取名为 contextx?
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.
done
util/hcontext/README.md
Outdated
## usage | ||
|
||
```golang | ||
//移除超时信息,保留value信息 |
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.
在 // 后加一个空格,并且 README.md 改为英文吧,中文的可以放在 README_ZH.md 中,同时记得英文和中文间加个空格,如下:
// 移除超时信息,保留 value 信息
下同。
util/hcontext/hcontext.go
Outdated
func (valueOnlyContext) Done() <-chan struct{} { return nil } | ||
func (valueOnlyContext) Err() error { return nil } | ||
|
||
//WithNoDeadline 移除超时控制,保留value信息 |
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.
comment 改成英文吧,记得 // 后加空格,下同。
util/hcontext/hcontext_test.go
Outdated
"testing" | ||
"time" | ||
|
||
"gopkg.in/go-playground/assert.v1" |
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.
用 testify 吧,不引入新的依赖了
util/hcontext/hcontext_test.go
Outdated
stub func(*testing.T, context.Context) | ||
}{ | ||
{ | ||
name: "value正常传递", |
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.
@zhangyunhao116 有时间瞅一眼不?
lang/contextx/contextx.go
Outdated
func (valueOnlyContext) Done() <-chan struct{} { return nil } | ||
func (valueOnlyContext) Err() error { return nil } | ||
|
||
//WithNoDeadline only remove deadline value |
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.
// WithNoDeadline only remove deadline value
在 // 后加个空格吧
lang/contextx/contextx.go
Outdated
return valueOnlyContext{ctx} | ||
} | ||
|
||
//WithNoCancel only remove cancel value |
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.
感觉 API 和功能的必要性我们可以再讨论下,因为兼容原则,这些以后可能不太好修改
lang/contextx/contextx.go
Outdated
} | ||
|
||
//WithNoCancel only remove cancel value | ||
func WithNoCancel(ctx context.Context) context.Context { |
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.
WithNoCancel
是不是换成 WithoutCancel
或者 RemoveCancel
这样会好点?好像没有WithNo
这样的 name convention
var cancel context.CancelFunc | ||
ctx = WithNoDeadline(ctx) | ||
ctx, cancel = context.WithDeadline(ctx, deadline) | ||
_ = cancel |
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.
这里是为了避免 linter 报错么
lang/contextx/contextx_test.go
Outdated
|
||
import ( | ||
"context" | ||
"github.com/stretchr/testify/assert" |
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.
可以使用 goimports 排序下
lang/contextx/contextx.go
Outdated
func (valueOnlyContext) Err() error { return nil } | ||
|
||
//WithNoDeadline only remove deadline value | ||
func WithNoDeadline(ctx context.Context) context.Context { |
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.
这里的注释是不是可以更详细点呢,描述下使用的情况,以免误用
从功能上来说感觉问题不大。 |
那微信讨论一下?9393103 |
好像例如注释这些还没解决,要不再更新下?我们的讨论过程还是尽量在 issues 里面吧,方便其他开发者找到相关的上下文 |
func (valueOnlyContext) Done() <-chan struct{} { return nil } | ||
func (valueOnlyContext) Err() error { return nil } | ||
|
||
//RemoveDeadline only remove deadline value |
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.
// WithNoDeadline only remove deadline value
在 // 后加个空格吧
return valueOnlyContext{ctx} | ||
} | ||
|
||
//RemoveCancel only remove cancel value |
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.
同上
if ok { | ||
var cancel context.CancelFunc | ||
ctx = RemoveDeadline(ctx) | ||
ctx, cancel = context.WithDeadline(ctx, deadline) |
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, _ = context.WithDeadline(ctx, deadline) 是不是就行了?
在工程场景中我们会遇到,当前上下文有超时时间、一些场景后(RPC Response)释放资源,但是我们还要继续去 执行一些任务。如果我们用到了协程池,我们可能需要传递没有超时、取消的上下文进去。�(上下文中会携带TraceID等信息用来溯源)