Open Wireless4024 opened 2 months ago
It's good! I wanted to add metrics that expose more detailed behavior of the runtime internals because I encountered some related needs, but it seems that your suggestion is better: let users plug in hooks to complete these statistics and even more things by themselves. It is also zero-cost because it has no overhead when users do not need to perform custom behavior. Adding such a Callback will make the generics of Runtime and RuntimeBuilder more complicated. Maybe using a default type can solve this problem.
can pre_park hook also have bool in return value as skip park? use case is when having code to spawn or wake something inside hook. I think by default runtime will continue to park?
this idea seem hard for user to get it right, always false make it not wake the runtime and alway true turn runtime into spin lock :smile:.
The runtime needs syscall when park to get op completion or fd readiness. So skip park is not correct, but you can make the wait time to zero to do spin(currently there's no method to do it, previously I impl a draft to allow users impl io component in https://github.com/bytedance/monoio/pull/269).
As you mentioned, control park(timeout=None or Some(0))
by the hook's return value is a good solution too, though it may be misused(return an enum should make it harder to misuse). A poc is like:
enum ParkPolicy {
Wait,
Spin,
}
pub trait ParkHook {
fn before_park(&mut self) -> ParkPolicy;
}
or pass runtime's park as a lambda to the hook, and let it control the park timeout:
pub trait ParkHook {
fn park(&mut self, f: impl FnOnce(Option<Duration>));
}
Is your feature request related to a problem? Please describe. I have a special use case for my buffer pool,
Describe the solution you'd like and I would like to have some function to register hook before executor park itself, so I can compact my buffer that might overflow its capacity during brust of connections.
implmentation example
probably run the hook before this call. https://github.com/bytedance/monoio/blob/master/monoio/src/runtime.rs#L187