/* * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.netconf.nettyutil; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkState; import static java.util.Objects.requireNonNull; import io.netty.util.concurrent.EventExecutor; import io.netty.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import org.checkerframework.checker.lock.qual.GuardedBy; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Swiss army knife equivalent for reconnect strategies. This class is thread-safe. * *
* This strategy continues to schedule reconnect attempts, each having to complete in a fixed time (connectTime). * *
* Initial sleep time is specified as minSleep. Each subsequent unsuccessful attempt multiplies this time by a constant * factor (sleepFactor) -- this allows for either constant reconnect times (sleepFactor = 1), or various degrees of * exponential back-off (sleepFactor > 1). Maximum sleep time between attempts can be capped to a specific value * (maxSleep). * *
* The strategy can optionally give up based on two criteria: * *
* A preset number of connection retries (maxAttempts) has been reached, or * *
* A preset absolute deadline is reached (deadline nanoseconds, as reported by System.nanoTime(). In this specific case, * both connectTime and maxSleep will be controlled such that the connection attempt is resolved as closely to the * deadline as possible. * *
* Both these caps can be combined, with the strategy giving up as soon as the first one is reached.
*/
@Deprecated
public final class TimedReconnectStrategy implements ReconnectStrategy {
private static final Logger LOG = LoggerFactory.getLogger(TimedReconnectStrategy.class);
private final EventExecutor executor;
private final Long deadline;
private final Long maxAttempts;
private final Long maxSleep;
private final double sleepFactor;
private final int connectTime;
private final long minSleep;
@GuardedBy("this")
private long attempts;
@GuardedBy("this")
private long lastSleep;
@GuardedBy("this")
private boolean scheduled;
public TimedReconnectStrategy(final EventExecutor executor, final int connectTime, final long minSleep,
final double sleepFactor, final Long maxSleep, final Long maxAttempts, final Long deadline) {
checkArgument(maxSleep == null || minSleep <= maxSleep);
checkArgument(sleepFactor >= 1);
checkArgument(connectTime >= 0);
this.executor = requireNonNull(executor);
this.deadline = deadline;
this.maxAttempts = maxAttempts;
this.minSleep = minSleep;
this.maxSleep = maxSleep;
this.sleepFactor = sleepFactor;
this.connectTime = connectTime;
}
@Override
public synchronized Future