write to channel immediately after async connect

468 views
Skip to first unread message

adria...@gmail.com

unread,
Jan 5, 2016, 12:57:14 AM1/5/16
to Netty discussions
hi there,

I'm writing a RPC client using netty4.  When the first message is pushed to the RPC client, the client chooses a server based on load balance logic and calls bootstrap.connect(host, port) and then calls channel.write().  The flush() method is called in the listener of the connect future.  And once the channel is active, the future messages will trigger channel.writeAndFlush().  I made it this way as I don't want to maintain a queue myself as I know netty has outbound buffer to store the writes before flush.

The problem is that sometimes the first message's write operation is failed:
2016-01-04 14:42:58 DEBUG com.qq.gdt.rpc.netty.Connection$2.operationComplete(Connection.java:144) Send failed to 127.0.0.1, 59262, cause: java.lang.UnsupportedOperationException: unsupported message type: DefaultFullHttpRequest (expected: ByteBuf, FileRegion)

In my ChannelInitializer<SocketChannel> I have:
  @Override
  protected void initChannel(SocketChannel ch) throws Exception {
    ch.pipeline().addLast(new HttpClientCodec());
    ch.pipeline().addLast(new NettyHttpRpcHandler());
  }

So I think the problem is that the channel is not registered when I call write().

Am I right?
Should I use netty this way or should I use a queue on my own?  Thanks.

Norman Maurer

unread,
Jan 5, 2016, 3:51:07 AM1/5/16
to ne...@googlegroups.com
Yeah that sounds right... maybe you could just "block" until the registration is complete ?

--
You received this message because you are subscribed to the Google Groups "Netty discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netty+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netty/dd4b7805-1271-47e5-a895-1638a505463e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

이희승 (Trustin Lee)

unread,
Jan 5, 2016, 5:49:56 AM1/5/16
to ne...@googlegroups.com
Any write attempt that were made before the successful connection establishment will marked as failed.
 
You could keep the pending messages in a queue, but you can add multiple listeners to the connect future instead:
 
    connectFuture.addListener(new ChannelFuture() { ... connectFuture.channel().write(...); ... });
 
Perhaps we could add a new ChannelFutureListener implementation that writes a message when future is done. WDYT, Norman?
 

Adrian Liu

unread,
Jan 10, 2016, 9:14:15 PM1/10/16
to Netty discussions
Sorry for my late response.  We do not want to block the callers of our rpc client, though I know some systems like grpc blocks until the registration is complete.  We planned to use a timeout logic to call the callbacks provided by the callers of our rpc client when the connection can't be established.

在 2016年1月5日星期二 UTC+8下午4:51:07,Norman Maurer写道:

Adrian Liu

unread,
Jan 10, 2016, 9:19:38 PM1/10/16
to Netty discussions
I'm not very clear of the second option:
1. I'm using netty4, and the addListener() method needs a Listener as the argument.
2. Callers of our rpc client triggers the rpc requests at any time.  How can I write to netty channel in my connect future listener without a queue?

Could you please provide more details? Thanks in advance!

在 2016年1月5日星期二 UTC+8下午6:49:56,Trustin Lee写道:
Reply all
Reply to author
Forward
0 new messages