-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[sdk#1026] Add closectx #1035
[sdk#1026] Add closectx #1035
Conversation
0b182de
to
cfcfbe4
Compare
pkg/tools/closectx/context.go
Outdated
"github.com/networkservicemesh/sdk/pkg/tools/extend" | ||
) | ||
|
||
const closeTimeout = 15 * time.Second |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't put magic timer constants in the system.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So here comes a problem, what should we use as a timeout in such case? Or should we just make Close
on a context without any deadline?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See below... derive the context from the previous duration of the Request... not from an arbitrary timer value.
Over reliance on (and tuning of) timer values leads to things like: networkservicemesh/cmd-admission-webhook-k8s#18 where we are now seeking to propagate the ability to over tune those timers further and further.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed with using timeout taken before the Request
.
pkg/tools/closectx/context.go
Outdated
timeout = closeTimeout | ||
} | ||
|
||
ctx = extend.WithValuesFromContext(context.Background(), ctx) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't generally know if extending values is correct or incorrect here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method provides the same context with postponed deadline and disabled cancels from backward. So I suppose that extending values is correct here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we need to extend values... we can do that easily inline when using this as well... this is orthogonal to the issue of extending the timer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created 2 helper methods: Context
and ContextWithValues
.
pkg/tools/closectx/context.go
Outdated
if deadline, ok := ctx.Deadline(); ok { | ||
timeout = clockTime.Until(deadline) | ||
} | ||
if timeout < closeTimeout { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This context is going to expire, if anything, faster the the initial context from which its taken... its not going to be appropriate for 'Have to call Close at some later point' ... the right answer here is closer to:
func FromContext(ctx context.Context) func() context.Context {
ctxFunc := func() context.Context { return context.Background() }
if deadline, ok := ctx.Deadline(); ok {
duration := time.Until(deadline)
ctxFunc = func() context.Context {
ctx, _ := context.WithTimeout(context.Background(), duration)
return ctx
}
}
return ctxFunc
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We increase timeout
up to closeTimeout
if it is less than that. So it does mean that new context is going to expire in the same time or slower than the initial context.
So using here pure ctx.Deadline()
just leads us to the same old issue that should be fixed by closectx
:
- Request context expires (or is very close to be expired).
- Error happens.
- Chain element calls Close on the expired (almost expired) context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not if you handle it like this:
func (c *client) Request(ctx context.Context,request *networkservice.Request,opts...grpc.CallOptions) {
ctxFunc := ctxfunc.FromContext(ctx)
conn, err := next.Client(ctx).Request(ctx,request,opts...)
...
err = doSomething(...)
if err != nil {
_ = c.Close(extend.WithValues(ctxFunc(),ctx),conn)
return nil, err
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You capture the time at the beginning of the Request, and utilize it at the end of the Request for cleanup... all time spent calling down chain and doing something is recovered.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, done this in postpone.Context()
.
pkg/tools/closectx/context.go
Outdated
// limitations under the License. | ||
|
||
// Package closectx provides helper method to create Close/Unregister context in case of Request/Register failure | ||
package closectx |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we name this something less bound to Close and more reflecting the fact that we are creating a mechanism generally appropriate to allow us to reasonably do things like Close after ourselves, and make other sorts of calls that start at some point in the future...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably we can name it something like postponectx
? And so description would be:
// Package postponectx provides helper method to postpone context deadline.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is moving in the right direction :)
As a side note: I don't like the current name I'm using (ctxFunc) all that much either... so please don't read to much into it :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps in the vein of postponectx
we could have package postpone
and the call be something like postpone.Context(ctx)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
@Bolodya1997 I don't quite follow how this PR relates with #1035. |
I don't quite follow your question - here is an issue related to this PR #1026 and so it is a motivation. |
Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>
Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>
Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>
Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>
9bb2925
to
d17ec3a
Compare
@Bolodya1997 Sorry, I meant motivation of these changes. I'm intrested in why is this the best solution for the issue. Moreover I'm intrested on how is it related to #1035, as I see you pointed it a few days ago. @edwarnicke Do you have any comments on this? |
…k@main PR link: networkservicemesh/sdk#1035 Commit: a91779d Author: Vladimir Popov Date: 2021-08-17 20:25:36 +0700 Message: - [sdk#1026] Add closectx (#1035) * Add closectx Signed-off-by: Vladimir Popov <vladimir.popov@xored.com> * Add unit tests for Close on canceled context Signed-off-by: Vladimir Popov <vladimir.popov@xored.com> * Rework closectx to use context.Background() Signed-off-by: Vladimir Popov <vladimir.popov@xored.com> * Replace closectx.New() with postpone.ContextWithValues() Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
…k@main PR link: networkservicemesh/sdk#1035 Commit: a91779d Author: Vladimir Popov Date: 2021-08-17 20:25:36 +0700 Message: - [sdk#1026] Add closectx (#1035) * Add closectx Signed-off-by: Vladimir Popov <vladimir.popov@xored.com> * Add unit tests for Close on canceled context Signed-off-by: Vladimir Popov <vladimir.popov@xored.com> * Rework closectx to use context.Background() Signed-off-by: Vladimir Popov <vladimir.popov@xored.com> * Replace closectx.New() with postpone.ContextWithValues() Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
…k@main PR link: networkservicemesh/sdk#1035 Commit: a91779d Author: Vladimir Popov Date: 2021-08-17 20:25:36 +0700 Message: - [sdk#1026] Add closectx (#1035) * Add closectx Signed-off-by: Vladimir Popov <vladimir.popov@xored.com> * Add unit tests for Close on canceled context Signed-off-by: Vladimir Popov <vladimir.popov@xored.com> * Rework closectx to use context.Background() Signed-off-by: Vladimir Popov <vladimir.popov@xored.com> * Replace closectx.New() with postpone.ContextWithValues() Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
…k@main PR link: networkservicemesh/sdk#1035 Commit: a91779d Author: Vladimir Popov Date: 2021-08-17 20:25:36 +0700 Message: - [sdk#1026] Add closectx (#1035) * Add closectx Signed-off-by: Vladimir Popov <vladimir.popov@xored.com> * Add unit tests for Close on canceled context Signed-off-by: Vladimir Popov <vladimir.popov@xored.com> * Rework closectx to use context.Background() Signed-off-by: Vladimir Popov <vladimir.popov@xored.com> * Replace closectx.New() with postpone.ContextWithValues() Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
…k@main PR link: networkservicemesh/sdk#1035 Commit: a91779d Author: Vladimir Popov Date: 2021-08-17 20:25:36 +0700 Message: - [sdk#1026] Add closectx (#1035) * Add closectx Signed-off-by: Vladimir Popov <vladimir.popov@xored.com> * Add unit tests for Close on canceled context Signed-off-by: Vladimir Popov <vladimir.popov@xored.com> * Rework closectx to use context.Background() Signed-off-by: Vladimir Popov <vladimir.popov@xored.com> * Replace closectx.New() with postpone.ContextWithValues() Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
…k@main PR link: networkservicemesh/sdk#1035 Commit: a91779d Author: Vladimir Popov Date: 2021-08-17 20:25:36 +0700 Message: - [sdk#1026] Add closectx (#1035) * Add closectx Signed-off-by: Vladimir Popov <vladimir.popov@xored.com> * Add unit tests for Close on canceled context Signed-off-by: Vladimir Popov <vladimir.popov@xored.com> * Rework closectx to use context.Background() Signed-off-by: Vladimir Popov <vladimir.popov@xored.com> * Replace closectx.New() with postpone.ContextWithValues() Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
…k@main PR link: networkservicemesh/sdk#1035 Commit: a91779d Author: Vladimir Popov Date: 2021-08-17 20:25:36 +0700 Message: - [sdk#1026] Add closectx (#1035) * Add closectx Signed-off-by: Vladimir Popov <vladimir.popov@xored.com> * Add unit tests for Close on canceled context Signed-off-by: Vladimir Popov <vladimir.popov@xored.com> * Rework closectx to use context.Background() Signed-off-by: Vladimir Popov <vladimir.popov@xored.com> * Replace closectx.New() with postpone.ContextWithValues() Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
…k@main PR link: networkservicemesh/sdk#1035 Commit: a91779d Author: Vladimir Popov Date: 2021-08-17 20:25:36 +0700 Message: - [sdk#1026] Add closectx (#1035) * Add closectx Signed-off-by: Vladimir Popov <vladimir.popov@xored.com> * Add unit tests for Close on canceled context Signed-off-by: Vladimir Popov <vladimir.popov@xored.com> * Rework closectx to use context.Background() Signed-off-by: Vladimir Popov <vladimir.popov@xored.com> * Replace closectx.New() with postpone.ContextWithValues() Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
…k@main PR link: networkservicemesh/sdk#1035 Commit: a91779d Author: Vladimir Popov Date: 2021-08-17 20:25:36 +0700 Message: - [sdk#1026] Add closectx (#1035) * Add closectx Signed-off-by: Vladimir Popov <vladimir.popov@xored.com> * Add unit tests for Close on canceled context Signed-off-by: Vladimir Popov <vladimir.popov@xored.com> * Rework closectx to use context.Background() Signed-off-by: Vladimir Popov <vladimir.popov@xored.com> * Replace closectx.New() with postpone.ContextWithValues() Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
Description
Adds
closectx.New()
postpone.ContextWithValues()
to be used forClose
/Unregister
in failedRequest
/Register
cases.Issue link
#1026
How Has This Been Tested?
Types of changes