use of io.netty.channel.ChannelHandler in project ambry by linkedin.
the class FrontendNettyChannelInitializer method initChannel.
@Override
protected void initChannel(SocketChannel ch) throws Exception {
// If channel handler implementations are not annotated with @Sharable, Netty creates a new instance of every class
// in the pipeline for every connection.
// i.e. if there are a 1000 active connections there will be a 1000 NettyMessageProcessor instances.
ChannelPipeline pipeline = ch.pipeline();
// connection stats handler to track connection related metrics
pipeline.addLast("connectionStatsHandler", connectionStatsHandler);
// if SSL is enabled, add an SslHandler before the HTTP codec
if (sslFactory != null) {
InetSocketAddress peerAddress = ch.remoteAddress();
String peerHost = peerAddress.getHostName();
int peerPort = peerAddress.getPort();
SslHandler sslHandler = new SslHandler(sslFactory.createSSLEngine(peerHost, peerPort, SSLFactory.Mode.SERVER));
pipeline.addLast("sslHandler", sslHandler);
}
pipeline.addLast("codec", new HttpServerCodec(nettyConfig.nettyServerMaxInitialLineLength, nettyConfig.nettyServerMaxHeaderSize, nettyConfig.nettyServerMaxChunkSize)).addLast("healthCheckHandler", new HealthCheckHandler(restServerState, nettyMetrics)).addLast("publicAccessLogHandler", new PublicAccessLogHandler(publicAccessLogger, nettyMetrics)).addLast("idleStateHandler", new IdleStateHandler(0, 0, nettyConfig.nettyServerIdleTimeSeconds)).addLast("chunker", new ChunkedWriteHandler());
if (addedChannelHandlers != null) {
pipeline.addLast(addedChannelHandlers.toArray(new ChannelHandler[0]));
}
// custom processing class that interfaces with a RestRequestService.
pipeline.addLast("processor", new NettyMessageProcessor(nettyMetrics, nettyConfig, performanceConfig, requestHandler));
}
use of io.netty.channel.ChannelHandler in project pravega by pravega.
the class AdminConnectionListener method createEncodingStack.
@Override
public List<ChannelHandler> createEncodingStack(String connectionName) {
List<ChannelHandler> stack = new ArrayList<>();
stack.add(new ExceptionLoggingHandler(connectionName));
stack.add(new CommandEncoder(null, NO_OP_METRIC_NOTIFIER));
stack.add(new LengthFieldBasedFrameDecoder(MAX_WIRECOMMAND_SIZE, 4, 4));
stack.add(new CommandDecoder());
return stack;
}
use of io.netty.channel.ChannelHandler in project pravega by pravega.
the class PravegaConnectionListener method createEncodingStack.
@Override
public List<ChannelHandler> createEncodingStack(String connectionName) {
List<ChannelHandler> stack = new ArrayList<>();
stack.add(new ExceptionLoggingHandler(connectionName));
stack.add(new CommandEncoder(null, NO_OP_METRIC_NOTIFIER));
stack.add(new LengthFieldBasedFrameDecoder(MAX_WIRECOMMAND_SIZE, 4, 4));
stack.add(new CommandDecoder());
stack.add(new AppendDecoder());
return stack;
}
use of io.netty.channel.ChannelHandler in project janusgraph by JanusGraph.
the class SaslAndHMACAuthenticationHandlerTest method testHttpChannelReadWhenAuthenticatorHasBeenAdded.
@Test
public void testHttpChannelReadWhenAuthenticatorHasBeenAdded() throws Exception {
final SaslAndHMACAuthenticator authenticator = createMock(SaslAndHMACAuthenticator.class);
final HMACAuthenticator hmacAuth = createMock(HMACAuthenticator.class);
final ChannelHandlerContext ctx = createMock(ChannelHandlerContext.class);
final ChannelHandler mockHandler = createMock(ChannelHandler.class);
final ChannelPipeline pipeline = createMock(ChannelPipeline.class);
final HttpMessage msg = createMock(HttpMessage.class);
final HttpHeaders headers = createMock(HttpHeaders.class);
expect(authenticator.getHMACAuthenticator()).andReturn(hmacAuth);
expect(authenticator.getSimpleAuthenticator()).andReturn(createMock(JanusGraphSimpleAuthenticator.class));
expect(ctx.pipeline()).andReturn(pipeline);
expect(pipeline.get("hmac_authenticator")).andReturn(mockHandler);
expect(msg.headers()).andReturn(headers).times(2);
expect(headers.get(isA(String.class))).andReturn(null).times(2);
expect(ctx.fireChannelRead(eq(msg))).andReturn(ctx);
replayAll();
final SaslAndHMACAuthenticationHandler handler = new SaslAndHMACAuthenticationHandler(authenticator, null);
handler.channelRead(ctx, msg);
}
use of io.netty.channel.ChannelHandler in project riposte by Nike-Inc.
the class RequestStateCleanerHandler method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof HttpRequest) {
// New request incoming - setup/clear *all* state objects for new requests
for (ProcessingStateClassAndKeyPair<? extends ProcessingState> stateClassAndKeyPair : PROCESSING_STATE_ATTRIBUTE_KEYS) {
// See if we have an existing state object for this channel for the given state type.
@SuppressWarnings("unchecked") AttributeKey<ProcessingState> attrKey = (AttributeKey<ProcessingState>) stateClassAndKeyPair.getRight();
Attribute<ProcessingState> processingStateAttr = ctx.channel().attr(attrKey);
ProcessingState processingState = processingStateAttr.get();
if (processingState == null) {
// We don't already have one for this channel, so create one and register it.
processingState = stateClassAndKeyPair.getLeft().newInstance();
processingStateAttr.set(processingState);
}
// Clean the state for the new request.
processingState.cleanStateForNewRequest();
}
HttpProcessingState httpProcessingState = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get();
// Set the DistributedTracingConfig on the HttpProcessingState.
// noinspection deprecation - This is the only place that should actually be calling this method.
httpProcessingState.setDistributedTracingConfig(distributedTracingConfig);
// Send a request received event to the metricsListener.
if (metricsListener != null) {
metricsListener.onEvent(ServerMetricsEvent.REQUEST_RECEIVED, httpProcessingState);
}
// Remove the idle channel timeout handler (if there is one) so that it doesn't kill this new request if the
// endpoint takes longer to complete than the idle timeout value - the idle channel timeout is only for
// timing out channels that are idle *in-between* requests.
ChannelPipeline pipeline = ctx.pipeline();
ChannelHandler idleChannelTimeoutHandler = pipeline.get(HttpChannelInitializer.IDLE_CHANNEL_TIMEOUT_HANDLER_NAME);
if (idleChannelTimeoutHandler != null)
pipeline.remove(idleChannelTimeoutHandler);
// last chunk when the timeout hits.
if (incompleteHttpCallTimeoutMillis > 0 && !(msg instanceof LastHttpContent)) {
IncompleteHttpCallTimeoutHandler newHandler = new IncompleteHttpCallTimeoutHandler(incompleteHttpCallTimeoutMillis);
ChannelHandler existingHandler = pipeline.get(INCOMPLETE_HTTP_CALL_TIMEOUT_HANDLER_NAME);
if (existingHandler == null) {
pipeline.addFirst(INCOMPLETE_HTTP_CALL_TIMEOUT_HANDLER_NAME, newHandler);
} else {
logger.error("Handling HttpRequest for new request and found an IncompleteHttpCallTimeoutHandler " + "already in the pipeline. This should not be possible. A new " + "IncompleteHttpCallTimeoutHandler will replace the old one. worker_channel_id={}", ctx.channel().toString());
pipeline.replace(existingHandler, INCOMPLETE_HTTP_CALL_TIMEOUT_HANDLER_NAME, newHandler);
}
}
ProxyRouterProcessingState proxyRouterProcessingState = ChannelAttributes.getProxyRouterProcessingStateForChannel(ctx).get();
// Set the DistributedTracingConfig on the ProxyRouterProcessingState.
// noinspection deprecation - This is the only place that should actually be calling this method.
proxyRouterProcessingState.setDistributedTracingConfig(distributedTracingConfig);
} else if (msg instanceof LastHttpContent) {
// The HTTP call is complete, so we can remove the IncompleteHttpCallTimeoutHandler.
ChannelPipeline pipeline = ctx.pipeline();
ChannelHandler existingHandler = pipeline.get(INCOMPLETE_HTTP_CALL_TIMEOUT_HANDLER_NAME);
if (existingHandler != null)
pipeline.remove(INCOMPLETE_HTTP_CALL_TIMEOUT_HANDLER_NAME);
}
// Continue on the pipeline processing.
super.channelRead(ctx, msg);
}
Aggregations