未验证 提交 42bdfe94 编写于 作者: H He Wang 提交者: GitHub

set context to bootstrap attr for all channels (#31)

上级 5b6fe579
...@@ -58,17 +58,19 @@ public class ConnectionFactory { ...@@ -58,17 +58,19 @@ public class ConnectionFactory {
/** /**
* Create a {@link Bootstrap} instance. * Create a {@link Bootstrap} instance.
* *
* @param sslContext The {@link SslContext} used for encrypted communication. * @param context The {@link StreamContext} used in channels.
* @return A {@link Bootstrap} instance. * @return A {@link Bootstrap} instance.
*/ */
private Bootstrap initBootstrap(SslContext sslContext) { private Bootstrap initBootstrap(StreamContext context) {
Bootstrap bootstrap = new Bootstrap(); Bootstrap bootstrap = new Bootstrap();
bootstrap bootstrap
.attr(CONTEXT_KEY, context)
.group(WORKER_GROUP) .group(WORKER_GROUP)
.channel(NettyEventLoopUtil.getClientSocketChannelClass()) .channel(NettyEventLoopUtil.getClientSocketChannelClass())
.option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.TCP_NODELAY, true)
.option(ChannelOption.SO_KEEPALIVE, true); .option(ChannelOption.SO_KEEPALIVE, true);
SslContext sslContext = context.getSslContext();
bootstrap.handler( bootstrap.handler(
new ChannelInitializer<SocketChannel>() { new ChannelInitializer<SocketChannel>() {
...@@ -93,13 +95,12 @@ public class ConnectionFactory { ...@@ -93,13 +95,12 @@ public class ConnectionFactory {
* @throws LogProxyClientException If exception occurs. * @throws LogProxyClientException If exception occurs.
*/ */
public Connection createConnection(StreamContext context) throws LogProxyClientException { public Connection createConnection(StreamContext context) throws LogProxyClientException {
Bootstrap bootstrap = initBootstrap(context.getSslContext()); Bootstrap bootstrap = initBootstrap(context);
bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, ClientConf.CONNECT_TIMEOUT_MS); bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, ClientConf.CONNECT_TIMEOUT_MS);
ChannelFuture channelFuture = ChannelFuture channelFuture =
bootstrap.connect( bootstrap.connect(
new InetSocketAddress( new InetSocketAddress(
context.getParams().getHost(), context.getParams().getPort())); context.getParams().getHost(), context.getParams().getPort()));
channelFuture.channel().attr(CONTEXT_KEY).set(context);
channelFuture.awaitUninterruptibly(); channelFuture.awaitUninterruptibly();
if (!channelFuture.isDone()) { if (!channelFuture.isDone()) {
throw new LogProxyClientException(ErrorCode.E_CONNECT, "timeout of create connection!"); throw new LogProxyClientException(ErrorCode.E_CONNECT, "timeout of create connection!");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册