Commit Graph

126 Commits

Author SHA1 Message Date
Trustin Lee
e2fcb06d9a Remove unnecessary try-catch block in AbstractUnsafe.flush() 2013-07-12 20:00:30 +09:00
Norman Maurer
b57d9f307f Allow per-write promises and disallow promises on flush()
- write() now accepts a ChannelPromise and returns ChannelFuture as most
  users expected.  It makes the user's life much easier because it is
  now much easier to get notified when a specific message has been
  written.
- flush() does not create a ChannelPromise nor returns ChannelFuture.
  It is now similar to what read() looks like.
2013-07-11 00:49:48 +09:00
Trustin Lee
26e9d70457 Remove channelReadSuspended / Rename messageReceived(Last) to channelRead(Complete)
- Remove channelReadSuspended because it's actually same with messageReceivedLast
- Rename messageReceived to channelRead
- Rename messageReceivedLast to channelReadComplete

We renamed messageReceivedLast to channelReadComplete because it
reflects what it really is for.  Also, we renamed messageReceived to
channelRead for consistency in method names.
2013-07-09 23:58:51 +09:00
Trustin Lee
cbd8817905 Remove MessageList from public API and change ChannelInbound/OutboundHandler accordingly
I must admit MesageList was pain in the ass.  Instead of forcing a
handler always loop over the list of messages, this commit splits
messageReceived(ctx, list) into two event handlers:

- messageReceived(ctx, msg)
- mmessageReceivedLast(ctx)

When Netty reads one or more messages, messageReceived(ctx, msg) event
is triggered for each message.  Once the current read operation is
finished, messageReceivedLast() is triggered to tell the handler that
the last messageReceived() was the last message in the current batch.

Similarly, for outbound, write(ctx, list) has been split into two:

- write(ctx, msg)
- flush(ctx, promise)

Instead of writing a list of message with a promise, a user is now
supposed to call write(msg) multiple times and then call flush() to
actually flush the buffered messages.

Please note that write() doesn't have a promise with it.  You must call
flush() to get notified on completion. (or you can use writeAndFlush())

Other changes:

- Because MessageList is completely hidden, codec framework uses
  List<Object> instead of MessageList as an output parameter.
