-
-
Notifications
You must be signed in to change notification settings - Fork 15.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace
EmbeddedChannel
's 'frozen time' feature with Ticker
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.
- Loading branch information
Showing
14 changed files
with
483 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
common/src/main/java/io/netty5/util/concurrent/DefaultMockTicker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2023 The Netty Project | ||
* | ||
* The Netty Project licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package io.netty5.util.concurrent; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
import java.util.concurrent.locks.Condition; | ||
import java.util.concurrent.locks.Lock; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
import static io.netty5.util.internal.ObjectUtil.checkPositiveOrZero; | ||
|
||
/** | ||
* The default {@link MockTicker} implementation. | ||
*/ | ||
final class DefaultMockTicker implements MockTicker { | ||
|
||
private final Lock lock = new ReentrantLock(); | ||
private final Condition cond = lock.newCondition(); | ||
private final AtomicLong nanoTime = new AtomicLong(); | ||
|
||
@Override | ||
public long nanoTime() { | ||
return nanoTime.get(); | ||
} | ||
|
||
@Override | ||
public void sleep(long delay, TimeUnit unit) throws InterruptedException { | ||
checkPositiveOrZero(delay, "delay"); | ||
if (delay == 0) { | ||
return; | ||
} | ||
|
||
final long startTimeNanos = nanoTime(); | ||
final long delayNanos = unit.toNanos(delay); | ||
lock.lockInterruptibly(); | ||
try { | ||
do { | ||
cond.await(); | ||
} while (nanoTime() - startTimeNanos < delayNanos); | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
|
||
@Override | ||
public void advance(long amount, TimeUnit unit) { | ||
checkPositiveOrZero(amount, "amount"); | ||
if (amount == 0) { | ||
return; | ||
} | ||
|
||
final long amountNanos = unit.toNanos(amount); | ||
lock.lock(); | ||
try { | ||
nanoTime.addAndGet(amountNanos); | ||
cond.signalAll(); | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
common/src/main/java/io/netty5/util/concurrent/MockTicker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2023 The Netty Project | ||
* | ||
* The Netty Project licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package io.netty5.util.concurrent; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* A fake {@link Ticker} that allows the caller control the flow of time. | ||
* This can be useful when you test time-sensitive logic without waiting for too long | ||
* or introducing flakiness due to non-deterministic nature of system clock. | ||
*/ | ||
public interface MockTicker extends Ticker { | ||
@Override | ||
default long initialNanoTime() { | ||
return 0; | ||
} | ||
|
||
/** | ||
* Advances the current {@link #nanoTime()} by the given amount of time. | ||
* | ||
* @param amount the amount of time to advance this ticker by. | ||
* @param unit the {@link TimeUnit} of {@code amount}. | ||
*/ | ||
void advance(long amount, TimeUnit unit); | ||
|
||
/** | ||
* Advances the current {@link #nanoTime()} by the given amount of time. | ||
* | ||
* @param amountMillis the number of milliseconds to advance this ticker by. | ||
*/ | ||
default void advanceMillis(long amountMillis) { | ||
advance(amountMillis, TimeUnit.MILLISECONDS); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
common/src/main/java/io/netty5/util/concurrent/SystemTicker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2023 The Netty Project | ||
* | ||
* The Netty Project licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package io.netty5.util.concurrent; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
enum SystemTicker implements Ticker { | ||
INSTANCE; | ||
|
||
static final long START_TIME = System.nanoTime(); | ||
|
||
@Override | ||
public long initialNanoTime() { | ||
return START_TIME; | ||
} | ||
|
||
@Override | ||
public long nanoTime() { | ||
return System.nanoTime() - START_TIME; | ||
} | ||
|
||
@Override | ||
public void sleep(long delay, TimeUnit unit) throws InterruptedException { | ||
Thread.sleep(unit.toMillis(delay), (int) (delay % 1_000_000)); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
} | ||
} |
Oops, something went wrong.
missing toNanos