tailhook / rotor

The mio-based framework for rust for doing I/O in simple and composable way (ABANDONED)
MIT License
361 stars 25 forks source link

Expose mio::Handler.tick #17

Open seanmonstar opened 8 years ago

seanmonstar commented 8 years ago

Besides for some debugging purposes, (such as seeing how many ticks occurring before my other code wakes the socket up again), this would also be useful for the Machine to be able to notify a user if they are doing blocking work, or otherwise too much work, in the ready event. One could observe the time difference between ticks, and above a threshold, act accordingly.

tailhook commented 8 years ago

Well, I'm okay with exposing tick, but I'm not sure about the API's. There are now global events in rotor yet. But for your use case:

this would also be useful for the Machine to be able to notify a user if they are doing blocking work, or otherwise too much work, in the ready event. One could observe the time difference between ticks, and above a threshold, act accordingly.

The tick event is inherently global (for all state machines). To observe ready events for your state machine you may just write a wrapper type:

struct TickMeasure<M>(M);

impl<M> Machine for TickMeasure<M> {
    fn ready(...) {
        let tm = get_time();
        let res = self.0.ready(...);
        println!("time {:?}", get_time() - tm);
        return res;
    }
}

Then you can wrap any state machine into this type. Surely, you can wrap every state machine with it, just measurements will be individual (comparing to EventLoop::tick())

You can also submit a pull request to rotor-tools if you think such utility is generally useful.