Skip to content

Conversation

@sgbalogh
Copy link

Hi! I noticed some strange behavior while running a simulation where the tick duration was sub-millisecond, and discovered that tokio::time::sleep apparently uses millisecond resolution.

I added some validation to the builder, to panic whenever a duration with higher than ms resolution is supplied, since it looked like some other validation was already happening here. Let me know if this seems useful.

Love the project btw!

Copy link
Contributor

@mcches mcches left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for improving this!

src/builder.rs Outdated

if self.config.tick.as_nanos() % Duration::from_millis(1).as_nanos() != 0 {
// Tick duration is used for tokio::time::sleep, which requires millisecond resolution.
panic!("Tick duration resolution is in milliseconds, but value provided would require higher.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding the provided value in the panic message to aid in debugging.

panic!("Maximum message latency must be greater than minimum.");
}

if self.config.tick.as_nanos() % Duration::from_millis(1).as_nanos() != 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be simpler to just check < Duration::from_millis(1)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think someone could provide a value that was > 1 ms, but had a higher resolution, e.g. Duration::from_micros(1500), which I believe we would want to disallow still given that comment on tokio::time::sleep.

Though, we might want to disallow Duration::ZERO in addition I suppose?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it needs to be this restrictive. Given that the runtime is paused we aren't actually running for the duration, we are simply advancing time by it.

Duration::ZERO is a good callout, and I'm not actually sure what would happen in that case. My guess is that nothing runs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants