Java Code Examples for io.netty.util.concurrent.EventExecutor#schedule()
The following examples show how to use
io.netty.util.concurrent.EventExecutor#schedule() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SimpleNonceManager.java From quarkus-http with Apache License 2.0 | 6 votes |
private boolean addInvalidNonce(final Nonce nonce, final EventExecutor executor) { long now = System.currentTimeMillis(); long invalidBefore = now - firstUseTimeOut; long timeTillInvalid = nonce.timeStamp - invalidBefore; if (timeTillInvalid > 0) { if (invalidNonces.add(nonce.nonce)) { executor.schedule(new InvalidNonceCleaner(nonce.nonce), timeTillInvalid, TimeUnit.MILLISECONDS); return true; } else { return false; } } else { // So close to expiring any record of this nonce being used could have been cleared so // don't take a chance and just say no. return false; } }
Example 2
Source File: InflightResender.java From cassandana with Apache License 2.0 | 6 votes |
private void initialize(ChannelHandlerContext ctx) { // Avoid the case where destroy() is called before scheduling timeouts. // See: https://github.com/netty/netty/issues/143 if (LOG.isDebugEnabled()) { LOG.debug("Initializing autoflush handler on channel {}", ctx.channel()); } switch (state) { case 1: case 2: return; } state = 1; EventExecutor loop = ctx.executor(); lastExecutionTime = System.nanoTime(); resenderTimeout = loop.schedule(new WriterIdleTimeoutTask(ctx), resenderTimeNanos, TimeUnit.NANOSECONDS); }
Example 3
Source File: AutoFlushHandler.java From cassandana with Apache License 2.0 | 6 votes |
private void initialize(ChannelHandlerContext ctx) { // Avoid the case where destroy() is called before scheduling timeouts. // See: https://github.com/netty/netty/issues/143 if (LOG.isDebugEnabled()) { LOG.debug("Initializing autoflush handler on channel {} Cid: {}", ctx.channel(), NettyUtils.clientID(ctx.channel())); } switch (state) { case 1: case 2: return; } state = 1; EventExecutor loop = ctx.executor(); lastWriteTime = System.nanoTime(); writerIdleTimeout = loop.schedule(new WriterIdleTimeoutTask(ctx), writerIdleTimeNanos, TimeUnit.NANOSECONDS); }
Example 4
Source File: MqttAutoFlushChannelHandler.java From spring-boot-protocol with Apache License 2.0 | 6 votes |
private void initialize(ChannelHandlerContext ctx) { // Avoid the case where destroy() is called before scheduling timeouts. // See: https://github.com/netty/netty/issues/143 if (logger.isTraceEnabled()) { logger.trace("Initializing autoflush handler on channel {} Cid: {}", ctx.channel(), MqttUtil.clientID(ctx.channel())); } switch (state) { case 1: case 2: return; default: break; } state = 1; EventExecutor loop = ctx.executor(); lastWriteTime = System.nanoTime(); writerIdleTimeout = loop.schedule(new WriterIdleTimeoutTask(ctx), writerIdleTimeNanos, TimeUnit.NANOSECONDS); }
Example 5
Source File: MqttInflightResenderChannelHandler.java From spring-boot-protocol with Apache License 2.0 | 6 votes |
private void initialize(ChannelHandlerContext ctx) { // Avoid the case where destroy() is called before scheduling timeouts. // See: https://github.com/netty/netty/issues/143 if (logger.isDebugEnabled()) { logger.debug("Initializing autoflush handler on channel {}", ctx.channel()); } switch (state) { case 1: case 2: return; default: break; } state = 1; EventExecutor loop = ctx.executor(); lastExecutionTime = System.nanoTime(); resenderTimeout = loop.schedule(new WriterIdleTimeoutTask(ctx), resenderTimeNanos, TimeUnit.NANOSECONDS); }
Example 6
Source File: IdleStateHandler.java From netty4.0.27Learn with Apache License 2.0 | 5 votes |
private void initialize(ChannelHandlerContext ctx) { // Avoid the case where destroy() is called before scheduling timeouts. // See: https://github.com/netty/netty/issues/143 switch (state) { case 1: case 2: return; } state = 1; EventExecutor loop = ctx.executor(); lastReadTime = lastWriteTime = System.nanoTime(); if (readerIdleTimeNanos > 0) { readerIdleTimeout = loop.schedule( new ReaderIdleTimeoutTask(ctx), readerIdleTimeNanos, TimeUnit.NANOSECONDS); } if (writerIdleTimeNanos > 0) { writerIdleTimeout = loop.schedule( new WriterIdleTimeoutTask(ctx), writerIdleTimeNanos, TimeUnit.NANOSECONDS); } if (allIdleTimeNanos > 0) { allIdleTimeout = loop.schedule( new AllIdleTimeoutTask(ctx), allIdleTimeNanos, TimeUnit.NANOSECONDS); } }
Example 7
Source File: SimpleNonceManager.java From quarkus-http with Apache License 2.0 | 4 votes |
private boolean validateNonceWithCount(Nonce nonce, int nonceCount, final EventExecutor executor) { // This point could have been reached either because the knownNonces map contained the key or because // it didn't and a count was supplied - either way need to double check the contents of knownNonces once // the lock is in place. synchronized (knownNonces) { Nonce value = knownNonces.get(nonce.nonce); long now = System.currentTimeMillis(); // For the purpose of this validation we also add the cacheTimePostExpiry - when nextNonce is subsequently // called it will decide if we are in the interval to replace the nonce. long earliestAccepted = now - (overallTimeOut + cacheTimePostExpiry); if (value == null) { if (nonce.timeStamp < 0) { // Means it was in there, now it isn't - most likely a timestamp expiration mid check - abandon validation. return false; } if (nonce.timeStamp > earliestAccepted && nonce.timeStamp <= now) { knownNonces.put(nonce.nonce, nonce); long timeTillExpiry = nonce.timeStamp - earliestAccepted; nonce.executorKey = executor.schedule(new KnownNonceCleaner(nonce.nonce), timeTillExpiry, TimeUnit.MILLISECONDS); return true; } return false; } else { // We have it, just need to verify that it has not expired and that the nonce key is valid. if (value.timeStamp < earliestAccepted || value.timeStamp > now) { // The embedded timestamp is either expired or somehow is after now!! return false; } if (value.getMaxNonceCount() < nonceCount) { value.setMaxNonceCount(nonceCount); return true; } return false; } } }
Example 8
Source File: RequestContextTest.java From armeria with Apache License 2.0 | 4 votes |
@Test public void contextAwareEventExecutor() throws Exception { when(channel.eventLoop()).thenReturn(eventLoop.get()); final RequestContext context = createContext(); final Set<Integer> callbacksCalled = Collections.newSetFromMap(new ConcurrentHashMap<>()); final EventExecutor executor = context.contextAwareEventLoop(); final CountDownLatch latch = new CountDownLatch(18); executor.execute(() -> checkCallback(1, context, callbacksCalled, latch)); executor.schedule(() -> checkCallback(2, context, callbacksCalled, latch), 0, TimeUnit.SECONDS); executor.schedule(() -> { checkCallback(2, context, callbacksCalled, latch); return "success"; }, 0, TimeUnit.SECONDS); executor.scheduleAtFixedRate(() -> checkCallback(3, context, callbacksCalled, latch), 0, 1000, TimeUnit.SECONDS); executor.scheduleWithFixedDelay(() -> checkCallback(4, context, callbacksCalled, latch), 0, 1000, TimeUnit.SECONDS); executor.submit(() -> checkCallback(5, context, callbacksCalled, latch)); executor.submit(() -> checkCallback(6, context, callbacksCalled, latch), "success"); executor.submit(() -> { checkCallback(7, context, callbacksCalled, latch); return "success"; }); executor.invokeAll(makeTaskList(8, 10, context, callbacksCalled, latch)); executor.invokeAll(makeTaskList(11, 12, context, callbacksCalled, latch), 10000, TimeUnit.SECONDS); executor.invokeAny(makeTaskList(13, 13, context, callbacksCalled, latch)); executor.invokeAny(makeTaskList(14, 14, context, callbacksCalled, latch), 10000, TimeUnit.SECONDS); final Promise<String> promise = executor.newPromise(); promise.addListener(f -> checkCallback(15, context, callbacksCalled, latch)); promise.setSuccess("success"); executor.newSucceededFuture("success") .addListener(f -> checkCallback(16, context, callbacksCalled, latch)); executor.newFailedFuture(new IllegalArgumentException()) .addListener(f -> checkCallback(17, context, callbacksCalled, latch)); final ProgressivePromise<String> progressivePromise = executor.newProgressivePromise(); progressivePromise.addListener(f -> checkCallback(18, context, callbacksCalled, latch)); progressivePromise.setSuccess("success"); latch.await(); eventLoop.get().shutdownGracefully(); await().untilAsserted(() -> { assertThat(callbacksCalled).containsExactlyElementsOf(IntStream.rangeClosed(1, 18) .boxed()::iterator); }); }
Example 9
Source File: Http2ConnectionCloseHandler.java From zuul with Apache License 2.0 | 4 votes |
/** * WARNING: Found the OkHttp client gets confused by this behaviour (it ends up putting itself in a bad shutdown state * after receiving the first goaway frame, and then dropping any inflight responses but also timing out waiting for them). * * And worried that other http/2 stacks may be similar, so for now we should NOT use this. * * This is unfortunate, as FTL wanted this, and it is correct according to the spec. * * See this code in okhttp where it drops response header frame if state is already shutdown: * https://github.com/square/okhttp/blob/master/okhttp/src/main/java/okhttp3/internal/http2/Http2Connection.java#L609 */ private void gracefullyWithDelay(EventExecutor executor, Channel parent, ChannelPromise promise) { // See javadoc for explanation of why this may be disabled. boolean allowGracefulDelayed = ConnectionCloseChannelAttributes.allowGracefulDelayed(parent); if (! allowGracefulDelayed) { immediate(parent, promise); return; } if (! parent.isActive()) { promise.setSuccess(); return; } // First send a 'graceful shutdown' GOAWAY frame. /* "A server that is attempting to gracefully shut down a connection SHOULD send an initial GOAWAY frame with the last stream identifier set to 231-1 and a NO_ERROR code. This signals to the client that a shutdown is imminent and that initiating further requests is prohibited." -- https://http2.github.io/http2-spec/#GOAWAY */ DefaultHttp2GoAwayFrame goaway = new DefaultHttp2GoAwayFrame(Http2Error.NO_ERROR); goaway.setExtraStreamIds(Integer.MAX_VALUE); parent.writeAndFlush(goaway); LOG.debug("gracefullyWithDelay: flushed initial go_away frame. channel=" + parent.id().asShortText()); // In N secs time, throw an error that causes the http2 codec to send another GOAWAY frame // (this time with accurate lastStreamId) and then close the connection. int gracefulCloseDelay = ConnectionCloseChannelAttributes.gracefulCloseDelay(parent); executor.schedule(() -> { // Check that the client hasn't already closed the connection (due to the earlier goaway we sent). if (parent.isActive()) { // NOTE - the netty Http2ConnectionHandler specifically does not send another goaway when we call // channel.close() if one has already been sent .... so when we want more than one sent, we need to do it // explicitly ourselves like this. LOG.debug("gracefullyWithDelay: firing graceful_shutdown event to make netty send a final go_away frame and then close connection. channel=" + parent.id().asShortText()); Http2Exception h2e = new Http2Exception(Http2Error.NO_ERROR, Http2Exception.ShutdownHint.GRACEFUL_SHUTDOWN); parent.pipeline().fireExceptionCaught(h2e); parent.close().addListener(future -> { promise.setSuccess(); }); } else { promise.setSuccess(); } }, gracefulCloseDelay, TimeUnit.SECONDS); }