From 42bdfe94e6cd37b10cf8c96ba823e7f6e619667c Mon Sep 17 00:00:00 2001 From: He Wang Date: Thu, 24 Mar 2022 15:31:22 +0800 Subject: [PATCH] set context to bootstrap attr for all channels (#31) --- .../clogproxy/client/connection/ConnectionFactory.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/logproxy-client/src/main/java/com/oceanbase/clogproxy/client/connection/ConnectionFactory.java b/logproxy-client/src/main/java/com/oceanbase/clogproxy/client/connection/ConnectionFactory.java index 41a71a4..c8d4b37 100644 --- a/logproxy-client/src/main/java/com/oceanbase/clogproxy/client/connection/ConnectionFactory.java +++ b/logproxy-client/src/main/java/com/oceanbase/clogproxy/client/connection/ConnectionFactory.java @@ -58,17 +58,19 @@ public class ConnectionFactory { /** * 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. */ - private Bootstrap initBootstrap(SslContext sslContext) { + private Bootstrap initBootstrap(StreamContext context) { Bootstrap bootstrap = new Bootstrap(); bootstrap + .attr(CONTEXT_KEY, context) .group(WORKER_GROUP) .channel(NettyEventLoopUtil.getClientSocketChannelClass()) .option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.SO_KEEPALIVE, true); + SslContext sslContext = context.getSslContext(); bootstrap.handler( new ChannelInitializer() { @@ -93,13 +95,12 @@ public class ConnectionFactory { * @throws LogProxyClientException If exception occurs. */ 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); ChannelFuture channelFuture = bootstrap.connect( new InetSocketAddress( context.getParams().getHost(), context.getParams().getPort())); - channelFuture.channel().attr(CONTEXT_KEY).set(context); channelFuture.awaitUninterruptibly(); if (!channelFuture.isDone()) { throw new LogProxyClientException(ErrorCode.E_CONNECT, "timeout of create connection!"); -- GitLab