add contexthelper;

This commit is contained in:
2023-03-29 18:11:01 +08:00
parent 2582171d99
commit d98d6dffef
2 changed files with 20 additions and 0 deletions

3
contexthelper/go.mod Normal file
View File

@@ -0,0 +1,3 @@
module git.blauwelle.com/go/crate/contexthelper
go 1.20

17
contexthelper/nocancel.go Normal file
View File

@@ -0,0 +1,17 @@
package contexthelper
import "context"
// WithNoCancel 阻断上级 ctx 的中断信号
// 想要使用上级 ctx 中的值但不想受上级 ctx 控制退出时可以使用
func WithNoCancel(ctx context.Context) context.Context {
return &noCancelCtx{Context: ctx}
}
type noCancelCtx struct {
context.Context
}
func (ctx *noCancelCtx) Done() <-chan struct{} {
return nil
}