Skip to content

Commit bc9c3e6

Browse files
authored
Empty stacktrace of TimeoutException (#202)
Empty stacktrace of `TimeoutException` #### Problem `TimeoutPublisher` creates a new `TimeoutException` instance for every timeout. This is costly when the system is under load and has a lot of timers that timeout, which is typical when handling latent outbound services. #### Modification Used a cached `TimeoutException` instance and empty the stacktrace of the exception. #### Result Less overhead of timeouts.
1 parent 7f3cf71 commit bc9c3e6

File tree

1 file changed

+11
-1
lines changed
  • reactivesocket-publishers/src/main/java/io/reactivesocket/reactivestreams/extensions/internal/publishers

1 file changed

+11
-1
lines changed

reactivesocket-publishers/src/main/java/io/reactivesocket/reactivestreams/extensions/internal/publishers/TimeoutPublisher.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@
2828

2929
public final class TimeoutPublisher<T> implements Px<T> {
3030

31+
@SuppressWarnings("ThrowableInstanceNeverThrown")
32+
private static final TimeoutException timeoutException = new TimeoutException() {
33+
private static final long serialVersionUID = 6195545973881750858L;
34+
35+
@Override
36+
public synchronized Throwable fillInStackTrace() {
37+
return this;
38+
}
39+
};
40+
3141
private final Publisher<T> child;
3242
private final Scheduler scheduler;
3343
private final long timeout;
@@ -42,7 +52,7 @@ public TimeoutPublisher(Publisher<T> child, long timeout, TimeUnit unit, Schedul
4252

4353
@Override
4454
public void subscribe(Subscriber<? super T> subscriber) {
45-
Runnable onTimeout = () -> subscriber.onError(new TimeoutException());
55+
Runnable onTimeout = () -> subscriber.onError(timeoutException);
4656
CancellableSubscriber<Void> timeoutSub = Subscribers.create(null, null, throwable -> onTimeout.run(),
4757
onTimeout, null);
4858
Runnable cancelTimeout = () -> timeoutSub.cancel();

0 commit comments

Comments
 (0)