Skip to content

Commit

Permalink
Start HashWheelTimer in ClientResources to avoid blocking calls in Ev…
Browse files Browse the repository at this point in the history
…entLoop #1489

We now eagerly start HashWheelTimer when creating DefaultClientResources to avoid blocking calls originating from Timer startup on an EventLoop thread.
  • Loading branch information
mp911de committed Nov 2, 2020
1 parent 06f5c59 commit dabd8b2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ protected DefaultClientResources(Builder builder) {
reconnectDelay = builder.reconnectDelay;
nettyCustomizer = builder.nettyCustomizer;
tracing = builder.tracing;

if (!sharedTimer && timer instanceof HashedWheelTimer) {
((HashedWheelTimer) timer).start();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import io.netty.util.concurrent.Future;

/**
* Unit tests for {@link DefaultClientResources}.
*
* @author Mark Paluch
*/
class DefaultClientResourcesUnitTests {
Expand All @@ -52,6 +54,10 @@ void testDefaults() throws Exception {
assertThat(sut.commandLatencyRecorder()).isNotNull();
assertThat(sut.commandLatencyRecorder().isEnabled()).isTrue();

HashedWheelTimer timer = (HashedWheelTimer) sut.timer();

assertThat(timer).hasFieldOrPropertyWithValue("workerState", 1);

EventExecutorGroup eventExecutors = sut.eventExecutorGroup();
NioEventLoopGroup eventLoopGroup = sut.eventLoopGroupProvider().allocate(NioEventLoopGroup.class);

Expand Down Expand Up @@ -222,7 +228,7 @@ void considersSharedStateFromMutation() {
ClientResources clientResources = ClientResources.create();
HashedWheelTimer timer = (HashedWheelTimer) clientResources.timer();

assertThat(timer).hasFieldOrPropertyWithValue("workerState", 0);
assertThat(timer).hasFieldOrPropertyWithValue("workerState", 1);

ClientResources copy = clientResources.mutate().build();
assertThat(copy.timer()).isSameAs(timer);
Expand All @@ -238,15 +244,15 @@ void considersDecoupledSharedStateFromMutation() {
ClientResources clientResources = ClientResources.create();
HashedWheelTimer timer = (HashedWheelTimer) clientResources.timer();

assertThat(timer).hasFieldOrPropertyWithValue("workerState", 0);
assertThat(timer).hasFieldOrPropertyWithValue("workerState", 1);

ClientResources copy = clientResources.mutate().timer(new HashedWheelTimer()).build();
HashedWheelTimer copyTimer = (HashedWheelTimer) copy.timer();
assertThat(copy.timer()).isNotSameAs(timer);

copy.shutdown().awaitUninterruptibly();

assertThat(timer).hasFieldOrPropertyWithValue("workerState", 0);
assertThat(timer).hasFieldOrPropertyWithValue("workerState", 1);
assertThat(copyTimer).hasFieldOrPropertyWithValue("workerState", 0);

copyTimer.stop();
Expand Down

0 comments on commit dabd8b2

Please sign in to comment.