From cb1818ef9d889f5824b5e82398cd64d31f9be088 Mon Sep 17 00:00:00 2001 From: Orkun Karaduman Date: Fri, 4 Mar 2022 20:05:56 +0300 Subject: [PATCH] implemented NewTerminateContext --- terminatecontext.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/terminatecontext.go b/terminatecontext.go index 107867d..41dbfd5 100644 --- a/terminatecontext.go +++ b/terminatecontext.go @@ -15,13 +15,22 @@ type terminateContext struct { context.CancelFunc } +// Terminate calls cancel function of the underlying context. func (c *terminateContext) Terminate() { c.CancelFunc() } -// WithTerminate creates a new TerminateContext. +// NewTerminateContext returns the underlying context as TerminateContext. +// The code should call Terminate method or cancel function to release resources associated with it. +func NewTerminateContext(ctx context.Context, cancel context.CancelFunc) TerminateContext { + result := new(terminateContext) + result.Context = ctx + result.CancelFunc = cancel + return result +} + +// WithTerminate creates a new cancel context as TerminateContext. +// The code should call Terminate method to release resources associated with it, as cancel function. func WithTerminate(parent context.Context) TerminateContext { - ctx := new(terminateContext) - ctx.Context, ctx.CancelFunc = context.WithCancel(parent) - return ctx + return NewTerminateContext(context.WithCancel(parent)) }