For high-performance with many concurrent connections you WANT to use NIO or NIO.2
Create one Thread per connection and let the OS try to deal with thousands of threads
If you want.... good luck ;)
Allocate / Deallocate the shit out of it!
Rule of thumb: use static if it's immutable and used often. If its mutable only pool / cache if allocation costs are high!
Every time I hear allocation / deallocation of objects is a no-brainer a kitten dies!
But I never had GC-Pressure ....
Well, you not pushed your system hard enough!
channelIdle(ctx, new IdleStateEvent(IdleState.READER_IDLE,
readerIdleCount ++, currentTime - lastReadTime));
channelIdle(ctx, IdleStateEvent.READER_IDLE_EVENT);
Stop-the-world GC is your worst enemy if you want to push data hard
Free up memory of direct buffers is expensive
Unfortunately zero-out the byte array of heap buffers is not for free too
Can't insert int here as we need 4 slots!
WHY ?
Internally the JDK* will copy the buffer content to a direct buffer if you not use one
Only call Channel.write(...) / Channel.read(...) if you really need!
ByteBuffer expose operations like slice(), duplicate() for a good reason
USE THEM!
Only possible if you not need to transform the data during transfer!
But not call interestedOps(...) too often, its expensive!
https://github.com/netty/netty/issues/1024
Remember most of the times the Channel is writable!
I look at you DNS resolution!
If you really need to block move it to an extra ThreadPool
RED != Good!
This method may be invoked at any time. Whether or not it blocks, and for how long is implementation-dependent
public void suspendRead() {
key.interestOps(key.interestOps() & ~OP_READ);
}
public void suspendRead() {
int ops = key.interestOps();
if ((ops & OP_READ) != 0) {
key.interestOps(ops & ~OP_READ);
}
}
When write a System that handles 100k of concurrent connections every saved memory count for long-living objects
It's ugly, but sometimes you just have to do it!
private volatile Selector selector;
public void method() .... {
selector.select();
....
}
BETTER
private volatile Selector selector;
public void method() .... {
Selector selector = this.selector;
selector.select();
....
}
WHY?
Everything that needs to be stored till the call is done needs memory...
Allocation / Deallocation of ByteBuffers is a lot faster now...
It's always a trade-off!
If you write your own protocol think about Pipelining!
There are a few frameworks to rescue....
Attend my talk about Netty 4 tomorrow ;)