fn: expand the ContextGuard and add tests#9343
fn: expand the ContextGuard and add tests#9343guggero merged 2 commits intolightningnetwork:masterfrom
Conversation
|
Important Review skippedAuto reviews are limited to specific labels. 🏷️ Labels to auto review (1)
Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
|
cc @guggero - interested to hear what you think of this 👀 |
|
See usage here: #9344 |
guggero
left a comment
There was a problem hiding this comment.
Concept ACK, nice improvements! Will make it easier and more clear to use.
fn/context_guard.go
Outdated
|
|
||
| // WithCtx is used to derive a cancellable context from the parent. Various | ||
| // options can be provided to configure the behaviour of the derived context. | ||
| func (g *ContextGuard) WithCtx(ctx context.Context, |
There was a problem hiding this comment.
We could rename this to Create() since this is the main purpose of the struct. Then we can keep the With...() naming for the options above (so WithTimeout(), WithCustomTimeout(), WithBlocking() and so on).
There was a problem hiding this comment.
happy to change it to Create
but for the options: the reason I went with TimeoutCGOpt() etc, (ie, usingCGOpt) is mainly cause this isnt in its own package so if we just had WithTimeout but then later on another helper in the fn package introduced functional options where one of them adds a timeout, then what do we call that one? so thought maybe better to namespace from the get go. Thoughts?
There was a problem hiding this comment.
Seen this comment now, see my proposal (postfixing with ContextGuard or CG.
There was a problem hiding this comment.
cool yeah - went with that 👍
1fe1087 to
8bec5f6
Compare
ellemouton
left a comment
There was a problem hiding this comment.
Thanks for taking a look @guggero 🙏 - updated. Just one question about the naming suggestion
fn/context_guard.go
Outdated
|
|
||
| // WithCtx is used to derive a cancellable context from the parent. Various | ||
| // options can be provided to configure the behaviour of the derived context. | ||
| func (g *ContextGuard) WithCtx(ctx context.Context, |
There was a problem hiding this comment.
happy to change it to Create
but for the options: the reason I went with TimeoutCGOpt() etc, (ie, usingCGOpt) is mainly cause this isnt in its own package so if we just had WithTimeout but then later on another helper in the fn package introduced functional options where one of them adds a timeout, then what do we call that one? so thought maybe better to namespace from the get go. Thoughts?
8bec5f6 to
b24241f
Compare
bhandras
left a comment
There was a problem hiding this comment.
LGTM, very nice work! ✨
fn/context_guard.go
Outdated
| func (g *ContextGuard) CtxBlocking() (context.Context, func()) { | ||
| return g.CtxBlockingCustomTimeout(g.DefaultTimeout) | ||
| // important tasks. | ||
| func BlockingCGOpt() ContextGuardOption { |
There was a problem hiding this comment.
nit: naming wise should we use the With prefix for functional options? So something like: WithBlockingContextGuard/CG, WithCustomTimeoutContextGuard/CG etc? I think this style is widely used around the codebase.
There was a problem hiding this comment.
cool yeah good naming suggestion 🙏 updated to this!
fn/context_guard.go
Outdated
|
|
||
| // WithCtx is used to derive a cancellable context from the parent. Various | ||
| // options can be provided to configure the behaviour of the derived context. | ||
| func (g *ContextGuard) WithCtx(ctx context.Context, |
There was a problem hiding this comment.
Seen this comment now, see my proposal (postfixing with ContextGuard or CG.
| return | ||
| } | ||
|
|
||
| fn() |
There was a problem hiding this comment.
Does the cancel fn need to be called while we hold the mutex?
There was a problem hiding this comment.
cool - restructured it a bit (still want the delete to fall under the lock)
but fwiw, i dont think a context.CancelFunc is ever blocking so dont think it really matters thaaaat much. updated non the less!
|
|
||
| // Give the goroutine above a chance to spin up so that it's | ||
| // waiting on the select. | ||
| time.Sleep(time.Millisecond * 200) |
There was a problem hiding this comment.
nit: alternatively you could read from a temp channel that is written to in the goroutine's body.
There was a problem hiding this comment.
(and below for similar sleeps).
There was a problem hiding this comment.
if we do that then yes we know that the goroutine has at least started, but we dont actually know that it is hanging on the select so it will still be flaky in terms of timing there
| // will be not be cancelled when the quit channel is closed but will be | ||
| // if either the context is cancelled or the timeout is reached. | ||
| t.Run("Blocking context with timeout", func(t *testing.T) { | ||
| t.Parallel() |
There was a problem hiding this comment.
I wonder if you'd consider adding a GuardTimeout to these tests (see here:
lnd/cluster/etcd_elector_test.go
Line 19 in fdb43c0
There was a problem hiding this comment.
oh cool! TIL!
I like it!
There was a problem hiding this comment.
ok i've added it to the main test.. do you think it should instead be invoked for each sub test?
b24241f to
8dd07dc
Compare
ellemouton
left a comment
There was a problem hiding this comment.
updated, thanks @bhandras !
fn/context_guard.go
Outdated
| func (g *ContextGuard) CtxBlocking() (context.Context, func()) { | ||
| return g.CtxBlockingCustomTimeout(g.DefaultTimeout) | ||
| // important tasks. | ||
| func BlockingCGOpt() ContextGuardOption { |
There was a problem hiding this comment.
cool yeah good naming suggestion 🙏 updated to this!
fn/context_guard.go
Outdated
|
|
||
| // WithCtx is used to derive a cancellable context from the parent. Various | ||
| // options can be provided to configure the behaviour of the derived context. | ||
| func (g *ContextGuard) WithCtx(ctx context.Context, |
There was a problem hiding this comment.
cool yeah - went with that 👍
| return | ||
| } | ||
|
|
||
| fn() |
There was a problem hiding this comment.
cool - restructured it a bit (still want the delete to fall under the lock)
but fwiw, i dont think a context.CancelFunc is ever blocking so dont think it really matters thaaaat much. updated non the less!
|
|
||
| // Give the goroutine above a chance to spin up so that it's | ||
| // waiting on the select. | ||
| time.Sleep(time.Millisecond * 200) |
There was a problem hiding this comment.
if we do that then yes we know that the goroutine has at least started, but we dont actually know that it is hanging on the select so it will still be flaky in terms of timing there
| // will be not be cancelled when the quit channel is closed but will be | ||
| // if either the context is cancelled or the timeout is reached. | ||
| t.Run("Blocking context with timeout", func(t *testing.T) { | ||
| t.Parallel() |
There was a problem hiding this comment.
oh cool! TIL!
I like it!
| // will be not be cancelled when the quit channel is closed but will be | ||
| // if either the context is cancelled or the timeout is reached. | ||
| t.Run("Blocking context with timeout", func(t *testing.T) { | ||
| t.Parallel() |
There was a problem hiding this comment.
ok i've added it to the main test.. do you think it should instead be invoked for each sub test?
Copied from lightninglabs/loop
In this commit, the ContextGuard struct is re-worked such that the context that its new main WithCtx method provides is cancelled in sync with a parent context being cancelled or with it's quit channel being cancelled. Tests are added to assert the behaviour. In order for the close of the quit channel to be consistent with the cancelling of the derived context, the quit channel _must_ be contained internal to the ContextGuard so that callers are only able to close the channel via the exposed Quit method which will then take care to first cancel any derived context that depend on the quit channel before returning.
8dd07dc to
f99cabf
Compare
|
Pushed a new tag |
In this commit, the ContextGuard struct is re-worked such that the context that its new main
WithCtxmethod provides is cancelled in sync with a parent context being cancelled or with it's quit channel being cancelled. Tests are added to assert the behaviour. In order for the close of the quit channel to be consistent with the cancelling of the derived context, the quit channel must be contained internal to the ContextGuard so that callers are only able to close the channel via the exposed Quit method which will then take care to first cancel any derived context that depend on the quit channel before returning.Furthermore, the main
WithCtxmethod, is extended to take a parent context such that any derived context extends theparent instead of needing to create a fresh one.
PR updating to the new fn version & using the updated ContextGuard: #9344