Search in sources :

Example 46 with EventLoop

use of io.netty.channel.EventLoop in project grpc-java by grpc.

the class WriteQueueTest method setUp.

/**
   * Set up for test.
   */
@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;
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) EventLoop(io.netty.channel.EventLoop) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Channel(io.netty.channel.Channel) QueuedCommand(io.grpc.netty.WriteQueue.QueuedCommand) Before(org.junit.Before)

Example 47 with EventLoop

use of io.netty.channel.EventLoop in project ProxProx by GoMint.

the class ConnectionHandler method flush.

private void flush(FlushItem item) {
    EventLoop loop = item.channel.eventLoop();
    Flusher flusher = flusherLookup.get(loop);
    if (flusher == null) {
        Flusher alt = flusherLookup.putIfAbsent(loop, flusher = new Flusher(loop));
        if (alt != null) {
            flusher = alt;
        }
    }
    flusher.queued.add(item);
    flusher.start();
}
Also used : EventLoop(io.netty.channel.EventLoop)

Example 48 with EventLoop

use of io.netty.channel.EventLoop in project riposte by Nike-Inc.

the class ProxyRouterEndpointExecutionHandler method getCircuitBreaker.

protected Optional<CircuitBreaker<HttpResponse>> getCircuitBreaker(DownstreamRequestFirstChunkInfo downstreamReqFirstChunkInfo, ChannelHandlerContext ctx) {
    if (downstreamReqFirstChunkInfo == null || downstreamReqFirstChunkInfo.disableCircuitBreaker)
        return Optional.empty();
    // custom one is not specified.
    if (downstreamReqFirstChunkInfo.customCircuitBreaker.isPresent())
        return downstreamReqFirstChunkInfo.customCircuitBreaker;
    // No custom circuit breaker. Use the default for the given request's host.
    EventLoop nettyEventLoop = ctx.channel().eventLoop();
    CircuitBreaker<Integer> defaultStatusCodeCircuitBreaker = getDefaultHttpStatusCodeCircuitBreakerForKey(downstreamReqFirstChunkInfo.host, Optional.ofNullable(nettyEventLoop), Optional.ofNullable(nettyEventLoop));
    return Optional.of(new CircuitBreakerDelegate<>(defaultStatusCodeCircuitBreaker, httpResponse -> (httpResponse == null ? null : httpResponse.getStatus().code())));
}
Also used : Span(com.nike.wingtips.Span) LoggerFactory(org.slf4j.LoggerFactory) ResponseInfo(com.nike.riposte.server.http.ResponseInfo) HttpObject(io.netty.handler.codec.http.HttpObject) ProxyRouterEndpoint(com.nike.riposte.server.http.ProxyRouterEndpoint) Map(java.util.Map) HttpRequest(io.netty.handler.codec.http.HttpRequest) CompletionException(java.util.concurrent.CompletionException) EventLoop(io.netty.channel.EventLoop) DownstreamRequestFirstChunkInfo(com.nike.riposte.server.http.ProxyRouterEndpoint.DownstreamRequestFirstChunkInfo) BaseInboundHandlerWithTracingAndMdcSupport(com.nike.riposte.server.handler.base.BaseInboundHandlerWithTracingAndMdcSupport) Endpoint(com.nike.riposte.server.http.Endpoint) HttpUtils(com.nike.riposte.util.HttpUtils) StreamingChannel(com.nike.riposte.client.asynchttp.netty.StreamingAsyncHttpClient.StreamingChannel) ChannelAttributes(com.nike.riposte.server.channelpipeline.ChannelAttributes) CircuitBreaker(com.nike.fastbreak.CircuitBreaker) RiposteInternalRequestInfo(com.nike.riposte.server.http.impl.RiposteInternalRequestInfo) Optional(java.util.Optional) HttpResponse(io.netty.handler.codec.http.HttpResponse) StreamingCallback(com.nike.riposte.client.asynchttp.netty.StreamingAsyncHttpClient.StreamingCallback) HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) EventExecutor(io.netty.util.concurrent.EventExecutor) RequestInfo(com.nike.riposte.server.http.RequestInfo) CircuitBreakerDelegate(com.nike.fastbreak.CircuitBreakerDelegate) ManualModeTask(com.nike.fastbreak.CircuitBreaker.ManualModeTask) CompletableFuture(java.util.concurrent.CompletableFuture) StreamingAsyncHttpClient(com.nike.riposte.client.asynchttp.netty.StreamingAsyncHttpClient) PipelineContinuationBehavior(com.nike.riposte.server.handler.base.PipelineContinuationBehavior) Deque(java.util.Deque) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) AsyncNettyHelper(com.nike.riposte.util.AsyncNettyHelper) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpContent(io.netty.handler.codec.http.HttpContent) Attribute(io.netty.util.Attribute) OutboundMessageSendHeadersChunkFromResponseInfo(com.nike.riposte.server.channelpipeline.message.OutboundMessageSendHeadersChunkFromResponseInfo) Logger(org.slf4j.Logger) Executor(java.util.concurrent.Executor) AsyncNettyHelper.executeOnlyIfChannelIsActive(com.nike.riposte.util.AsyncNettyHelper.executeOnlyIfChannelIsActive) CircuitBreakerForHttpStatusCode.getDefaultHttpStatusCodeCircuitBreakerForKey(com.nike.fastbreak.CircuitBreakerForHttpStatusCode.getDefaultHttpStatusCodeCircuitBreakerForKey) AsyncNettyHelper.functionWithTracingAndMdc(com.nike.riposte.util.AsyncNettyHelper.functionWithTracingAndMdc) ChannelFuture(io.netty.channel.ChannelFuture) ExecutionException(java.util.concurrent.ExecutionException) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) WrapperException(com.nike.backstopper.exception.WrapperException) OutboundMessageSendContentChunk(com.nike.riposte.server.channelpipeline.message.OutboundMessageSendContentChunk) AsyncNettyHelper.runnableWithTracingAndMdc(com.nike.riposte.util.AsyncNettyHelper.runnableWithTracingAndMdc) LastOutboundMessageSendLastContentChunk(com.nike.riposte.server.channelpipeline.message.LastOutboundMessageSendLastContentChunk) Pair(com.nike.internal.util.Pair) ProxyRouterProcessingState(com.nike.riposte.server.http.ProxyRouterProcessingState) EventLoop(io.netty.channel.EventLoop)

