tokio-timer icon indicating copy to clipboard operation
tokio-timer copied to clipboard

Initial Capacity = Max(Initial Capacity, Channel Capacity)

Open GGist opened this issue 7 years ago • 1 comments

I am seeing issues because of this. For example, the following code:

    timer::wheel()
        .num_slots(8)
        .tick_duration(Duration::from_millis(58))
        .max_capacity(2000)
        .channel_capacity(2000)
        .num_slots(2048)
        .build();

Will cause problems at the call to unwrap at https://github.com/tokio-rs/tokio-timer/blob/master/src/worker.rs#L56 since https://github.com/tokio-rs/tokio-timer/blob/master/src/worker.rs#L52 won't protect us if capacity is not a power of two, since we will end up increasing capacity at https://github.com/tokio-rs/tokio-timer/blob/master/src/mpmc.rs#L32 which then causes the outer call to unwrap to fail.

This code where channel capacity is greater than max capacity also causes a different issue:

    timer::wheel()
        .num_slots(8)
        .tick_duration(Duration::from_millis(58))
        .max_capacity(2000)
        .channel_capacity(2001)
        .num_slots(2048)
        .build();

The error occurs at https://github.com/tokio-rs/tokio-timer/blob/master/src/wheel.rs#L109 due to a subtraction underflow (because by the time https://github.com/tokio-rs/tokio-timer/blob/master/src/wheel.rs#L107 is triggered, we were already past self.max_capacity since initial capacity was derived from a channel capacity that was greater than max capacity).

I don't mind submitting a PR, but wanted to check first what would be the best course of action, as there are a couple ways to fix these two issues.

GGist avatar Jul 07 '17 06:07 GGist

In particular, I was thinking we could do two things in the builder:

  • Do the power of two promotion on channel capacity in the channel capacity getter
  • Compare initial capacity against max capacity in the initial capacity getter

Let me know what you think.

GGist avatar Jul 07 '17 06:07 GGist