From d98d6dffef50c6def88d3541abac4771403de09e Mon Sep 17 00:00:00 2001 From: Ge Song Date: Wed, 29 Mar 2023 18:11:01 +0800 Subject: [PATCH] add contexthelper; --- contexthelper/go.mod | 3 +++ contexthelper/nocancel.go | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 contexthelper/go.mod create mode 100644 contexthelper/nocancel.go diff --git a/contexthelper/go.mod b/contexthelper/go.mod new file mode 100644 index 0000000..f1d7824 --- /dev/null +++ b/contexthelper/go.mod @@ -0,0 +1,3 @@ +module git.blauwelle.com/go/crate/contexthelper + +go 1.20 diff --git a/contexthelper/nocancel.go b/contexthelper/nocancel.go new file mode 100644 index 0000000..ea35f79 --- /dev/null +++ b/contexthelper/nocancel.go @@ -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 +}