Search code examples
rustidentifiermio

What constraints should I be aware of when generating mio's Tokens?


The mio library for asynchronous I/O relies on the developer to provide instances of the Token type in order to correlate events that have happened back to the source, e.g. a particular TcpStream or Handler::Timeout.

As you can see from the implementation, Token is just a wrapper type around a usize. It's tempting to simply increment a counter each time a Token is needed, but it will eventually overflow.

What rules should I keep in mind as I go to generate Tokens to pass to the EventLoop? Some specific questions:

  • If I have two threads who each have their own EventLoop, can they both use Token=0 to listen for events on two different streams? (i.e. are Tokens bound to a particular EventLoop instance?)
  • Can I use Token=0 to simultaneously represent both a TcpStream and a pending Timeout, or are they both stored in the same collection of Tokens?
  • Is there any harm in jumping from 0 to 1,000,000? (e.g. Are they being stored in a data structure that's optimized for sequential numbers?)

Thanks!


Solution

  • The short version: mio doesn't actually do anything with the tokens except pass them back to you when you receive the corresponding event, so you can use whatever tokens you want as far as mio is concerned. To answer your questions individually:

    If I have two threads who each have their own EventLoop, can they both use Token=0 to listen for events on two different streams? (i.e. are Tokens bound to a particular EventLoop instance?)

    Sure, that's fine.

    Can I use Token=0 to simultaneously represent both a TcpStream and a pending Timeout, or are they both stored in the same collection of Tokens?

    mio doesn't have a collection of tokens. If you don't need unique tokens to identify things in your application code, you're free to use the same token in different places. (I'm a bit confused by this question though, since as far as I can tell, timeouts don't use mio Tokens at all)

    Is there any harm in jumping from 0 to 1,000,000? (e.g. Are they being stored in a data structure that's optimized for sequential numbers?)

    No. As I said above, mio doesn't care about the value of your tokens.