From bde2865ef893f907fe99a140209f34af4d4396ef Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Fri, 2 Nov 2018 08:10:18 +0100 Subject: [PATCH] Make it clear that HashedWheelTimer only support millis. (#8322) Motivation: HWT does not support anything smaller then 1ms so we should make it clear that this is the case. Modifications: Log a warning if < 1ms is used. Result: Less suprising behaviour. --- .../java/io/netty/util/HashedWheelTimer.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/common/src/main/java/io/netty/util/HashedWheelTimer.java b/common/src/main/java/io/netty/util/HashedWheelTimer.java index e0afbe67ce..e8e72b6387 100644 --- a/common/src/main/java/io/netty/util/HashedWheelTimer.java +++ b/common/src/main/java/io/netty/util/HashedWheelTimer.java @@ -84,6 +84,7 @@ public class HashedWheelTimer implements Timer { private static final AtomicInteger INSTANCE_COUNTER = new AtomicInteger(); private static final AtomicBoolean WARNED_TOO_MANY_INSTANCES = new AtomicBoolean(); private static final int INSTANCE_COUNT_LIMIT = 64; + private static final long MILLISECOND_NANOS = TimeUnit.MILLISECONDS.toNanos(1); private static final ResourceLeakDetector leakDetector = ResourceLeakDetectorFactory.instance() .newResourceLeakDetector(HashedWheelTimer.class, 1); @@ -259,14 +260,25 @@ public class HashedWheelTimer implements Timer { mask = wheel.length - 1; // Convert tickDuration to nanos. - this.tickDuration = unit.toNanos(tickDuration); + long duration = unit.toNanos(tickDuration); // Prevent overflow. - if (this.tickDuration >= Long.MAX_VALUE / wheel.length) { + if (duration >= Long.MAX_VALUE / wheel.length) { throw new IllegalArgumentException(String.format( "tickDuration: %d (expected: 0 < tickDuration in nanos < %d", tickDuration, Long.MAX_VALUE / wheel.length)); } + + if (duration < MILLISECOND_NANOS) { + if (logger.isWarnEnabled()) { + logger.warn("Configured tickDuration %d smaller then %d, using 1ms.", + tickDuration, MILLISECOND_NANOS); + } + this.tickDuration = MILLISECOND_NANOS; + } else { + this.tickDuration = duration; + } + workerThread = threadFactory.newThread(worker); leak = leakDetection || !workerThread.isDaemon() ? leakDetector.track(this) : null;