Search in sources :

Example 6 with Timeout

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);
        }
    }
}
Also used : FutureListener(io.netty.util.concurrent.FutureListener) ChannelFutureListener(io.netty.channel.ChannelFutureListener) Timeout(io.netty.util.Timeout) Channel(io.netty.channel.Channel) RedisAskException(org.redisson.client.RedisAskException) RedisLoadingException(org.redisson.client.RedisLoadingException) RedisTimeoutException(org.redisson.client.RedisTimeoutException) RedisException(org.redisson.client.RedisException) RedisMovedException(org.redisson.client.RedisMovedException) WriteRedisConnectionException(org.redisson.client.WriteRedisConnectionException) RedisTryAgainException(org.redisson.client.RedisTryAgainException) RedissonShutdownException(org.redisson.RedissonShutdownException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TimerTask(io.netty.util.TimerTask) RFuture(org.redisson.api.RFuture) ChannelFuture(io.netty.channel.ChannelFuture) Future(io.netty.util.concurrent.Future) RedissonShutdownException(org.redisson.RedissonShutdownException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 7 with Timeout

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);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) WriteRedisConnectionException(org.redisson.client.WriteRedisConnectionException) TimerTask(io.netty.util.TimerTask) RedisTimeoutException(org.redisson.client.RedisTimeoutException) Timeout(io.netty.util.Timeout)

Example 8 with 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);
}
Also used : TimerTask(io.netty.util.TimerTask) Timeout(io.netty.util.Timeout) RedisException(org.redisson.client.RedisException)

Example 9 with Timeout

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);
}
Also used : TimerTask(io.netty.util.TimerTask) Timeout(io.netty.util.Timeout)

Example 10 with 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();
    }
}
Also used : TimerTask(io.netty.util.TimerTask) Timeout(io.netty.util.Timeout)

Aggregations

Timeout (io.netty.util.Timeout)30 TimerTask (io.netty.util.TimerTask)26 AtomicReference (java.util.concurrent.atomic.AtomicReference)9 Future (io.netty.util.concurrent.Future)8 FutureListener (io.netty.util.concurrent.FutureListener)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 RFuture (org.redisson.api.RFuture)8 AtomicLong (java.util.concurrent.atomic.AtomicLong)6 RedisTimeoutException (org.redisson.client.RedisTimeoutException)5 WriteRedisConnectionException (org.redisson.client.WriteRedisConnectionException)5 ByteBuf (io.netty.buffer.ByteBuf)4 ChannelFuture (io.netty.channel.ChannelFuture)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 ArrayList (java.util.ArrayList)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 RedisAskException (org.redisson.client.RedisAskException)3 RedisException (org.redisson.client.RedisException)3 RedisLoadingException (org.redisson.client.RedisLoadingException)3 RedisMovedException (org.redisson.client.RedisMovedException)3 RedisTryAgainException (org.redisson.client.RedisTryAgainException)3