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

增加上下文时限管理能力 #54

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from

Conversation

dino-ma
Copy link

@dino-ma dino-ma commented Jun 28, 2021

在工程场景中我们会遇到,当前上下文有超时时间、一些场景后(RPC Response)释放资源,但是我们还要继续去 执行一些任务。如果我们用到了协程池,我们可能需要传递没有超时、取消的上下文进去。�(上下文中会携带TraceID等信息用来溯源)

Copy link
Collaborator

@PureWhiteWu PureWhiteWu left a 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 的哲学~
另外还有一些小的改动,可以完善一下~

@@ -0,0 +1,15 @@
# hcontext
Copy link
Collaborator

Choose a reason for hiding this comment

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

感觉像是对标准库 context 的增强,不如放在 lang 下,取名为 contextx?

Copy link
Author

Choose a reason for hiding this comment

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

done

## usage

```golang
//移除超时信息,保留value信息
Copy link
Collaborator

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 信息

下同。

func (valueOnlyContext) Done() <-chan struct{} { return nil }
func (valueOnlyContext) Err() error { return nil }

//WithNoDeadline 移除超时控制,保留value信息
Copy link
Collaborator

Choose a reason for hiding this comment

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

comment 改成英文吧,记得 // 后加空格,下同。

"testing"
"time"

"gopkg.in/go-playground/assert.v1"
Copy link
Collaborator

Choose a reason for hiding this comment

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

用 testify 吧,不引入新的依赖了

stub func(*testing.T, context.Context)
}{
{
name: "value正常传递",
Copy link
Collaborator

Choose a reason for hiding this comment

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

改为英文吧,下同

Copy link
Collaborator

@PureWhiteWu PureWhiteWu left a comment

Choose a reason for hiding this comment

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

@zhangyunhao116 有时间瞅一眼不?

func (valueOnlyContext) Done() <-chan struct{} { return nil }
func (valueOnlyContext) Err() error { return nil }

//WithNoDeadline only remove deadline value
Copy link
Collaborator

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}
}

//WithNoCancel only remove cancel value
Copy link
Collaborator

Choose a reason for hiding this comment

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

同上

Copy link
Member

@zhangyunhao116 zhangyunhao116 left a comment

Choose a reason for hiding this comment

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

感觉 API 和功能的必要性我们可以再讨论下,因为兼容原则,这些以后可能不太好修改

}

//WithNoCancel only remove cancel value
func WithNoCancel(ctx context.Context) context.Context {
Copy link
Member

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
Copy link
Member

Choose a reason for hiding this comment

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

这里是为了避免 linter 报错么


import (
"context"
"github.com/stretchr/testify/assert"
Copy link
Member

Choose a reason for hiding this comment

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

可以使用 goimports 排序下

func (valueOnlyContext) Err() error { return nil }

//WithNoDeadline only remove deadline value
func WithNoDeadline(ctx context.Context) context.Context {
Copy link
Member

Choose a reason for hiding this comment

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

这里的注释是不是可以更详细点呢,描述下使用的情况,以免误用

@PureWhiteWu
Copy link
Collaborator

感觉 API 和功能的必要性我们可以再讨论下,因为兼容原则,这些以后可能不太好修改

从功能上来说感觉问题不大。

@dino-ma
Copy link
Author

dino-ma commented Jun 29, 2021

感觉 API 和功能的必要性我们可以再讨论下,因为兼容原则,这些以后可能不太好修改

从功能上来说感觉问题不大。

那微信讨论一下?9393103

@zhangyunhao116
Copy link
Member

好像例如注释这些还没解决,要不再更新下?我们的讨论过程还是尽量在 issues 里面吧,方便其他开发者找到相关的上下文

func (valueOnlyContext) Done() <-chan struct{} { return nil }
func (valueOnlyContext) Err() error { return nil }

//RemoveDeadline only remove deadline value
Copy link
Collaborator

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
Copy link
Collaborator

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)
Copy link
Collaborator

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) 是不是就行了?

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.

3 participants