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
Update tokio-macros to new builder #2906
Conversation
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 a ton for getting this started!
tokio-macros/src/entry.rs
Outdated
"in_place" => Ok(RuntimeFlavor::InPlace), | ||
"work_stealing" => Ok(RuntimeFlavor::WorkStealing), | ||
"single_threaded" => Err(format!("The single threaded runtime flavor is called `in_place`.")), | ||
"basic_scheduler" => Err(format!("The `basic_scheduler` runtime flavor has been renamed to `in_place`.")), |
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.
do we need to do this? I'd rather not leave "old" stuff in a breaking change.
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.
No. I added it to ease the transition for users coming from v0.2, and because it wouldn't be a breaking change to remove later.
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 this will just generate a compile failure right? Then I think that is fine.
tokio-macros/src/entry.rs
Outdated
"single_threaded" => Err(format!("The single threaded runtime flavor is called `in_place`.")), | ||
"basic_scheduler" => Err(format!("The `basic_scheduler` runtime flavor has been renamed to `in_place`.")), | ||
"threaded_scheduler" => Err(format!("The `threaded_scheduler` runtime flavor has been renamed to `work_stealing`.")), | ||
_ => Err(format!("No such runtime flavor `{}`.", s)), |
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.
Should we list the valid options here?
num_workers: Option<(usize, Span)>, | ||
max_threads: Option<(usize, Span)>, | ||
} | ||
impl Configuration { |
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.
Some new lines in this file would be nice 😄
tokio-macros/src/entry.rs
Outdated
"The `num_workers` option requires the `work_stealing` runtime flavor." | ||
)); | ||
} | ||
assert!(self.num_workers.is_none()); |
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.
can we statically ensure this somehow? Maybe via enum values?
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 could certainly statically ensure it in the FinalConfig
, but that wouldn't remove this assert. I suppose we could turn Configuration
into a state machine of some sort.
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.
Though turning Configuration
into a state machine is awkward. I chose the current design because it allows e.g. setting num_workers
first, then setting the flavor
.
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.
Here's one option:
match (flavor, self.num_workers) {
(CurrentThread, Some((_, num_workers_span))) => {
Err(syn::Error::new(
num_workers_span.clone(),
"The `num_workers` option requires the `threaded` runtime flavor."
))
},
(CurrentThread, None) => {
Ok(FinalConfig {
flavor,
num_workers: None,
})
},
(Threaded, num_workers) if self.rt_threaded_available => {
Ok(FinalConfig {
flavor,
num_workers: num_workers.map(|(val, _span)| val),
})
},
(Threaded, _) => {
let msg = if self.flavor.is_none() {
"The default runtime flavor is `threaded`, but the `rt-threaded` feature is disabled."
} else {
"The runtime flavor `threaded` requires the `rt-threaded` feature."
};
Err(syn::Error::new(Span::call_site(), msg))
},
}
tokio-macros/src/entry.rs
Outdated
(None, Some((_, span))) => { | ||
return Err(syn::Error::new( | ||
span, | ||
"`num_workers` must also be set when using `max_threads`" |
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 following why this is required?
tokio-macros/src/entry.rs
Outdated
(Some((num_workers, _)), Some((max_threads, span))) if max_threads <= num_workers => { | ||
return Err(syn::Error::new( | ||
span, | ||
"`max_threads` must be larger than `num_workers`", |
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.
In this case, can we just default to max(core_threads, max_threads)
? I think that is a better default and I think we should do the same in the builder.
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.
Allowing max_threads == core_threads
seems dangerous and like an edge case that I'm not sure we should support from the macro.
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 could do max_threads = core_threads + 1
at a min. That said, lets not do this here since I think this could also be a potential footgun. Honestly, should we even allow someone to set max threads here?
}, | ||
"core_threads" => { |
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 are renaming lets just drop it.
pub fn main_fail(_args: TokenStream, _item: TokenStream) -> TokenStream { | ||
syn::Error::new( | ||
proc_macro2::Span::call_site(), | ||
"The #[tokio::main] macro requires rt-core or rt-threaded.", |
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.
should we link to docs here?
tokio/tests/task_blocking.rs
Outdated
@@ -28,7 +28,7 @@ async fn basic_blocking() { | |||
} | |||
} | |||
|
|||
#[tokio::test(threaded_scheduler)] | |||
#[tokio::test(flavor = "work_stealing")] |
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.
personally, I prefer the old method rather than this change.
@Darksonn looks like some CI errors? |
I managed to make it compile. |
Thanks 👍 I merged this into #2876. I am going to close this PR. |
Macro part of #2720 (comment).
Since the builder is not yet fully updated, this still generates code with the old builder syntax, but this is easily changed later. The
rt-threaded
feature is also not renamed tort-work-stealing
, but this is also easily changed later. Documentation is still a TODO.