Example 49 with EventLoop

use of io.netty.channel.EventLoop in project pravega by pravega.

the class ServerConnectionInboundHandler method send.

@Override
public void send(WireCommand cmd) {
    Channel c = getChannel();
    // Work around for https://github.com/netty/netty/issues/3246
    EventLoop eventLoop = c.eventLoop();
    if (eventLoop.inEventLoop()) {
        eventLoop.execute(() -> writeAndFlush(c, cmd));
    } else {
        writeAndFlush(c, cmd);
    }
}
Also used : EventLoop(io.netty.channel.EventLoop) Channel(io.netty.channel.Channel)

Example 50 with EventLoop

use of io.netty.channel.EventLoop in project riposte by Nike-Inc.

the class NonblockingEndpointExecutionHandlerTest method beforeMethod.

@Before
public void beforeMethod() {
    stateMock = mock(HttpProcessingState.class);
    proxyRouterStateMock = mock(ProxyRouterProcessingState.class);
    ctxMock = mock(ChannelHandlerContext.class);
    channelMock = mock(Channel.class);
    stateAttrMock = mock(Attribute.class);
    proxyRouterStateAttrMock = mock(Attribute.class);
    requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests();
    endpointMock = mock(StandardEndpoint.class);
    longRunningTaskExecutorMock = mock(Executor.class);
    responseFuture = new CompletableFuture<>();
    stateWorkChainFutureSpy = spy(CompletableFuture.completedFuture(null));
    eventLoopMock = mock(EventLoop.class);
    eventExecutorMock = mock(EventExecutor.class);
    doReturn(channelMock).when(ctxMock).channel();
    doReturn(stateAttrMock).when(channelMock).attr(ChannelAttributes.HTTP_PROCESSING_STATE_ATTRIBUTE_KEY);
    doReturn(stateMock).when(stateAttrMock).get();
    doReturn(false).when(stateMock).isRequestHandled();
    doReturn(proxyRouterStateAttrMock).when(channelMock).attr(ChannelAttributes.PROXY_ROUTER_PROCESSING_STATE_ATTRIBUTE_KEY);
    doReturn(proxyRouterStateMock).when(proxyRouterStateAttrMock).get();
    doReturn(endpointMock).when(stateMock).getEndpointForExecution();
    doReturn(requestInfo).when(stateMock).getRequestInfo();
    doReturn(responseFuture).when(endpointMock).execute(any(RequestInfo.class), any(Executor.class), any(ChannelHandlerContext.class));
    doReturn(eventLoopMock).when(channelMock).eventLoop();
    doReturn(eventExecutorMock).when(ctxMock).executor();
    doReturn(true).when(eventExecutorMock).inEventLoop();
    doReturn(true).when(channelMock).isActive();
    doAnswer(invocation -> {
        CompletableFuture actualFutureForAttaching = (CompletableFuture) invocation.callRealMethod();
        futureThatWillBeAttachedToSpy = spy(actualFutureForAttaching);
        return futureThatWillBeAttachedToSpy;
    }).when(stateWorkChainFutureSpy).thenCompose(any(Function.class));
    doReturn(stateWorkChainFutureSpy).when(stateMock).getPreEndpointExecutionWorkChain();
    handlerSpy = spy(new NonblockingEndpointExecutionHandler(longRunningTaskExecutorMock, defaultCompletableFutureTimeoutMillis));
}
Also used : Attribute(io.netty.util.Attribute) HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) Channel(io.netty.channel.Channel) ProxyRouterProcessingState(com.nike.riposte.server.http.ProxyRouterProcessingState) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) RequestInfo(com.nike.riposte.server.http.RequestInfo) Function(java.util.function.Function) CompletableFuture(java.util.concurrent.CompletableFuture) EventExecutor(io.netty.util.concurrent.EventExecutor) Executor(java.util.concurrent.Executor) EventLoop(io.netty.channel.EventLoop) EventExecutor(io.netty.util.concurrent.EventExecutor) StandardEndpoint(com.nike.riposte.server.http.StandardEndpoint) Before(org.junit.Before)

Aggregations

EventLoop (io.netty.channel.EventLoop)77 EventLoopGroup (io.netty.channel.EventLoopGroup)27 Test (org.junit.jupiter.api.Test)27 DefaultEventLoopGroup (io.netty.channel.DefaultEventLoopGroup)18 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)17 Channel (io.netty.channel.Channel)10 InetSocketAddress (java.net.InetSocketAddress)10 Bootstrap (io.netty.bootstrap.Bootstrap)9 InetAddress (java.net.InetAddress)9 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)7 ChannelFuture (io.netty.channel.ChannelFuture)6 ChannelPromise (io.netty.channel.ChannelPromise)6 LocalAddress (io.netty.channel.local.LocalAddress)5 HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)4 ChannelPipeline (io.netty.channel.ChannelPipeline)4 ClosedChannelException (java.nio.channels.ClosedChannelException)4 SingleThreadEventLoop (io.netty.channel.SingleThreadEventLoop)3 IOException (java.io.IOException)3 UnknownHostException (java.net.UnknownHostException)3 Callable (java.util.concurrent.Callable)3