Skip to content

Commit 74797c1

Browse files
Make some Netty Handlers Singletons (#80156)
Some of these handlers can be singletons, they hold no state. Also, removed some unused fields in the transport.
1 parent 00aa197 commit 74797c1

File tree

6 files changed

+22
-25
lines changed

6 files changed

+22
-25
lines changed

modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpRequest.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,6 @@ public Exception getInboundException() {
227227
return inboundException;
228228
}
229229

230-
public FullHttpRequest nettyRequest() {
231-
return request;
232-
}
233-
234230
/**
235231
* A wrapper of {@link HttpHeaders} that implements a map to prevent copying unnecessarily. This class does not support modifications
236232
* and due to the underlying implementation, it performs case insensitive lookups of key to values.

modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpRequestCreator.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
import java.util.List;
1919

2020
@ChannelHandler.Sharable
21-
class Netty4HttpRequestCreator extends MessageToMessageDecoder<FullHttpRequest> {
21+
final class Netty4HttpRequestCreator extends MessageToMessageDecoder<FullHttpRequest> {
22+
23+
static final Netty4HttpRequestCreator INSTANCE = new Netty4HttpRequestCreator();
24+
25+
private Netty4HttpRequestCreator() {}
2226

2327
@Override
2428
protected void decode(ChannelHandlerContext ctx, FullHttpRequest msg, List<Object> out) {

modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpResponseCreator.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727
* Split up large responses to prevent batch compression {@link JdkZlibEncoder} down the pipeline.
2828
*/
2929
@ChannelHandler.Sharable
30-
class Netty4HttpResponseCreator extends MessageToMessageEncoder<Netty4HttpResponse> {
30+
final class Netty4HttpResponseCreator extends MessageToMessageEncoder<Netty4HttpResponse> {
31+
32+
static final Netty4HttpResponseCreator INSTANCE = new Netty4HttpResponseCreator();
33+
34+
private Netty4HttpResponseCreator() {}
3135

3236
private static final String DO_NOT_SPLIT = "es.unsafe.do_not_split_http_responses";
3337

modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpServerTransport.java

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,6 @@ public class Netty4HttpServerTransport extends AbstractHttpServerTransport {
127127
Property.NodeScope
128128
);
129129

130-
private final ByteSizeValue maxInitialLineLength;
131-
private final ByteSizeValue maxHeaderSize;
132-
private final ByteSizeValue maxChunkSize;
133-
134130
private final int pipeliningMaxEvents;
135131

136132
private final SharedGroupFactory sharedGroupFactory;
@@ -157,9 +153,6 @@ public Netty4HttpServerTransport(
157153
NettyAllocator.logAllocatorDescriptionIfNeeded();
158154
this.sharedGroupFactory = sharedGroupFactory;
159155

160-
this.maxChunkSize = SETTING_HTTP_MAX_CHUNK_SIZE.get(settings);
161-
this.maxHeaderSize = SETTING_HTTP_MAX_HEADER_SIZE.get(settings);
162-
this.maxInitialLineLength = SETTING_HTTP_MAX_INITIAL_LINE_LENGTH.get(settings);
163156
this.pipeliningMaxEvents = SETTING_PIPELINING_MAX_EVENTS.get(settings);
164157

165158
this.maxCompositeBufferComponents = SETTING_HTTP_NETTY_MAX_COMPOSITE_BUFFER_COMPONENTS.get(settings);
@@ -172,9 +165,9 @@ public Netty4HttpServerTransport(
172165
logger.debug(
173166
"using max_chunk_size[{}], max_header_size[{}], max_initial_line_length[{}], max_content_length[{}], "
174167
+ "receive_predictor[{}], max_composite_buffer_components[{}], pipelining_max_events[{}]",
175-
maxChunkSize,
176-
maxHeaderSize,
177-
maxInitialLineLength,
168+
SETTING_HTTP_MAX_CHUNK_SIZE.get(settings),
169+
SETTING_HTTP_MAX_HEADER_SIZE.get(settings),
170+
SETTING_HTTP_MAX_INITIAL_LINE_LENGTH.get(settings),
178171
maxContentLength,
179172
receivePredictor,
180173
maxCompositeBufferComponents,
@@ -297,17 +290,13 @@ public ChannelHandler configureServerChannelHandler() {
297290
protected static class HttpChannelHandler extends ChannelInitializer<Channel> {
298291

299292
private final Netty4HttpServerTransport transport;
300-
private final Netty4HttpRequestCreator requestCreator;
301293
private final Netty4HttpRequestHandler requestHandler;
302-
private final Netty4HttpResponseCreator responseCreator;
303294
private final HttpHandlingSettings handlingSettings;
304295

305296
protected HttpChannelHandler(final Netty4HttpServerTransport transport, final HttpHandlingSettings handlingSettings) {
306297
this.transport = transport;
307298
this.handlingSettings = handlingSettings;
308-
this.requestCreator = new Netty4HttpRequestCreator();
309299
this.requestHandler = new Netty4HttpRequestHandler(transport);
310-
this.responseCreator = new Netty4HttpResponseCreator();
311300
}
312301

313302
@Override
@@ -331,8 +320,8 @@ protected void initChannel(Channel ch) throws Exception {
331320
if (handlingSettings.isCompression()) {
332321
ch.pipeline().addLast("encoder_compress", new HttpContentCompressor(handlingSettings.getCompressionLevel()));
333322
}
334-
ch.pipeline().addLast("request_creator", requestCreator);
335-
ch.pipeline().addLast("response_creator", responseCreator);
323+
ch.pipeline().addLast("request_creator", Netty4HttpRequestCreator.INSTANCE);
324+
ch.pipeline().addLast("response_creator", Netty4HttpResponseCreator.INSTANCE);
336325
ch.pipeline().addLast("pipelining", new Netty4HttpPipeliningHandler(logger, transport.pipeliningMaxEvents));
337326
ch.pipeline().addLast("handler", requestHandler);
338327
transport.serverAcceptedChannel(nettyHttpChannel);

modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/ESLoggingHandler.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@
88

99
package org.elasticsearch.transport.netty4;
1010

11+
import io.netty.channel.ChannelHandler;
1112
import io.netty.channel.ChannelHandlerContext;
1213
import io.netty.handler.logging.LogLevel;
1314
import io.netty.handler.logging.LoggingHandler;
1415

16+
@ChannelHandler.Sharable
1517
final class ESLoggingHandler extends LoggingHandler {
1618

17-
ESLoggingHandler() {
19+
static final ESLoggingHandler INSTANCE = new ESLoggingHandler();
20+
21+
private ESLoggingHandler() {
1822
super(LogLevel.TRACE);
1923
}
2024

modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/Netty4Transport.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ protected void initChannel(Channel ch) throws Exception {
334334
assert ch instanceof Netty4NioSocketChannel;
335335
NetUtils.tryEnsureReasonableKeepAliveConfig(((Netty4NioSocketChannel) ch).javaChannel());
336336
ch.pipeline().addLast("byte_buf_sizer", NettyByteBufSizer.INSTANCE);
337-
ch.pipeline().addLast("logging", new ESLoggingHandler());
337+
ch.pipeline().addLast("logging", ESLoggingHandler.INSTANCE);
338338
// using a dot as a prefix means this cannot come from any settings parsed
339339
ch.pipeline().addLast("dispatcher", new Netty4MessageChannelHandler(Netty4Transport.this, recycler));
340340
}
@@ -362,7 +362,7 @@ protected void initChannel(Channel ch) throws Exception {
362362
Netty4TcpChannel nettyTcpChannel = new Netty4TcpChannel(ch, true, name, ch.newSucceededFuture());
363363
ch.attr(CHANNEL_KEY).set(nettyTcpChannel);
364364
ch.pipeline().addLast("byte_buf_sizer", NettyByteBufSizer.INSTANCE);
365-
ch.pipeline().addLast("logging", new ESLoggingHandler());
365+
ch.pipeline().addLast("logging", ESLoggingHandler.INSTANCE);
366366
ch.pipeline().addLast("dispatcher", new Netty4MessageChannelHandler(Netty4Transport.this, recycler));
367367
serverAcceptedChannel(nettyTcpChannel);
368368
}

0 commit comments

Comments
 (0)