I am aware that this is not the proper use of a context package, but I am using gopher-lua and I am not able to find better solution.
I am calling a function with a context parameter (lua.NewState with the context set) and I am properly canceling the context.
ctx, cancel := context.WithDeadline(context.Background(), deadline)
defer cancel()
Foo(ctx, parameters)
But somewhere inside the function Foo in another subfunction Poo I need to cut the deadline a little bit. I am using:
func Poo(l *lua.LState, timeout time.Duration) {
ctx, cancel := context.WithTimeout(l.Context(), timeout)
l.SetContext(ctx)
}
However, I am not sure if I have to call the cancel function from function Poo to avoid a memory leak. In current code I am discarding the cancel, because the parent cancel is always called and propagated downwards. Go vet complains about discarding the cancel, but it probably misses the point.
Is my idea good enough or should I find better solution?