2013-07-09 23:51:48 +09:00
Trustin Lee
7396f9f2b2 Remove Channel.id completely / Use 64-bit hashCode internally to reduce the chance of collision in compareTo() 2013-07-09 14:49:06 +09:00
Norman Maurer
9c1b31d20a [#1535] Remove Channel.id() and so fix the possible leakage of the previous used Channel id map
The user can still use Channel.hashCode() for logging. It's just not 100% unique but should be good enough for most cases
2013-07-08 14:07:18 +02:00
Trustin Lee
2b2f8db5d2 Use ThreadLocalRandom where possible 2013-07-07 12:37:36 +09:00
Trustin Lee
734ec51ac9 Allow specifying 0 as the default number of threads when instantiating an EventLoopGroup
- Fixes #1426
- We already allow a user instantiate an EventLoopGroup with the default number of threads via the default constructor, so I think it's OK although it's not always optimal.
2013-06-27 10:39:39 +09:00
Trustin Lee
fe40d4b67f Make sure writing to a closed channel does not trigger an UnsupportedOperationException
- Fixes #1442
2013-06-14 11:15:46 +09:00
Trustin Lee
7a1550631d Make write operation cancellation while it's in progress
.. which should be useful when writing a large buffer/file
2013-06-12 04:24:07 +09:00
Trustin Lee
7a5cf48b8d Implement Promise/Future cancellation properly for outbound traffic
- Related issue: #1432
- Make sure the Promise of a write operation is not cancellable before writing out
2013-06-11 17:54:35 +09:00
Trustin Lee
14158070bf Revamp the core API to reduce memory footprint and consumption
The API changes made so far turned out to increase the memory footprint
and consumption while our intention was actually decreasing them.

Memory consumption issue:

When there are many connections which does not exchange data frequently,
the old Netty 4 API spent a lot more memory than 3 because it always
allocates per-handler buffer for each connection unless otherwise
explicitly stated by a user.  In a usual real world load, a client
doesn't always send requests without pausing, so the idea of having a
buffer whose life cycle if bound to the life cycle of a connection
didn't work as expected.

Memory footprint issue:

The old Netty 4 API decreased overall memory footprint by a great deal
in many cases.  It was mainly because the old Netty 4 API did not
allocate a new buffer and event object for each read.  Instead, it
created a new buffer for each handler in a pipeline.  This works pretty
well as long as the number of handlers in a pipeline is only a few.
However, for a highly modular application with many handlers which
handles connections which lasts for relatively short period, it actually
makes the memory footprint issue much worse.

Changes:

All in all, this is about retaining all the good changes we made in 4 so
far such as better thread model and going back to the way how we dealt
with message events in 3.

To fix the memory consumption/footprint issue mentioned above, we made a
hard decision to break the backward compatibility again with the
following changes:

- Remove MessageBuf
- Merge Buf into ByteBuf
- Merge ChannelInboundByte/MessageHandler and ChannelStateHandler into ChannelInboundHandler
  - Similar changes were made to the adapter classes
- Merge ChannelOutboundByte/MessageHandler and ChannelOperationHandler into ChannelOutboundHandler
  - Similar changes were made to the adapter classes
- Introduce MessageList which is similar to `MessageEvent` in Netty 3
- Replace inboundBufferUpdated(ctx) with messageReceived(ctx, MessageList)
- Replace flush(ctx, promise) with write(ctx, MessageList, promise)
- Remove ByteToByteEncoder/Decoder/Codec
  - Replaced by MessageToByteEncoder<ByteBuf>, ByteToMessageDecoder<ByteBuf>, and ByteMessageCodec<ByteBuf>
- Merge EmbeddedByteChannel and EmbeddedMessageChannel into EmbeddedChannel
- Add SimpleChannelInboundHandler which is sometimes more useful than
  ChannelInboundHandlerAdapter
- Bring back Channel.isWritable() from Netty 3
- Add ChannelInboundHandler.channelWritabilityChanges() event
- Add RecvByteBufAllocator configuration property
  - Similar to ReceiveBufferSizePredictor in Netty 3
  - Some existing configuration properties such as
    DatagramChannelConfig.receivePacketSize is gone now.
- Remove suspend/resumeIntermediaryDeallocation() in ByteBuf

This change would have been impossible without @normanmaurer's help. He
fixed, ported, and improved many parts of the changes.
2013-06-10 16:10:39 +09:00
Norman Maurer
f7931af704 Re-add Unsafe.voidPromise() which can be used for Unsafe operations for which no notification should be done [#1375] 2013-05-25 14:35:22 +02:00
Norman Maurer
699ef0784e [#1317] Allow to use VoidPromise for flush(...), write(...) and sendFile(...)
* This also move rename Channel.Unsafe.voidFuture() to ChannelPropertyAccess.voidPromise()
2013-05-17 15:50:14 +02:00
Trustin Lee
fd1d31e7d8 Remove unnecessary inEventLoop() checks in Channel.Unsafe
.. because HeadHandler in the pipeline always ensures those methods are always invoked from the correct I/O thread
2013-05-17 19:20:46 +09:00
Trustin Lee
6646db171e Use tryFailure() to notify registration failure to avoid IllegalStateException
.. which is raised when the registration promise has been failed already due to an internal error in the pipeline.

- Fixes #1319
2013-04-30 18:30:17 +09:00
Trustin Lee
8bb6d5b303 Rename Unsafe.directOutboundContext to headContext 2013-04-26 10:23:01 +09:00
Trustin Lee
e80fb65c36 Clean up the pipeline implementation / Ensure Embedded*Channel does not run pending tasks immediately
- Replace ugly 'prev != null' check with explicit event scheduling
- Fix an incorrect flag operation in freeHandlerBuffersAfterRemoval()
- Fix a bug in AbstractEmbeddedChannel.doRegister where it makes pending tasks immediately, where the pending tasks actually triggers inbound events
- Remove unnecessary suppression of inboundBufferUpdated() event in DefaultChannelPipeline, which potentially hides an event ordering bug. Unfortunately, I don't remember why I added it in cca35454d2.
2013-04-22 19:41:00 +09:00
Trustin Lee
7ee571968c Use progress + total instead of delta
.. because there is sometimes a task whose total is only a rough
estimation
2013-04-15 20:11:02 +09:00
Trustin Lee
e69033a4c3 Replace TransferFuture(Listener) with (Channel)ProgressiveFuture(Listener)
- Now works without the transport package
- Renamed TransferFuture to ProgressiveFuture and ChannelProgressiveFuture / same for promises
- ProgressiveFutureListener now extends GenericProgressiveFutureListener and GenericFutureListener (add/removeTransferListener*() were removed)
- Renamed DefaultEventListeners to DefaultFutureListeners and only accept GenericFutureListeners
- Various clean-up
2013-04-15 15:26:20 +09:00
kerr
713b200adf [#1244] Support ChannelTransferPromise for sendFile 2013-04-14 21:22:03 +02:00
Norman Maurer
8632d9011e [#1239] Fix possible NPE while fail flush futures during close 2013-04-05 19:23:49 +02:00
Trustin Lee
c08919d0a0 Fix the dead lock described in #1175
- Similar to @normanmaurer's fix in that this commit also makes Bootstrap.init(Channel) asynchronous, but it is simpler and less invasive.
- Also made sure a connection attempt failure in the local transport does not trigger an exceptionCaught event
2013-03-21 19:19:14 +09:00
Trustin Lee
bd8d53eaed Fix checkstyle 2013-03-21 19:02:34 +09:00
Trustin Lee
a980638190 Ensure the best effort is made even if future listeners could not be notified / Handle registration failure in a robust manner
- Related: #1187
2013-03-21 17:48:10 +09:00
Trustin Lee
70a51bcd8d Fix memory leak in AbstractEmbeddedChannel
- Allow a transport implementation to perform an arbitrary task when a channel has been deregistered completely
2013-03-14 15:51:33 +09:00
Norman Maurer
88cc8c1739 [#1065] Provide Future/Promise without channel reference 2013-03-07 07:21:37 +01:00
Trustin Lee
671f9d48d4 Use ConcurrentHashMapV8 wherever possible
- Fixes #1052
2013-02-26 15:54:51 -08:00
Trustin Lee
f67441354a Move logging classes from internal to internal.logging
.. because internal is crowded
2013-02-26 14:54:25 -08:00
Norman Maurer
12b392b4cc Revert "[#1058] Add Channel/ChannelHandlerContext/ChannelPipeline.isWritable()"
This reverts commit 4e36fbca58.
2013-02-22 14:42:32 +01:00
Norman Maurer
4e36fbca58 [#1058] Add Channel/ChannelHandlerContext/ChannelPipeline.isWritable() 2013-02-17 21:13:03 +01:00
Norman Maurer
7cf7d7455d [#1048] Make sure the promise is not notified multiple times on failure 2013-02-12 20:46:39 +01:00
Trustin Lee
b4f4b95739 Move io.netty.logging to io.netty.internal / Move Signal out of internal because we use it in Channel*MessageAdapters 2013-02-11 20:08:18 +09:00
Norman Maurer
33c94a98a3 Let FileRegion extend ReferenceCounted and add ChannelGroup.flush() , ChannelGroup.sendFile(..) 2013-02-10 14:25:53 +01:00
Trustin Lee
e424a2f4b3 Move flushTaskInProgress to AbstractUnsafe
.. because it's referenced only there.  Also did tiny optimizations.
2013-02-09 01:27:54 +09:00
Norman Maurer
a3b46ec9d7 Fix a bug where a closed channel was tried to register with the eventloop 2013-02-08 11:31:48 +01:00
Norman Maurer
539418ecac Let ChannelPipeline.set* methods return itself to be more consistent with the reset 2013-02-08 07:24:55 +01:00
Trustin Lee
d4742bbe16 Clean up abstract ChannelHandler impls / Remove ChannelHandlerContext.hasNext*()
- Rename ChannelHandlerAdapter to ChannelDuplexHandler
- Add ChannelHandlerAdapter that implements only ChannelHandler
- Rename CombinedChannelHandler to CombinedChannelDuplexHandler and
  improve runtime validation
- Remove ChannelInbound/OutboundHandlerAdapter which are not useful
- Make ChannelOutboundByteHandlerAdapter similar to
  ChannelInboundByteHandlerAdapter
- Make the tail and head handler of DefaultChannelPipeline accept both
  bytes and messages.  ChannelHandlerContext.hasNext*() were removed
  because they always return true now.
- Removed various unnecessary null checks.
- Correct method/field names:
  inboundBufferSuspended -> channelReadSuspended
2013-02-07 23:47:45 +09:00
Trustin Lee
42c65cca3a Make MessageBuf bounded
- Move common methods from ByteBuf to Buf
- Rename ensureWritableBytes() to ensureWritable()
- Rename readable() to isReadable()
- Rename writable() to isWritable()
- Add isReadable(int) and isWritable(int)
- Add AbstractMessageBuf
- Rewrite DefaultMessageBuf and QueueBackedMessageBuf
  - based on Josh Bloch's public domain ArrayDeque impl
2013-01-31 18:11:06 +01:00
Trustin Lee
64ae8b6a37 Replace and merge DetectionUtil and DirectByteBufUtil into PlatformDependent and PlatformDependent0
PlatformDependent delegates the operations requires sun.misc.* to PlatformDependent0 to avoid runtime errors due to missing sun.misc.* classes.
2013-01-11 14:03:27 +09:00
Trustin Lee
137f29ba65 Do not read if a channel is inactive 2013-01-09 21:28:31 +09:00
Trustin Lee
dd6b7969b7 Give a handler more control over how its buffers' read bytes are discarded.
This pull request adds two new handler methods: discardInboundReadBytes(ctx) and discardOutboundReadBytes(ctx) to ChannelInboundByteHandler and ChannelOutboundByteHandler respectively. They are called between every inboundBufferUpdated() and flush() respectively. Their default implementation is to call discardSomeReadBytes() on their buffers and a user can override this behavior easily. For example, ReplayingDecoder.discardInboundReadBytes() looks like the following:

    @Override
    public void discardInboundReadBytes(ChannelHandlerContext ctx) throws Exception {
        ByteBuf in = ctx.inboundByteBuffer();
        final int oldReaderIndex = in.readerIndex();
        super.discardInboundReadBytes(ctx);
        final int newReaderIndex = in.readerIndex();
        checkpoint -= oldReaderIndex - newReaderIndex;
    }

If a handler, which has its own buffer index variable, extends ReplayingDecoder or ByteToMessageDecoder, the handler can also override discardInboundReadBytes() and adjust its index variable accordingly.
2013-01-09 13:34:09 +09:00
Trustin Lee
7277536ca6 Remove unnecessary finally block 2013-01-09 13:30:25 +09:00
Norman Maurer
5161ca733c Move utility method to abstract base class and correctly handle expand of buffer also for OIO 2012-12-31 22:09:27 +01:00
Norman Maurer
e0a6dc0ac3 Remove ChannelFutureProgressListener 2012-12-31 23:27:37 +09:00
Norman Maurer
4e77bacdf7 [#873] [#868] Split ChannelFuture into ChannelFuture and ChannelPromise 2012-12-31 23:27:16 +09:00
Trustin Lee
0909878581 Read only when requested (read-on-demand)
This pull request introduces a new operation called read() that replaces the existing inbound traffic control method. EventLoop now performs socket reads only when the read() operation has been issued. Once the requested read() operation is actually performed, EventLoop triggers an inboundBufferSuspended event that tells the handlers that the requested read() operation has been performed and the inbound traffic has been suspended again. A handler can decide to continue reading or not.

Unlike other outbound operations, read() does not use ChannelFuture at all to avoid GC cost. If there's a good reason to create a new future per read at the GC cost, I'll change this.

This pull request consequently removes the readable property in ChannelHandlerContext, which means how the traffic control works changed significantly.

This pull request also adds a new configuration property ChannelOption.AUTO_READ whose default value is true. If true, Netty will call ctx.read() for you. If you need a close control over when read() is called, you can set it to false.

Another interesting fact is that non-terminal handlers do not really need to call read() at all. Only the last inbound handler will have to call it, and that's just enough. Actually, you don't even need to call it at the last handler in most cases because of the ChannelOption.AUTO_READ mentioned above.

There's no serious backward compatibility issue. If the compiler complains your handler does not implement the read() method, add the following:

public void read(ChannelHandlerContext ctx) throws Exception {
    ctx.read();
}

Note that this pull request certainly makes bounded inbound buffer support very easy, but itself does not add the bounded inbound buffer support.
2012-12-31 23:26:00 +09:00
Norman Maurer
b49b3d9c56 [#879] Notify correct ChannelFuture for queued FileRegions 2012-12-31 11:35:25 +01:00
Norman Maurer
ef555d268c Add more javadocs and replace some abstract methods with noops as we often implemented them as noops 2012-12-21 17:06:24 +01:00
Trustin Lee
e59ac8e79b Do not call inbound event methods directly
- Fixes #831

This commit ensures the following events are never triggered as a direct
invocation if they are triggered via ChannelPipeline.fire*():

- channelInactive
- channelUnregistered
- exceptionCaught

This commit also fixes the following issues surfaced by this fix:

- Embedded channel implementations run scheduled tasks too early
- SpdySessionHandlerTest tries to generate inbound data even after the
  channel is closed.
- AioSocketChannel enters into an infinite loop on I/O error.
2012-12-18 03:04:26 +09:00