4
votes

On the server, ChannelPipelineFactory#getPipeline() is called for every new connection. My server's ChannelHandlers have allocation for serialization buffers and other objects that are not thread safe. How can I avoid allocating them for each connection? My application has a high volume of short-lived connections and this is hurting performance. I don't store per connection state in the handlers, I really only need to allocate them once per thread that will use the pipeline because the objects in the handlers are not thread safe.

Possibly I could use a pipeline with a single handler. When any event is received, I would obtain a reference to my actual handler from a ThreadLocal. This way I only allocate an actual handler once per thread serving connections. It does mean a ThreadLocal look up for every event though.

Are there other solutions that might be better?

I have the impression that the Netty pipeline looks great in relatively simple example code and is quite neat when it fits the problem well (such as the HTTP handling), but that it isn't very flexible for many other scenarios. Here is someone elses' thoughts along those lines. It isn't any sort of disaster since using a single handler to roll your own "pipeline" seems perfectly viable, but I wonder if I'm doing it wrong?

1

1 Answers

3
votes

Using a ThreadLocal seems like a good idea if that is enough for you. Be aware that this will only work well for upstream events as downstream events may be fired by any thread and so the ThreadLocal stuff may not work out that good.