use of io.netty.util.Timeout in project redisson by redisson.
the class CommandAsyncService method handleBlockingOperations.
private <R, V> void handleBlockingOperations(final AsyncDetails<V, R> details, final RedisConnection connection, Long popTimeout) {
final FutureListener<Boolean> listener = new FutureListener<Boolean>() {
@Override
public void operationComplete(Future<Boolean> future) throws Exception {
details.getMainPromise().tryFailure(new RedissonShutdownException("Redisson is shutdown"));
}
};
final AtomicBoolean canceledByScheduler = new AtomicBoolean();
final Timeout scheduledFuture;
if (popTimeout != 0) {
// to handle cases when connection has been lost
final Channel orignalChannel = connection.getChannel();
scheduledFuture = connectionManager.newTimeout(new TimerTask() {
@Override
public void run(Timeout timeout) throws Exception {
// and connection is still active
if (orignalChannel == connection.getChannel() && connection.isActive()) {
return;
}
canceledByScheduler.set(true);
details.getAttemptPromise().trySuccess(null);
}
}, popTimeout, TimeUnit.SECONDS);
} else {
scheduledFuture = null;
}
details.getMainPromise().addListener(new FutureListener<R>() {
@Override
public void operationComplete(Future<R> future) throws Exception {
if (scheduledFuture != null) {
scheduledFuture.cancel();
}
synchronized (listener) {
connectionManager.getShutdownPromise().removeListener(listener);
}
// handling cancel operation for commands from skipTimeout collection
if ((future.isCancelled() && details.getAttemptPromise().cancel(true)) || canceledByScheduler.get()) {
connection.forceFastReconnectAsync();
return;
}
if (future.cause() instanceof RedissonShutdownException) {
details.getAttemptPromise().tryFailure(future.cause());
}
}
});
synchronized (listener) {
if (!details.getMainPromise().isDone()) {
connectionManager.getShutdownPromise().addListener(listener);
}
}
}
use of io.netty.util.Timeout in project redisson by redisson.
the class CommandAsyncService method checkWriteFuture.
private <V, R> void checkWriteFuture(final AsyncDetails<V, R> details, final RedisConnection connection) {
ChannelFuture future = details.getWriteFuture();
if (details.getAttemptPromise().isDone() || future.isCancelled()) {
return;
}
if (!future.isSuccess()) {
details.setException(new WriteRedisConnectionException("Can't write command: " + details.getCommand() + ", params: " + LogHelper.toString(details.getParams()) + " to channel: " + future.channel(), future.cause()));
return;
}
details.getTimeout().cancel();
long timeoutTime = connectionManager.getConfig().getTimeout();
if (RedisCommands.BLOCKING_COMMANDS.contains(details.getCommand().getName())) {
Long popTimeout = Long.valueOf(details.getParams()[details.getParams().length - 1].toString());
handleBlockingOperations(details, connection, popTimeout);
if (popTimeout == 0) {
return;
}
timeoutTime += popTimeout * 1000;
// add 1 second due to issue https://github.com/antirez/redis/issues/874
timeoutTime += 1000;
}
final long timeoutAmount = timeoutTime;
TimerTask timeoutTask = new TimerTask() {
@Override
public void run(Timeout timeout) throws Exception {
details.getAttemptPromise().tryFailure(new RedisTimeoutException("Redis server response timeout (" + timeoutAmount + " ms) occured for command: " + details.getCommand() + " with params: " + LogHelper.toString(details.getParams()) + " channel: " + connection.getChannel()));
}
};
Timeout timeout = connectionManager.newTimeout(timeoutTask, timeoutTime, TimeUnit.MILLISECONDS);
details.setTimeout(timeout);
}
use of io.netty.util.Timeout in project redisson by redisson.
the class ConnectionWatchdog method reconnect.
private void reconnect(final RedisConnection connection, final int attempts) {
int timeout = 2 << attempts;
if (bootstrap.group().isShuttingDown()) {
return;
}
timer.newTimeout(new TimerTask() {
@Override
public void run(Timeout timeout) throws Exception {
tryReconnect(connection, Math.min(BACKOFF_CAP, attempts + 1));
}
}, timeout, TimeUnit.MILLISECONDS);
}
use of io.netty.util.Timeout in project netty-socketio by mrniko.
the class HashedWheelTimeoutScheduler method schedule.
@Override
public void schedule(final SchedulerKey key, final Runnable runnable, long delay, TimeUnit unit) {
Timeout timeout = executorService.newTimeout(new TimerTask() {
@Override
public void run(Timeout timeout) throws Exception {
try {
runnable.run();
} finally {
scheduledFutures.remove(key);
}
}
}, delay, unit);
replaceScheduledFuture(key, timeout);
}
use of io.netty.util.Timeout in project redisson by redisson.
the class QueueTransferTask method scheduleTask.
private void scheduleTask(final Long startTime) {
if (startTime == null) {
return;
}
Timeout oldTimeout = timeoutReference.get();
if (oldTimeout != null) {
oldTimeout.cancel();
timeoutReference.compareAndSet(oldTimeout, null);
}
long delay = startTime - System.currentTimeMillis();
if (delay > 10) {
Timeout timeout = connectionManager.newTimeout(new TimerTask() {
@Override
public void run(Timeout timeout) throws Exception {
pushTask();
}
}, delay, TimeUnit.MILLISECONDS);
timeoutReference.set(timeout);
} else {
pushTask();
}
}
Aggregations