You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
I have a special use case for my buffer pool,
structBufferPool{keep_capacity:usize,buffers:UnsafeCell<VecDeque<BytesMut>>,}implBufferPool{fnnew(keep_capacity:usize) -> Self{todo!()}fntake(&self,capacity:usize) -> BytesMut{todo!()}fnstore(&self,buffer:BytesMut){}// cleanup extra buffer, deallocation can take some timefncompact(&self){}}asyncfninit_listening_task(){let pool = Rc::new(BufferPool::new(16));// the idea!register_pre_park_hook(Rc::clone(&pool));loop{let buffer = pool.take(4096);let pool = Rc::clone(&pool);// accept connectionspawn(asyncmove{// do something with buffer
pool.store(buffer);});}}
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
implPreParkHookforBufferPool{fnpre_park(&self){self.compact();}}traitPreParkHook{fnpre_park(&self);}// example signature of register_pre_park_hookfnregister_pre_park_hook<T:PreParkHook>(value:Rc<T>){let hook = value asRc<dynPreParkHook>;// put into runtime context}// example of code that will run during pre-parkfnrun_pre_park(hooks:&mutVecDeque<Rc<dynPreParkHook>>){letmut i = 0;while i < hooks.len(){let hook = &hooks[i];ifRc::strong_count(hook) == 1{// cleanup hook that has no strong reference// drop here instead of using weak to reduce// performance hit of drop call of the hook during execution
hooks.remove(i);continue;}
hook.pre_park();
i += 1;}}
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 😄.
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 #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:
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
The text was updated successfully, but these errors were encountered: