@Override public void run() { timeoutKey = null; if (state.get() == 2) { return; } long time = System.currentTimeMillis(); if (timeout > time) { timeoutKey = connection.getIoThread().executeAfter(this, timeout - time, TimeUnit.MILLISECONDS); return; } if (tryClose()) { activeInvocationCount.decrementAndGet(); runPending(); //needed to avoid a very unlikely race } } };
private void handleResumeTimeout() { Integer timeout = getTimeout(); if (timeout == null || timeout <= 0) { return; } long currentTime = System.currentTimeMillis(); expireTime = currentTime + timeout; XnioExecutor.Key key = handle; if (key == null) { handle = connection.getIoThread().executeAfter(timeoutCommand, timeout, TimeUnit.MILLISECONDS); } } }
private void handleResumeTimeout() { Integer timeout = getTimeout(); if (timeout == null || timeout <= 0) { return; } long currentTime = System.currentTimeMillis(); expireTime = currentTime + timeout; XnioExecutor.Key key = handle; if (key == null) { handle = connection.getIoThread().executeAfter(timeoutCommand, timeout, TimeUnit.MILLISECONDS); } } }
@Override public void run() { sendGoAway(ERROR_NO_ERROR); //just to make sure the connection is actually closed we give it 2 seconds //then we forcibly kill the connection getIoThread().executeAfter(new Runnable() { @Override public void run() { IoUtils.safeClose(Http2Channel.this); } }, 2, TimeUnit.SECONDS); } });
@Override public void done(boolean close) { if (!state.compareAndSet(1, 0)) { return; } if (close) { IoUtils.safeClose(connection); } timeout = System.currentTimeMillis() + connectionIdleTimeout; if (timeoutKey == null && connectionIdleTimeout > 0 && !close) { timeoutKey = connection.getIoThread().executeAfter(timeoutTask, connectionIdleTimeout, TimeUnit.MILLISECONDS); } returnConnection(this); }
@Override public void sendPing(PingListener listener, long timeout, TimeUnit timeUnit) { long count = PING_COUNTER.incrementAndGet(); byte[] data = new byte[8]; data[0] = (byte) count; data[1] = (byte)(count << 8); data[2] = (byte)(count << 16); data[3] = (byte)(count << 24); data[4] = (byte)(count << 32); data[5] = (byte)(count << 40); data[6] = (byte)(count << 48); data[7] = (byte)(count << 54); final PingKey key = new PingKey(data); outstandingPings.put(key, listener); if(timeout > 0) { http2Channel.getIoThread().executeAfter(() -> { PingListener listener1 = outstandingPings.remove(key); if(listener1 != null) { listener1.failed(UndertowMessages.MESSAGES.pingTimeout()); } }, timeout, timeUnit); } http2Channel.sendPing(data, (channel, exception) -> listener.failed(exception)); }
/** * Schedules a task for future execution. If the execution is rejected because the worker is shutting * down then it is logged at debug level and the exception is not re-thrown * @param thread The IO thread * @param task The task to execute * @param timeout The timeout * @param timeUnit The time unit */ public static XnioExecutor.Key executeAfter(XnioIoThread thread, Runnable task, long timeout, TimeUnit timeUnit) { try { return thread.executeAfter(task, timeout, timeUnit); } catch (RejectedExecutionException e) { if(thread.getWorker().isShutdown()) { UndertowLogger.ROOT_LOGGER.debugf(e, "Failed to schedule task %s as worker is shutting down", task); //we just return a bogus key in this case return new XnioExecutor.Key() { @Override public boolean remove() { return false; } }; } else { throw e; } } } }
/** * {@inheritDoc} */ @Override public void handleRequest(HttpServerExchange exchange) throws Exception { // Save the thread/runnable // Keeping a reference to the thread object here does not prevent // GC'ing, as the reference is removed from the Map in the finally clause Long key = Thread.currentThread().getId(); MonitoredThread monitoredThread = new MonitoredThread(Thread.currentThread(), exchange.getRequestURI() + exchange.getQueryString()); activeThreads.put(key, monitoredThread); if(timerKey == null) { synchronized (this) { if(timerKey == null) { timerKey = exchange.getIoThread().executeAfter(stuckThreadTask, 1, TimeUnit.SECONDS); } } } try { next.handleRequest(exchange); } finally { activeThreads.remove(key); if (monitoredThread.markAsDone() == MonitoredThreadState.STUCK) { completedStuckThreadsQueue.add( new CompletedStuckThread(monitoredThread.getThread(), monitoredThread.getActiveTimeInMillis())); } } }
timerCancelKey = executor.executeAfter(cancelTask, (maxInactiveInterval * 1000L) + 1L, TimeUnit.MILLISECONDS);
pingListeners.add(pingListener); getIoThread().executeAfter(() -> { synchronized (pingListeners) { if(pingListeners.contains(pingListener)) {
@Override public void run() { try { task.call(); start = System.nanoTime(); delay = furtherDelay; key = executor.executeAfter(this, furtherDelay, TimeUnit.NANOSECONDS); } catch (Throwable cause) { tryFailure(cause); } }
@Override public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) { FixedScheduledFuture wrapper = new FixedScheduledFuture(command, initialDelay, delay, unit); wrapper.key = executor.executeAfter(wrapper, delay, unit); return wrapper; }
@Override public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) { FixedRateScheduledFuture wrapper = new FixedRateScheduledFuture(command, initialDelay, period, unit); wrapper.key = executor.executeAfter(wrapper, initialDelay, unit); return wrapper; }
@Override public void run() { try { task.call(); start = System.nanoTime(); delay = initialDelay + period * (++count); key = executor.executeAfter(this,delay, TimeUnit.NANOSECONDS); } catch (Throwable cause) { tryFailure(cause); } }
@Override public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) { ScheduledFutureWrapper<V> wrapper = new ScheduledFutureWrapper<V>(callable, delay, unit); wrapper.key = executor.executeAfter(wrapper, delay, unit); return wrapper; }
private void handleResumeTimeout() { Integer timeout = getTimeout(); if (timeout == null || timeout <= 0) { return; } long currentTime = System.currentTimeMillis(); expireTime = currentTime + timeout; XnioExecutor.Key key = handle; if (key == null) { handle = connection.getIoThread().executeAfter(timeoutCommand, timeout, TimeUnit.MILLISECONDS); } } }
private void handleResumeTimeout() { Integer timeout = getTimeout(); if (timeout == null || timeout <= 0) { return; } long currentTime = System.currentTimeMillis(); expireTime = currentTime + timeout; XnioExecutor.Key key = handle; if (key == null) { handle = connection.getIoThread().executeAfter(timeoutCommand, timeout, TimeUnit.MILLISECONDS); } } }
private void handleResumeTimeout() { Integer timeout = getTimeout(); if (timeout == null || timeout <= 0) { return; } long currentTime = System.currentTimeMillis(); expireTime = currentTime + timeout; XnioExecutor.Key key = handle; if (key == null) { handle = connection.getIoThread().executeAfter(timeoutCommand, timeout, TimeUnit.MILLISECONDS); } } }
@Override public void run() { sendGoAway(ERROR_NO_ERROR); //just to make sure the connection is actually closed we give it 2 seconds //then we forcibly kill the connection getIoThread().executeAfter(new Runnable() { @Override public void run() { IoUtils.safeClose(Http2Channel.this); } }, 2, TimeUnit.SECONDS); } });
@Override public void setTimeout(long timeout) { if (!cancelTimeout()) { // the prior timeout has already fired return; } if (timeout > 0) { this.timeoutKey = exchange.getIoThread().executeAfter(closer, timeout, TimeUnit.MILLISECONDS); } }