2009-02-05 07:55:14 +01:00
|
|
|
/*
|
2012-06-04 22:31:44 +02:00
|
|
|
* Copyright 2012 The Netty Project
|
2009-02-05 07:55:14 +01:00
|
|
|
*
|
2011-12-09 06:18:34 +01:00
|
|
|
* 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:
|
2009-02-05 07:55:14 +01:00
|
|
|
*
|
2020-10-23 14:44:18 +02:00
|
|
|
* https://www.apache.org/licenses/LICENSE-2.0
|
2009-02-05 07:55:14 +01:00
|
|
|
*
|
2009-08-28 09:15:49 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
2011-12-09 06:18:34 +01:00
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
2009-08-28 09:15:49 +02:00
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2009-02-05 07:55:14 +01:00
|
|
|
*/
|
2011-12-09 04:38:59 +01:00
|
|
|
package io.netty.handler.timeout;
|
2009-02-05 07:55:14 +01:00
|
|
|
|
2011-12-09 04:38:59 +01:00
|
|
|
import io.netty.bootstrap.ServerBootstrap;
|
2012-12-19 21:08:47 +01:00
|
|
|
import io.netty.channel.Channel;
|
2019-03-28 10:28:27 +01:00
|
|
|
import io.netty.channel.ChannelHandler;
|
2011-12-09 04:38:59 +01:00
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
2012-12-19 21:08:47 +01:00
|
|
|
import io.netty.channel.ChannelInitializer;
|
Clean up Future/Promises API (#11575)
Motivation:
The generics for the existing futures, promises, and listeners are too complicated.
This complication comes from the existence of `ChannelPromise` and `ChannelFuture`, which forces listeners to care about the particular _type_ of future being listened on.
Modification:
* Add a `FutureContextListener` which can take a context object as an additional argument. This allows our listeners to have the channel piped through to them, so they don't need to rely on the `ChannelFuture.channel()` method.
* Make the `FutureListener`, along with the `FutureContextListener` sibling, the default listener API, retiring the `GenericFutureListener` since we no longer need to abstract over the type of the future.
* Change all uses of `ChannelPromise` to `Promise<Void>`.
* Change all uses of `ChannelFuture` to `Future<Void>`.
* Change all uses of `GenericFutureListener` to either `FutureListener` or `FutureContextListener` as needed.
* Remove `ChannelFutureListener` and `GenericFutureListener`.
* Introduce a `ChannelFutureListeners` enum to house the constants that previously lived in `ChannelFutureListener`. These constants now implement `FutureContextListener` and take the `Channel` as a context.
* Remove `ChannelPromise` and `ChannelFuture` — all usages now rely on the plain `Future` and `Promise` APIs.
* Add static factory methods to `DefaultPromise` that allow us to create promises that are initialised as successful or failed.
* Remove `CompleteFuture`, `SucceededFuture`, `FailedFuture`, `CompleteChannelFuture`, `SucceededChannelFuture`, and `FailedChannelFuture`.
* Remove `ChannelPromiseNotifier`.
Result:
Cleaner generics and more straight forward code.
2021-08-20 09:55:16 +02:00
|
|
|
import io.netty.util.concurrent.Future;
|
|
|
|
import io.netty.util.concurrent.FutureListener;
|
2012-06-01 00:53:38 +02:00
|
|
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
2009-02-05 07:55:14 +01:00
|
|
|
|
Clean up Future/Promises API (#11575)
Motivation:
The generics for the existing futures, promises, and listeners are too complicated.
This complication comes from the existence of `ChannelPromise` and `ChannelFuture`, which forces listeners to care about the particular _type_ of future being listened on.
Modification:
* Add a `FutureContextListener` which can take a context object as an additional argument. This allows our listeners to have the channel piped through to them, so they don't need to rely on the `ChannelFuture.channel()` method.
* Make the `FutureListener`, along with the `FutureContextListener` sibling, the default listener API, retiring the `GenericFutureListener` since we no longer need to abstract over the type of the future.
* Change all uses of `ChannelPromise` to `Promise<Void>`.
* Change all uses of `ChannelFuture` to `Future<Void>`.
* Change all uses of `GenericFutureListener` to either `FutureListener` or `FutureContextListener` as needed.
* Remove `ChannelFutureListener` and `GenericFutureListener`.
* Introduce a `ChannelFutureListeners` enum to house the constants that previously lived in `ChannelFutureListener`. These constants now implement `FutureContextListener` and take the `Channel` as a context.
* Remove `ChannelPromise` and `ChannelFuture` — all usages now rely on the plain `Future` and `Promise` APIs.
* Add static factory methods to `DefaultPromise` that allow us to create promises that are initialised as successful or failed.
* Remove `CompleteFuture`, `SucceededFuture`, `FailedFuture`, `CompleteChannelFuture`, `SucceededChannelFuture`, and `FailedChannelFuture`.
* Remove `ChannelPromiseNotifier`.
Result:
Cleaner generics and more straight forward code.
2021-08-20 09:55:16 +02:00
|
|
|
import static java.util.Objects.requireNonNull;
|
|
|
|
|
2009-02-05 07:55:14 +01:00
|
|
|
/**
|
2015-12-28 09:33:19 +01:00
|
|
|
* Raises a {@link WriteTimeoutException} when a write operation cannot finish in a certain period of time.
|
2009-06-19 13:33:38 +02:00
|
|
|
*
|
2009-09-04 05:24:03 +02:00
|
|
|
* <pre>
|
2015-12-28 09:33:19 +01:00
|
|
|
* // The connection is closed when a write operation cannot finish in 30 seconds.
|
2010-02-02 01:38:07 +01:00
|
|
|
*
|
2014-11-28 19:46:30 +01:00
|
|
|
* public class MyChannelInitializer extends {@link ChannelInitializer}<{@link Channel}> {
|
2012-12-19 21:08:47 +01:00
|
|
|
* public void initChannel({@link Channel} channel) {
|
2012-12-20 15:45:49 +01:00
|
|
|
* channel.pipeline().addLast("writeTimeoutHandler", new {@link WriteTimeoutHandler}(30);
|
|
|
|
* channel.pipeline().addLast("myHandler", new MyHandler());
|
2010-02-02 01:38:07 +01:00
|
|
|
* }
|
2012-12-19 21:08:47 +01:00
|
|
|
* }
|
2009-09-04 05:24:03 +02:00
|
|
|
*
|
2012-12-19 21:08:47 +01:00
|
|
|
* // Handler should handle the {@link WriteTimeoutException}.
|
2019-03-28 10:28:27 +01:00
|
|
|
* public class MyHandler implements {@link ChannelHandler} {
|
2012-12-19 21:08:47 +01:00
|
|
|
* {@code @Override}
|
|
|
|
* public void exceptionCaught({@link ChannelHandlerContext} ctx, {@link Throwable} cause)
|
|
|
|
* throws {@link Exception} {
|
|
|
|
* if (cause instanceof {@link WriteTimeoutException}) {
|
|
|
|
* // do something
|
|
|
|
* } else {
|
|
|
|
* super.exceptionCaught(ctx, cause);
|
|
|
|
* }
|
2010-02-02 01:38:07 +01:00
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
2010-02-02 03:00:04 +01:00
|
|
|
* {@link ServerBootstrap} bootstrap = ...;
|
2010-02-02 01:38:07 +01:00
|
|
|
* ...
|
2012-12-19 21:08:47 +01:00
|
|
|
* bootstrap.childHandler(new MyChannelInitializer());
|
|
|
|
* ...
|
2009-09-04 05:24:03 +02:00
|
|
|
* </pre>
|
2009-06-19 13:33:38 +02:00
|
|
|
* @see ReadTimeoutHandler
|
|
|
|
* @see IdleStateHandler
|
2009-02-05 07:55:14 +01:00
|
|
|
*/
|
2019-03-28 10:28:27 +01:00
|
|
|
public class WriteTimeoutHandler implements ChannelHandler {
|
2014-03-14 08:47:18 +01:00
|
|
|
private static final long MIN_TIMEOUT_NANOS = TimeUnit.MILLISECONDS.toNanos(1);
|
2009-02-05 07:55:14 +01:00
|
|
|
|
2014-03-14 08:47:18 +01:00
|
|
|
private final long timeoutNanos;
|
2009-02-05 07:55:14 +01:00
|
|
|
|
2015-12-28 09:33:19 +01:00
|
|
|
/**
|
|
|
|
* A doubly-linked list to track all WriteTimeoutTasks
|
|
|
|
*/
|
|
|
|
private WriteTimeoutTask lastTask;
|
|
|
|
|
2012-06-01 00:53:38 +02:00
|
|
|
private boolean closed;
|
|
|
|
|
2009-06-19 13:33:38 +02:00
|
|
|
/**
|
|
|
|
* Creates a new instance.
|
|
|
|
*
|
|
|
|
* @param timeoutSeconds
|
|
|
|
* write timeout in seconds
|
|
|
|
*/
|
2012-06-01 00:53:38 +02:00
|
|
|
public WriteTimeoutHandler(int timeoutSeconds) {
|
|
|
|
this(timeoutSeconds, TimeUnit.SECONDS);
|
2009-02-11 06:23:05 +01:00
|
|
|
}
|
|
|
|
|
2009-06-19 13:33:38 +02:00
|
|
|
/**
|
|
|
|
* Creates a new instance.
|
|
|
|
*
|
|
|
|
* @param timeout
|
|
|
|
* write timeout
|
|
|
|
* @param unit
|
|
|
|
* the {@link TimeUnit} of {@code timeout}
|
|
|
|
*/
|
2012-06-01 00:53:38 +02:00
|
|
|
public WriteTimeoutHandler(long timeout, TimeUnit unit) {
|
2019-02-04 10:32:25 +01:00
|
|
|
requireNonNull(unit, "unit");
|
2009-02-05 07:55:14 +01:00
|
|
|
|
2009-09-04 09:19:32 +02:00
|
|
|
if (timeout <= 0) {
|
2014-03-14 08:47:18 +01:00
|
|
|
timeoutNanos = 0;
|
2009-09-04 09:19:32 +02:00
|
|
|
} else {
|
2014-03-14 08:47:18 +01:00
|
|
|
timeoutNanos = Math.max(unit.toNanos(timeout), MIN_TIMEOUT_NANOS);
|
2009-09-04 09:19:32 +02:00
|
|
|
}
|
2009-02-05 07:55:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-08-25 14:12:33 +02:00
|
|
|
public Future<Void> write(ChannelHandlerContext ctx, Object msg) {
|
|
|
|
Future<Void> f = ctx.write(msg);
|
2014-07-02 10:39:34 +02:00
|
|
|
if (timeoutNanos > 0) {
|
2021-08-25 14:12:33 +02:00
|
|
|
scheduleTimeout(ctx, f);
|
2014-07-02 10:39:34 +02:00
|
|
|
}
|
2021-08-25 14:12:33 +02:00
|
|
|
return f;
|
2012-12-19 21:16:41 +01:00
|
|
|
}
|
|
|
|
|
2015-12-28 09:33:19 +01:00
|
|
|
@Override
|
|
|
|
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
|
2021-03-04 15:27:02 +01:00
|
|
|
assert ctx.executor().inEventLoop();
|
2015-12-28 09:33:19 +01:00
|
|
|
WriteTimeoutTask task = lastTask;
|
|
|
|
lastTask = null;
|
|
|
|
while (task != null) {
|
2021-03-04 15:27:02 +01:00
|
|
|
assert task.ctx.executor().inEventLoop();
|
2015-12-28 09:33:19 +01:00
|
|
|
task.scheduledFuture.cancel(false);
|
|
|
|
WriteTimeoutTask prev = task.prev;
|
|
|
|
task.prev = null;
|
|
|
|
task.next = null;
|
|
|
|
task = prev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-25 14:12:33 +02:00
|
|
|
private void scheduleTimeout(final ChannelHandlerContext ctx, final Future<Void> future) {
|
2014-07-02 10:39:34 +02:00
|
|
|
// Schedule a timeout.
|
2021-08-25 14:12:33 +02:00
|
|
|
final WriteTimeoutTask task = new WriteTimeoutTask(ctx, future);
|
2015-12-28 09:33:19 +01:00
|
|
|
task.scheduledFuture = ctx.executor().schedule(task, timeoutNanos, TimeUnit.NANOSECONDS);
|
|
|
|
|
|
|
|
if (!task.scheduledFuture.isDone()) {
|
|
|
|
addWriteTimeoutTask(task);
|
|
|
|
|
|
|
|
// Cancel the scheduled timeout if the flush promise is complete.
|
2021-08-25 14:12:33 +02:00
|
|
|
future.addListener(task);
|
2015-12-28 09:33:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void addWriteTimeoutTask(WriteTimeoutTask task) {
|
2021-03-04 15:27:02 +01:00
|
|
|
assert task.ctx.executor().inEventLoop();
|
2018-04-11 14:36:57 +02:00
|
|
|
if (lastTask != null) {
|
2015-12-28 09:33:19 +01:00
|
|
|
lastTask.next = task;
|
|
|
|
task.prev = lastTask;
|
|
|
|
}
|
2018-04-11 14:36:57 +02:00
|
|
|
lastTask = task;
|
2015-12-28 09:33:19 +01:00
|
|
|
}
|
2009-02-11 05:59:45 +01:00
|
|
|
|
2015-12-28 09:33:19 +01:00
|
|
|
private void removeWriteTimeoutTask(WriteTimeoutTask task) {
|
2021-03-04 15:27:02 +01:00
|
|
|
assert task.ctx.executor().inEventLoop();
|
2015-12-28 09:33:19 +01:00
|
|
|
if (task == lastTask) {
|
|
|
|
// task is the tail of list
|
|
|
|
assert task.next == null;
|
|
|
|
lastTask = lastTask.prev;
|
|
|
|
if (lastTask != null) {
|
|
|
|
lastTask.next = null;
|
2014-07-02 10:39:34 +02:00
|
|
|
}
|
2015-12-28 09:33:19 +01:00
|
|
|
} else if (task.prev == null && task.next == null) {
|
|
|
|
// Since task is not lastTask, then it has been removed or not been added.
|
|
|
|
return;
|
|
|
|
} else if (task.prev == null) {
|
|
|
|
// task is the head of list and the list has at least 2 nodes
|
|
|
|
task.next.prev = null;
|
|
|
|
} else {
|
|
|
|
task.prev.next = task.next;
|
|
|
|
task.next.prev = task.prev;
|
|
|
|
}
|
|
|
|
task.prev = null;
|
|
|
|
task.next = null;
|
2012-06-01 00:53:38 +02:00
|
|
|
}
|
2009-06-19 13:17:38 +02:00
|
|
|
|
2013-07-12 15:43:50 +02:00
|
|
|
/**
|
|
|
|
* Is called when a write timeout was detected
|
|
|
|
*/
|
2012-06-01 00:53:38 +02:00
|
|
|
protected void writeTimedOut(ChannelHandlerContext ctx) throws Exception {
|
|
|
|
if (!closed) {
|
|
|
|
ctx.fireExceptionCaught(WriteTimeoutException.INSTANCE);
|
|
|
|
ctx.close();
|
|
|
|
closed = true;
|
2009-03-04 15:08:31 +01:00
|
|
|
}
|
|
|
|
}
|
2015-12-28 09:33:19 +01:00
|
|
|
|
Clean up Future/Promises API (#11575)
Motivation:
The generics for the existing futures, promises, and listeners are too complicated.
This complication comes from the existence of `ChannelPromise` and `ChannelFuture`, which forces listeners to care about the particular _type_ of future being listened on.
Modification:
* Add a `FutureContextListener` which can take a context object as an additional argument. This allows our listeners to have the channel piped through to them, so they don't need to rely on the `ChannelFuture.channel()` method.
* Make the `FutureListener`, along with the `FutureContextListener` sibling, the default listener API, retiring the `GenericFutureListener` since we no longer need to abstract over the type of the future.
* Change all uses of `ChannelPromise` to `Promise<Void>`.
* Change all uses of `ChannelFuture` to `Future<Void>`.
* Change all uses of `GenericFutureListener` to either `FutureListener` or `FutureContextListener` as needed.
* Remove `ChannelFutureListener` and `GenericFutureListener`.
* Introduce a `ChannelFutureListeners` enum to house the constants that previously lived in `ChannelFutureListener`. These constants now implement `FutureContextListener` and take the `Channel` as a context.
* Remove `ChannelPromise` and `ChannelFuture` — all usages now rely on the plain `Future` and `Promise` APIs.
* Add static factory methods to `DefaultPromise` that allow us to create promises that are initialised as successful or failed.
* Remove `CompleteFuture`, `SucceededFuture`, `FailedFuture`, `CompleteChannelFuture`, `SucceededChannelFuture`, and `FailedChannelFuture`.
* Remove `ChannelPromiseNotifier`.
Result:
Cleaner generics and more straight forward code.
2021-08-20 09:55:16 +02:00
|
|
|
private final class WriteTimeoutTask implements Runnable, FutureListener<Void> {
|
2015-12-28 09:33:19 +01:00
|
|
|
|
|
|
|
private final ChannelHandlerContext ctx;
|
2021-08-25 14:12:33 +02:00
|
|
|
private final Future<Void> future;
|
2015-12-28 09:33:19 +01:00
|
|
|
|
|
|
|
// WriteTimeoutTask is also a node of a doubly-linked list
|
|
|
|
WriteTimeoutTask prev;
|
|
|
|
WriteTimeoutTask next;
|
|
|
|
|
2021-08-31 16:06:34 +02:00
|
|
|
Future<?> scheduledFuture;
|
2021-08-25 14:12:33 +02:00
|
|
|
WriteTimeoutTask(ChannelHandlerContext ctx, Future<Void> future) {
|
2015-12-28 09:33:19 +01:00
|
|
|
this.ctx = ctx;
|
2021-08-25 14:12:33 +02:00
|
|
|
this.future = future;
|
2015-12-28 09:33:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
// Was not written yet so issue a write timeout
|
|
|
|
// The promise itself will be failed with a ClosedChannelException once the close() was issued
|
|
|
|
// See https://github.com/netty/netty/issues/2159
|
2021-08-25 14:12:33 +02:00
|
|
|
if (!future.isDone()) {
|
2015-12-28 09:33:19 +01:00
|
|
|
try {
|
|
|
|
writeTimedOut(ctx);
|
|
|
|
} catch (Throwable t) {
|
|
|
|
ctx.fireExceptionCaught(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
removeWriteTimeoutTask(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
Clean up Future/Promises API (#11575)
Motivation:
The generics for the existing futures, promises, and listeners are too complicated.
This complication comes from the existence of `ChannelPromise` and `ChannelFuture`, which forces listeners to care about the particular _type_ of future being listened on.
Modification:
* Add a `FutureContextListener` which can take a context object as an additional argument. This allows our listeners to have the channel piped through to them, so they don't need to rely on the `ChannelFuture.channel()` method.
* Make the `FutureListener`, along with the `FutureContextListener` sibling, the default listener API, retiring the `GenericFutureListener` since we no longer need to abstract over the type of the future.
* Change all uses of `ChannelPromise` to `Promise<Void>`.
* Change all uses of `ChannelFuture` to `Future<Void>`.
* Change all uses of `GenericFutureListener` to either `FutureListener` or `FutureContextListener` as needed.
* Remove `ChannelFutureListener` and `GenericFutureListener`.
* Introduce a `ChannelFutureListeners` enum to house the constants that previously lived in `ChannelFutureListener`. These constants now implement `FutureContextListener` and take the `Channel` as a context.
* Remove `ChannelPromise` and `ChannelFuture` — all usages now rely on the plain `Future` and `Promise` APIs.
* Add static factory methods to `DefaultPromise` that allow us to create promises that are initialised as successful or failed.
* Remove `CompleteFuture`, `SucceededFuture`, `FailedFuture`, `CompleteChannelFuture`, `SucceededChannelFuture`, and `FailedChannelFuture`.
* Remove `ChannelPromiseNotifier`.
Result:
Cleaner generics and more straight forward code.
2021-08-20 09:55:16 +02:00
|
|
|
public void operationComplete(Future<? extends Void> future) throws Exception {
|
2015-12-28 09:33:19 +01:00
|
|
|
// scheduledFuture has already be set when reaching here
|
|
|
|
scheduledFuture.cancel(false);
|
2021-03-04 15:27:02 +01:00
|
|
|
|
|
|
|
// Check if its safe to modify the "doubly-linked-list" that we maintain. If its not we will schedule the
|
|
|
|
// modification so its picked up by the executor..
|
|
|
|
if (ctx.executor().inEventLoop()) {
|
|
|
|
removeWriteTimeoutTask(this);
|
|
|
|
} else {
|
|
|
|
// So let's just pass outself to the executor which will then take care of remove this task
|
|
|
|
// from the doubly-linked list. Schedule ourself is fine as the promise itself is done.
|
|
|
|
//
|
|
|
|
// This fixes https://github.com/netty/netty/issues/11053
|
2021-08-25 14:12:33 +02:00
|
|
|
assert future.isDone();
|
2021-03-04 15:27:02 +01:00
|
|
|
ctx.executor().execute(this);
|
|
|
|
}
|
2015-12-28 09:33:19 +01:00
|
|
|
}
|
|
|
|
}
|
2009-02-05 07:55:14 +01:00
|
|
|
}
|