public FixedIntervalKeepAliveStrategy(Duration keepAliveInterval) { if (keepAliveInterval == null) { keepAliveInMs = -1; } else { keepAliveInMs = (long) keepAliveInterval.toMillis(); } }
@PostConstruct public void start() { if (executor != null) { executor.scheduleWithFixedDelay(new Runnable() { @Override public void run() { refreshKeys(); } }, 0, (long) refreshInterval.toMillis(), TimeUnit.MILLISECONDS); } }
@PostConstruct public void start() { timerService.scheduleWithFixedDelay(new Runnable() { @Override public void run() { try { updateAllCoordinators(); } catch (Throwable e) { log.error(e, "Unexpected exception updating coordinators"); } try { updateAllAgents(); } catch (Throwable e) { log.error(e, "Unexpected exception updating agents"); } } }, 0, (long) statusExpiration.toMillis(), TimeUnit.MILLISECONDS); }
@Config("agent.max-lock-wait") public AgentConfig setMaxLockWait(Duration lockWait) { // TODO: remove once configuration supports bean validation Preconditions.checkNotNull(lockWait, "ttl must not be null"); Preconditions.checkArgument(lockWait.toMillis() > 0, "ttl must be > 0"); this.maxLockWait = lockWait; return this; } }
private void lock() { try { if (!lock.tryLock((long) lockWait.toMillis(), TimeUnit.MILLISECONDS)) { throw new LockTimeoutException(lockOwner, lockWait, lockAcquisitionLocation); } // capture the location where the lock was acquired lockAcquisitionLocation = ImmutableList.copyOf(new Exception("lock acquired HERE").fillInStackTrace().getStackTrace()); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new RuntimeException(e); } }
void waitForListener() { InetSocketAddress testAddress = new InetSocketAddress(rpcAddress, rpcPort); long start = System.nanoTime(); while (Duration.nanosSince(start).toMillis() < TimeUnit.MINUTES.toMillis(1)) { try { Socket testSocket = new Socket(); testSocket.connect(testAddress, (int) TimeUnit.SECONDS.toMillis(1)); testSocket.close(); return; } catch (IOException e) { try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException interrupted) { Thread.currentThread().interrupt(); throw new RuntimeException(interrupted); } } } }
public HttpClientPipelineFactory(NettyConnectionPool nettyConnectionPool, Timer timer, OrderedMemoryAwareThreadPoolExecutor executor, Duration readTimeout, DataSize maxContentLength) { this.timer = timer; Preconditions.checkNotNull(nettyConnectionPool, "nettyConnectionPool is null"); Preconditions.checkNotNull(executor, "executor is null"); Preconditions.checkNotNull(readTimeout, "readTimeout is null"); Preconditions.checkNotNull(maxContentLength, "maxContentLength is null"); this.nettyConnectionPool = nettyConnectionPool; this.executor = executor; this.timeoutHandler = new ReadTimeoutHandler(this.timer, (long) readTimeout.toMillis(), TimeUnit.MILLISECONDS); this.maxContentLength = Ints.checkedCast(maxContentLength.toBytes()); }
/** * Returns a new future that is completed when the supplied future completes or * when the timeout expires. If the timeout occurs or the returned CompletableFuture * is canceled, the supplied future will be canceled. */ public static <T> CompletableFuture<T> addTimeout(CompletableFuture<T> future, Callable<T> onTimeout, Duration timeout, ScheduledExecutorService executorService) { requireNonNull(future, "future is null"); requireNonNull(onTimeout, "timeoutValue is null"); requireNonNull(timeout, "timeout is null"); requireNonNull(executorService, "executorService is null"); // if the future is already complete, just return it if (future.isDone()) { return future; } // create an unmodifiable future that propagates cancel // down cast is safe because this is our code UnmodifiableCompletableFuture<T> futureWithTimeout = (UnmodifiableCompletableFuture<T>) unmodifiableFuture(future, true); // schedule a task to complete the future when the time expires ScheduledFuture<?> timeoutTaskFuture = executorService.schedule(new TimeoutFutureTask<>(futureWithTimeout, onTimeout, future), timeout.toMillis(), MILLISECONDS); // when future completes, cancel the timeout task future.whenCompleteAsync((value, exception) -> timeoutTaskFuture.cancel(false), executorService); return futureWithTimeout; }
public int execute(Executor executor) throws CommandFailedException { Preconditions.checkNotNull(executor, "executor is null"); Preconditions.checkNotNull(command, "command is null"); ProcessCallable processCallable = new ProcessCallable(this, executor); Future<Integer> future = submit(executor, processCallable); try { Integer result = future.get((long) timeLimit.toMillis(), TimeUnit.MILLISECONDS); return result; } catch (ExecutionException e) { Throwables.propagateIfPossible(e.getCause(), CommandFailedException.class); Throwable cause = e.getCause(); if (cause == null) { cause = e; } throw new CommandFailedException(this, "unexpected exception", cause); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new CommandFailedException(this, "interrupted", e); } catch (TimeoutException e) { future.cancel(true); throw new CommandTimeoutException(this); } }
/** * Returns a new future that is completed when the supplied future completes or * when the timeout expires. If the timeout occurs or the returned CompletableFuture * is canceled, the supplied future will be canceled. */ public static <T> ListenableFuture<T> addTimeout(ListenableFuture<T> future, Callable<T> onTimeout, Duration timeout, ScheduledExecutorService executorService) { AsyncFunction<TimeoutException, T> timeoutHandler = timeoutException -> { try { return immediateFuture(onTimeout.call()); } catch (Throwable throwable) { return immediateFailedFuture(throwable); } }; return FluentFuture.from(future) .withTimeout(timeout.toMillis(), MILLISECONDS, executorService) .catchingAsync(TimeoutException.class, timeoutHandler, directExecutor()); }
public AsyncResponseHandler withTimeout(Duration timeout, Supplier<Response> timeoutResponse) { asyncResponse.setTimeoutHandler(asyncResponse -> { asyncResponse.resume(timeoutResponse.get()); cancelFuture(); }); asyncResponse.setTimeout(timeout.toMillis(), MILLISECONDS); return this; }
if (abs(serverTime - millis) > MAX_REQUEST_TIME_SKEW.toMillis()) { sendError(response, BAD_REQUEST, format("Request time too skewed (server time: %s)", serverTime / 1000)); return;
public ApacheHttpClient(HttpClientConfig config, Set<? extends HttpRequestFilter> requestFilters) { Preconditions.checkNotNull(config, "config is null"); Preconditions.checkNotNull(requestFilters, "requestFilters is null"); PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager(); connectionManager.setMaxTotal(config.getMaxConnections()); connectionManager.setDefaultMaxPerRoute(config.getMaxConnectionsPerServer()); BasicHttpParams httpParams = new BasicHttpParams(); httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, (int) config.getReadTimeout().toMillis()); httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, (int) config.getConnectTimeout().toMillis()); httpParams.setParameter(CoreConnectionPNames.SO_LINGER, 0); // do we need this? DefaultHttpClient defaultHttpClient = new DefaultHttpClient(connectionManager, httpParams); defaultHttpClient.setKeepAliveStrategy(new FixedIntervalKeepAliveStrategy(config.getKeepAliveInterval())); defaultHttpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false)); this.httpClient = defaultHttpClient; this.requestFilters = ImmutableList.copyOf(requestFilters); }
long stopTrafficDelay = config.getStopTrafficDelay().toMillis(); if (stopTrafficDelay != 0) { Thread.sleep(stopTrafficDelay);
bootstrap = new Socks4ClientBootstrap(channelFactory, config.getSocksProxy()); bootstrap.setOption("connectTimeoutMillis", (long) config.getConnectTimeout().toMillis()); bootstrap.setOption("soLinger", 0);
http2.setMaxConcurrentStreams(config.getHttp2MaxConcurrentStreams()); http2.setInputBufferSize(toIntExact(config.getHttp2InputBufferSize().toBytes())); http2.setStreamIdleTimeout(config.getHttp2StreamIdleTimeout().toMillis()); connectionFactories.add(http2);
threadPool.setIdleTimeout(Ints.checkedCast(config.getThreadMaxIdleTime().toMillis())); threadPool.setName("http-worker"); server = new Server(threadPool); server.setStopTimeout(config.getStopTimeout().toMillis()); registerErrorHandler = config.isShowStackTrace(); http2c.setMaxConcurrentStreams(config.getHttp2MaxConcurrentStreams()); http2c.setInputBufferSize(toIntExact(config.getHttp2InputBufferSize().toBytes())); http2c.setStreamIdleTimeout(config.getHttp2StreamIdleTimeout().toMillis()); httpConnector = createServerConnector( httpServerInfo.getHttpChannel(), httpConnector.setName("http"); httpConnector.setPort(httpServerInfo.getHttpUri().getPort()); httpConnector.setIdleTimeout(config.getNetworkMaxIdleTime().toMillis()); httpConnector.setHost(nodeInfo.getBindIp().getHostAddress()); httpConnector.setAcceptQueueSize(config.getHttpAcceptQueueSize()); httpsConnector.setName("https"); httpsConnector.setPort(httpServerInfo.getHttpsUri().getPort()); httpsConnector.setIdleTimeout(config.getNetworkMaxIdleTime().toMillis()); httpsConnector.setHost(nodeInfo.getBindIp().getHostAddress()); httpsConnector.setAcceptQueueSize(config.getHttpAcceptQueueSize()); adminThreadPool.setName("http-admin-worker"); adminThreadPool.setMinThreads(config.getAdminMinThreads()); adminThreadPool.setIdleTimeout(Ints.checkedCast(config.getThreadMaxIdleTime().toMillis())); adminConnector.setIdleTimeout(config.getNetworkMaxIdleTime().toMillis());
requestTimeoutMillis = requestTimeout.toMillis(); idleTimeoutMillis = config.getIdleTimeout().toMillis(); httpClient.setConnectTimeout(config.getConnectTimeout().toMillis()); httpClient.setAddressResolutionTimeout(config.getConnectTimeout().toMillis()); long connectTimeout = config.getConnectTimeout().toMillis(); httpClient.setConnectTimeout(connectTimeout); httpClient.setAddressResolutionTimeout(connectTimeout); httpClient.getExecutor(), httpClient.getScheduler(), config.getConnectTimeout().toMillis())); config.getConnectTimeout().toMillis()));