-
Notifications
You must be signed in to change notification settings - Fork 408
feat(background-processor): add BackgroundProcessorBuilder for optional components #3688
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
base: main
Are you sure you want to change the base?
feat(background-processor): add BackgroundProcessorBuilder for optional components #3688
Conversation
👋 I see @wpaulino was un-assigned. |
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.
Already looks pretty good, one question though.
Also, do you have any idea how we could solve the same issue for the async variant of the background processor, i.e., for the optional arguments of process_events_async
?
@@ -1046,6 +1049,171 @@ impl BackgroundProcessor { | |||
None => Ok(()), | |||
} | |||
} | |||
|
|||
/// Creates a new [`BackgroundProcessorBuilder`] to construct a [`BackgroundProcessor`] with optional components. | |||
pub fn builder<'a, PS, EH, M, CM, PGS, RGS, G, UL, L, PM>( |
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.
I'm a bit confused on the purpose of this method: in which scenario would we already have a working BackgroundProcessor
to call builder
on, just to then use the BackgroundProcessorBuilder
to create yet another BackgroundProcessor
?
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 confusing indeed. I was thinking the BackgroundProcessorBuilder::new()
can be an internal method and accessible via builder
, but it doesn't make sense. I will remove the builder
method here and allow users use BackgroundProcessorBuilder::new()
directly.
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.
@tnull this doesn't take self
, though?
Either way, normally the builder pattern has the builder()
associated function, but works something like:
BackgroundProcessor::builder()
constructs a BackgroundProcessorBuilder
with "default" fields (which might be hard in this case), and you would not have any parameters for builder()
.
So yeah, probably removing this is best.
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.
@tnull this doesn't take self, though?
Ah true.
Either way, normally the builder pattern has the
builder()
associated function, but works something like:BackgroundProcessor::builder()
constructs aBackgroundProcessorBuilder
with "default" fields (which might be hard in this case), and you would not have any parameters forbuilder()
.
Yeah, unclear what defaults would be.
👋 The first review has been submitted! Do you think this PR is ready for a second reviewer? If so, click here to assign a second reviewer. |
Yea, I think we can extend the new |
The question is how this would work? What do you think? |
Thank you for the suggestion. I agree that introducing a What do you think about that? |
Sounds good to me, although we might need to account for the fact that the two variants have slightly different type requirements (i.e., trait bounds). But I think we should be able to accommodate that via feature-gates on |
Yes, that can be handled conditionally using feature-gates. I guess I can go ahead with the implementation, right? |
This PR is ready for another round of review. |
onion_messenger: Option<OM>, gossip_sync: GossipSync<PGS, RGS, G, UL, L>, peer_manager: PM, | ||
logger: L, scorer: Option<S>, sleeper: Sleeper, mobile_interruptable_platform: bool, | ||
fetch_time: FetchTime, | ||
config: BackgroundProcessorConfig< |
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.
nit: I think we might want to add a #[rustfmt::skip]
here/above to avoid having rustfmt
making this that vertical.
@@ -1048,6 +1160,227 @@ impl BackgroundProcessor { | |||
} | |||
} | |||
|
|||
/// Configuration for both synchronous [`BackgroundProcessor`] and asynchronous [`process_events_async`] event processing. |
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.
I'm not sure these doc links will work under all circumstances as process_events_async
will only be exposed when the futures
feature is set. You might need to alter the docs based on the set feature via cfg_attr
, see the changes to the background processor docs in #3509 for reference how to do this.
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.
Ah true! Thank you.
_phantom: PhantomData<(&'a (), CF, T, F, P)>, | ||
} | ||
|
||
/// A builder for constructing a [`BackgroundProcessor`] with optional components. |
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 should say BackgroundProcessorConfig
now, no? And do we want to rename the builder to BackgroundProcessorConfigBuilder
?
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.
I agree. The builder build
s a BackgroundProcessorConfig
object, so renaming that to BackgroundProcessorConfigBuilder
might be more suitable.
PM::Target: APeerManager + Send + Sync, | ||
{ | ||
/// Creates a new builder instance. | ||
pub(crate) fn new( |
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.
Seems CI is failing due to this being pub(crate)
?
Did another round of review, will take another look once CI passes. |
/// .build(); | ||
/// let bg_processor = BackgroundProcessor::from_config(config); | ||
/// ``` | ||
pub fn from_config< |
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.
I wonder if this should even replace start
(i.e., whether we should have start
just take a BackgroundProcessorConfig
)?
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.
I thought about this too but not sure if we want to make that change on start
immediately.
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.
Okay. I think we could drop the parameters from start
and really only have it start the background thread, but no strong opinion.
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.
Makes sense. So start
takes a BackgroundProcessorConfig
, and we can drop the from_config
method?
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.
Yeah, it sounds reasoable to have that as the new default constructor, as it should be easier to use anyways. Not sure if somebody else has a different opinion though, may be good to run this by a second reviewer.
Thank you very much for the review. I'll address all the feedbacks and update the PR. |
🔔 1st Reminder Hey @dunxen! This PR has been waiting for your review. |
🔔 2nd Reminder Hey @dunxen! This PR has been waiting for your review. |
CI still failing, fixing it. |
🔔 3rd Reminder Hey @dunxen! This PR has been waiting for your review. |
b967d87
to
cb7ed79
Compare
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.
Thanks! You'll need to run cargo fmt
on lightning-background-processor/src/lib.rs
and make sure each commit builds fine. You can run this check locally with ./ci/check-each-commit.sh main
. It will do an interactive rebase and stop at the first problem commit, which you can fix with appropriate changes, git commit --amend '-S'
, and then git rebase --continue
.
Thank you so much for the pointer. The email notification was getting much and I looked at the contributing guide severally to see if I can find some info on how to run the CI checks locally, but I couldn't find it. Happy to add that, if it's worth having. |
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.
It seems CI is still broken due to broken docstest(s) in lightning-background-processor
.
e862860
to
7e707a9
Compare
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 needs a rebase now that #3509 was merged.
Yes, I see the conflicts already. I'll fix that. |
Sorry I've not been available. I'll be continuing work on this PR this weekend to address all the issues. |
7e707a9
to
3445bd5
Compare
3445bd5
to
793c777
Compare
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3688 +/- ##
==========================================
+ Coverage 89.76% 90.69% +0.92%
==========================================
Files 159 159
Lines 128828 138540 +9712
Branches 128828 138540 +9712
==========================================
+ Hits 115644 125645 +10001
+ Misses 10503 10363 -140
+ Partials 2681 2532 -149 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Had to clean up and squash the commits while fixing the CI error. May still need more work, but the PR is ready for another round of review. |
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 good so far, although the async
counterpart is still missing. FWIW, if we can't reuse the exact type we should just have process_events_async
take a similar AsyncBackgroundProcessorConfig
or BackgroundProcessorConfigAsync
that can be constructed via a corresponding builder.
Also seems like the second fixup commit can be squashed in?
/// event processing. | ||
/// | ||
/// This configuration holds all components needed for background processing, | ||
/// including required components (like the channel manager and peer manager) and optional |
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.
nit: Let's either drop the examples here (as they don't add much IMO), or tick and link the actual objects, which will ensure they don't become outdated in the future as cargo doc
will check the links.
/// A builder for constructing a [`BackgroundProcessorConfig`] with optional components. | ||
/// | ||
/// This builder provides a flexible and type-safe way to construct a [`BackgroundProcessorConfig`] | ||
/// with optional components like `onion_messenger` and `scorer`. It helps avoid specifying |
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.
nit: Same here, let's either drop the like
examples, or link them properly (maybe the former is preferable).
K::Target: 'static + KVStore, | ||
{ | ||
/// Creates a new builder instance. | ||
pub fn new( |
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.
Hmm, still on the fence whether it makes sense to re-add the BackgroundProcessorConfig::builder
counterpart to this, which however usually doesn't take any parameters. But should we maybe still do 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.
That's a good suggestion. But I'm not sure how that will work since we don't have defaults for all the parameters.
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.
Well, I guess you could require to pass all required parameters to builder
, but that breaks the pattern a bit, which is why I'm on the fence still :)
Up to you, I think.
/// .build(); | ||
/// let bg_processor = BackgroundProcessor::from_config(config); | ||
/// ``` | ||
pub fn from_config< |
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.
Okay. I think we could drop the parameters from start
and really only have it start the background thread, but no strong opinion.
Yes. Thank you.
Yes, I'll add an |
This PR introduces two main improvements to the
BackgroundProcessor
:Adds a
BackgroundProcessorBuilder
for a more ergonomic way to construct aBackgroundProcessor
, and supports optional parameters through builder methodsIntroduces
BackgroundProcessorConfig
to standardize configuration across sync (BackgroundProcessor
) and async variants (process_events_async
), enabling same configuration to be used for both variants. The Builder returns this config object, which can be used to construct a processor viaBackgroundProcessor::from_config
Fixes #3612