From b0497293ed9b66aca1857bddd7c2c596c7a4fa73 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Wed, 12 Jul 2023 17:12:32 -0700 Subject: [PATCH] promising: AssertContextInTask This is just a helper for functions that require that they run in a task context, to panic if not. --- internal/promising/task.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/internal/promising/task.go b/internal/promising/task.go index e9eb6ae449..3abaf7ea96 100644 --- a/internal/promising/task.go +++ b/internal/promising/task.go @@ -106,6 +106,19 @@ func AsyncTask[P PromiseContainer](ctx context.Context, promises P, impl func(ct }() } +// AssertContextInTask panics if the given context does not belong to a +// promising task, or does nothing at all if it does. +// +// This is here just as a helper for clearly marking functions that only +// make sense to call when in a task context, typically because they are +// going to rely on promises. +func AssertContextInTask(ctx context.Context) { + _, ok := ctx.Value(taskContextKey).(*task) + if !ok { + panic("function requires an active task, but the given context does not belong to one") + } +} + func mustTaskFromContext(ctx context.Context) *task { ret, ok := ctx.Value(taskContextKey).(*task) if !ok {