List of usage examples for io.netty.channel EventLoop inEventLoop
boolean inEventLoop();
From source file:com.linecorp.armeria.client.http.HttpSessionChannelFactory.java
License:Apache License
private void initSession(SessionProtocol protocol, ChannelFuture connectFuture, Promise<Channel> sessionPromise) { assert connectFuture.isSuccess(); final Channel ch = connectFuture.channel(); final EventLoop eventLoop = ch.eventLoop(); assert eventLoop.inEventLoop(); final ScheduledFuture<?> timeoutFuture = eventLoop.schedule(() -> { if (sessionPromise.tryFailure(new SessionProtocolNegotiationException(protocol, "connection established, but session creation timed out: " + ch))) { ch.close();/*w w w. j a va 2 s . co m*/ } }, options.connectTimeoutMillis(), TimeUnit.MILLISECONDS); ch.pipeline().addLast(new HttpSessionHandler(this, ch, sessionPromise, timeoutFuture)); }
From source file:com.linecorp.armeria.client.HttpSessionChannelFactory.java
License:Apache License
private void initSession(SessionProtocol protocol, ChannelFuture connectFuture, Promise<Channel> sessionPromise) { assert connectFuture.isSuccess(); final Channel ch = connectFuture.channel(); final EventLoop eventLoop = ch.eventLoop(); assert eventLoop.inEventLoop(); final ScheduledFuture<?> timeoutFuture = eventLoop.schedule(() -> { if (sessionPromise.tryFailure(new SessionProtocolNegotiationException(protocol, "connection established, but session creation timed out: " + ch))) { ch.close();//from ww w . j ava2s .c o m } }, connectTimeoutMillis, TimeUnit.MILLISECONDS); ch.pipeline().addLast(new HttpSessionHandler(this, ch, sessionPromise, timeoutFuture)); }
From source file:com.linecorp.armeria.client.pool.DefaultKeyedChannelPool.java
License:Apache License
private Future<Channel> acquireHealthyFromPoolOrNew(final K key, final Promise<Channel> promise) { final Deque<Channel> queue = pool.get(key); final Channel ch = queue == null ? null : queue.poll(); if (ch == null) { Future<Channel> f = channelFactory.apply(key); if (f.isDone()) { notifyConnect(key, f, promise); } else {/*from w ww . j av a2s. co m*/ f.addListener((Future<Channel> future) -> notifyConnect(key, future, promise)); } return promise; } EventLoop loop = ch.eventLoop(); if (loop.inEventLoop()) { doHealthCheck(key, ch, promise); } else { loop.execute(() -> doHealthCheck(key, ch, promise)); } return promise; }
From source file:com.linecorp.armeria.client.pool.DefaultKeyedChannelPool.java
License:Apache License
@Override public Future<Void> release(final K key, final Channel channel, final Promise<Void> promise) { requireNonNull(key, "key"); requireNonNull(channel, "channel"); requireNonNull(promise, "promise"); try {//w w w .jav a 2 s . c om EventLoop loop = channel.eventLoop(); if (loop.inEventLoop()) { doReleaseChannel(key, channel, promise); } else { loop.execute(() -> doReleaseChannel(key, channel, promise)); } } catch (Throwable cause) { closeAndFail(channel, cause, promise); } return promise; }
From source file:io.gatling.http.client.impl.DefaultHttpClient.java
License:Apache License
@Override public void sendRequest(Request request, long clientId, boolean shared, HttpListener listener) { if (isClosed()) { return;//w ww. j a v a 2s . c o m } listener.onSend(); if (request.getUri().isSecured() && request.isHttp2Enabled() && !config.isEnableSni()) { listener.onThrowable(new UnsupportedOperationException("HTTP/2 can't work if SNI is disabled.")); return; } EventLoop eventLoop = eventLoopPicker.eventLoopWithAffinity(clientId); if (eventLoop.inEventLoop()) { sendRequestInEventLoop(request, clientId, shared, listener, eventLoop); } else { eventLoop.execute(() -> sendRequestInEventLoop(request, clientId, shared, listener, eventLoop)); } }
From source file:io.gatling.http.client.impl.DefaultHttpClient.java
License:Apache License
@Override public void sendHttp2Requests(Pair<Request, HttpListener>[] requestsAndListeners, long clientId, boolean shared) { if (isClosed()) { return;//from ww w .j av a2 s . c o m } for (Pair<Request, HttpListener> pair : requestsAndListeners) { pair.getRight().onSend(); } Request headRequest = requestsAndListeners[0].getLeft(); if (headRequest.getUri().isSecured() && headRequest.isHttp2Enabled() && !config.isEnableSni()) { for (Pair<Request, HttpListener> requestAndListener : requestsAndListeners) { HttpListener listener = requestAndListener.getRight(); listener.onThrowable(new UnsupportedOperationException("HTTP/2 can't work if SNI is disabled.")); } return; } EventLoop eventLoop = eventLoopPicker.eventLoopWithAffinity(clientId); if (eventLoop.inEventLoop()) { sendHttp2RequestsInEventLoop(requestsAndListeners, clientId, shared, eventLoop); } else { eventLoop .execute(() -> sendHttp2RequestsInEventLoop(requestsAndListeners, clientId, shared, eventLoop)); } }
From source file:io.gatling.http.client.impl.DefaultHttpClient.java
License:Apache License
@Override public void flushClientIdChannels(long clientId) { EventLoop eventLoop = eventLoopPicker.eventLoopWithAffinity(clientId); if (eventLoop.inEventLoop()) { eventLoopResources(eventLoop).channelPool.flushClientIdChannelPoolPartitions(clientId); } else {/*from w w w . j a va 2s. c o m*/ eventLoop.execute( () -> eventLoopResources(eventLoop).channelPool.flushClientIdChannelPoolPartitions(clientId)); } }
From source file:io.grpc.netty.WriteQueueTest.java
License:Apache License
/** * Set up for test./* w w w .ja v a 2 s. c o m*/ */ @Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); when(channel.newPromise()).thenReturn(promise); EventLoop eventLoop = Mockito.mock(EventLoop.class); doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { Runnable r = (Runnable) invocation.getArguments()[0]; r.run(); return null; } }).when(eventLoop).execute(any(Runnable.class)); when(eventLoop.inEventLoop()).thenReturn(true); when(channel.eventLoop()).thenReturn(eventLoop); when(channel.flush()).thenAnswer(new Answer<Channel>() { @Override public Channel answer(InvocationOnMock invocation) throws Throwable { synchronized (lock) { flushCalledNanos = System.nanoTime(); if (flushCalledNanos == writeCalledNanos) { flushCalledNanos += 1; } } return channel; } }); when(channel.write(any(QueuedCommand.class), eq(promise))).thenAnswer(new Answer<ChannelFuture>() { @Override public ChannelFuture answer(InvocationOnMock invocation) throws Throwable { synchronized (lock) { writeCalledNanos = System.nanoTime(); if (writeCalledNanos == flushCalledNanos) { writeCalledNanos += 1; } } return promise; } }); }
From source file:io.hekate.network.netty.NettyServerClient.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (isHandshakeDone()) { if (msg instanceof Heartbeat) { if (trace) { log.trace("Received network heartbeat from client [from={}]", address()); }/*from w w w .j av a 2 s.co m*/ } else { NettyMessage netMsg = (NettyMessage) msg; netMsg.prepare(log); if (trace) { log.trace("Message buffer prepared [from={}, message={}]", address(), netMsg); } if (metrics != null) { metrics.onMessageReceived(); } try { serverHandler.onMessage(netMsg, this); } finally { netMsg.release(); } } } else { if (trace) { log.trace("Received network handshake request [from={}, message={}]", address(), msg); } HandshakeRequest handshake = (HandshakeRequest) msg; String protocol; NettyServerHandler handlerReg; if (handshake == null) { protocol = null; handlerReg = null; } else { this.protocol = protocol = handshake.protocol(); handlerReg = handlers.get(protocol); } if (handlerReg == null) { if (debug) { log.debug("Closing connection with unsupported protocol [from={}, protocol={}]", address(), protocol); } HandshakeReject reject = new HandshakeReject("Unsupported protocol [protocol=" + protocol + ']'); ctx.writeAndFlush(reject).addListener(ChannelFutureListener.CLOSE); } else { // Map connection to a thread. EventLoop eventLoop = mapToThread(handshake.threadAffinity(), handlerReg); // Check if we need to re-bind this channel to another thread. if (eventLoop.inEventLoop()) { // No need to rebind. init(ctx.channel(), handshake, handlerReg); } else { if (trace) { log.trace("Registering channel to a custom NIO thread [from={}, protocol={}]", address(), protocol); } // Unregister and then re-register IdleStateHandler in order to prevent RejectedExecutionException if same // instance is used on different threads. ctx.pipeline().remove(IdleStateHandler.class.getName()); Channel channel = ctx.channel(); channel.deregister().addListener(deregister -> { if (deregister.isSuccess()) { if (!eventLoop.isShutdown() && channel.isOpen()) { eventLoop.register(channel).addListener(register -> { if (register.isSuccess() && channel.isOpen()) { if (trace) { log.trace( "Registered channel to a custom NIO thread [from={}, protocol={}]", address(), protocol); } mayBeCreateIdleStateHandler().ifPresent(handler -> ctx.pipeline() .addFirst(IdleStateHandler.class.getName(), handler)); init(channel, handshake, handlerReg); } }); } } }); } } } }
From source file:io.hekate.network.netty.NettyServerClient.java
License:Apache License
private void pauseReceiver(boolean pause, Consumer<NetworkEndpoint<Object>> callback) { ChannelHandlerContext localCtx = this.handlerCtx; if (localCtx != null) { if (debug) { if (pause) { log.debug("Pausing inbound receiver [from={}, protocol={}]", address(), protocol); } else { log.debug("Resuming Pausing inbound receiver [from={}, protocol={}]", address(), protocol); }// ww w . ja v a 2 s . c om } Channel channel = localCtx.channel(); EventLoop eventLoop = channel.eventLoop(); if (eventLoop.inEventLoop()) { channel.config().setAutoRead(!pause); notifyOnReceivePause(pause, callback, channel); } else { eventLoop.execute(() -> { channel.config().setAutoRead(!pause); notifyOnReceivePause(pause, callback, channel); }); } } else if (callback != null) { callback.accept(this); } }