Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace EmbeddedChannel's 'frozen time' feature with Ticker #13169

Merged
merged 5 commits into from
Mar 18, 2023
Merged

Conversation

trustin
Copy link
Member

@trustin trustin commented Jan 30, 2023

Motivation:

Netty currently lacks a standard abstraction for time source, as known as 'ticker' or 'clock'. As a result, Netty relies on ad-hoc overridable protected methods for 'time getter' methods, which gives hard time to users who want to test the time-sensitive logic that involes Netty.

Modifications:

  • Added Ticker and MockTicker and their default implementations.
  • (Breaking) Updated EmbeddedChannel and EmbeddedEventLoop to use Ticker instead of its own time manipulation API.
    • Removed the old time manipulation API.
    • Note that I removed freezeTime() and unfreezeTime() because it can easily be replicated with advance().

Result:

  • Netty now has a better abstraction for scheduling and testing time-sensitive logic.
  • A user can now specify system, mock or custom Ticker implementation when constructing an EmbeddedChannel, which is more flexible than the previous API.
    • (Breaking) The previous time-manipulation API in EmbeddedChannel has been removed in favor of the new API.
  • Partially resolves Expose AbstractScheduledEventExecutor.getCurrentTimeNanos #12827. However, this PR didn't update IdleStateHandler or any other time-sensitive logic in Netty, which is left as future work.

Motivation:

Netty currently lacks a standard abstraction for time source, as known
as 'ticker' or 'clock'. As a result, Netty relies on ad-hoc overridable
protected methods for 'time getter' methods, which gives hard time to
users who want to test the time-sensitive logic that involes Netty.

Modifications:

- Added `Ticker` and `MockTicker` and their default implementations.
- (Breaking) Updated `EmbeddedChannel` and `EmbeddedEventLoop` to use
  `Ticker` instead of its own time manipulation API.
  - Removed the old time manipulation API.
  - Note that I removed `freezeTime()` and `unfreezeTime()` because it
    can easily be replicated with `advance()`.

Result:

- Netty now has a better abstraction for scheduling and testing
  time-sensitive logic.
- A user can now specify system, mock or custom `Ticker` implementation
  when constructing an `EmbeddedChannel`, which is more flexible than
  the previous API.
  - (Breaking) The previous time-manipulation API in `EmbeddedChannel`
    has been removed in favor of the new API.
- Partially resolves #12827. However, this PR didn't update
  `IdleStateHandler` or any other time-sensitive logic in Netty, which
  is left as future work.
@yawkat
Copy link
Contributor

yawkat commented Jan 30, 2023

lgtm, but this change only applies to EmbeddedEventLoop still, do you plan to move the ticker to "normal" event loops later?

@trustin
Copy link
Member Author

trustin commented Jan 30, 2023

lgtm, but this change only applies to EmbeddedEventLoop still, do you plan to move the ticker to "normal" event loops later?

Thanks for taking a look, @yawkat 🙇 Yeah, that's the plan.

@yawkat
Copy link
Contributor

yawkat commented Jan 30, 2023

i wish fixing bad sleep was always this easy

…ntExecutor.java

Co-authored-by: jchrys <jchrys@me.com>
@trustin
Copy link
Member Author

trustin commented Feb 5, 2023

@normanmaurer @chrisvest @hyperxpro Thoughts on this change? Let me continue on this path if it looks good to you.

Copy link
Contributor

@hyperxpro hyperxpro left a comment

Choose a reason for hiding this comment

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

This API is a lot better than before! ^^

void newMockTickerShouldReturnDefaultMockTicker() {
assertTrue(Ticker.newMockTicker() instanceof DefaultMockTicker);
}
@Test
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
@Test
@Test

@trustin
Copy link
Member Author

trustin commented Feb 13, 2023

@hyperxpro Thanks for reviewing. Let me address the comments.

@trustin
Copy link
Member Author

trustin commented Feb 20, 2023

@hyperxpro Resolved all comments. PTAL again. 🙇

@trustin
Copy link
Member Author

trustin commented Mar 12, 2023

Will merge this if I don't get any comments until I check this PR again, like in 3 days.

Copy link
Contributor

@hyperxpro hyperxpro left a comment

Choose a reason for hiding this comment

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

Sorry for delay ;/

LGTM!

@trustin trustin merged commit f29593d into main Mar 18, 2023
@trustin trustin deleted the ticker branch March 18, 2023 05:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants