From e5c2c59f789dd4cf87c6c18dc685242f7abec1c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E9=91=AB?= Date: Fri, 22 Mar 2019 15:09:46 +0800 Subject: [PATCH 1/3] 1 Optimize performance when many new connections arrive --- .../TcpNioServerConnectionFactory.java | 64 ++++++++++--------- 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioServerConnectionFactory.java b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioServerConnectionFactory.java index a53f8cec9de..c6a095e65b2 100644 --- a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioServerConnectionFactory.java +++ b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioServerConnectionFactory.java @@ -201,38 +201,44 @@ private void doSelect(ServerSocketChannel server, final Selector selector) throw @Override protected void doAccept(final Selector selector, ServerSocketChannel server, long now) throws IOException { logger.debug("New accept"); - SocketChannel channel = server.accept(); - if (isShuttingDown()) { - if (logger.isInfoEnabled()) { - logger.info("New connection from " + channel.socket().getInetAddress().getHostAddress() - + ":" + channel.socket().getPort() - + " rejected; the server is in the process of shutting down."); + // when many new connections arrive, we should + // accept connections in a for loop until no new connection is ready + for(;;){ + SocketChannel channel = server.accept(); + if(channel == null) + return; + if (isShuttingDown()) { + if (logger.isInfoEnabled()) { + logger.info("New connection from " + channel.socket().getInetAddress().getHostAddress() + + ":" + channel.socket().getPort() + + " rejected; the server is in the process of shutting down."); + } + channel.close(); } - channel.close(); - } - else { - try { - channel.configureBlocking(false); - Socket socket = channel.socket(); - setSocketAttributes(socket); - TcpNioConnection connection = createTcpNioConnection(channel); - if (connection == null) { - return; + else { + try { + channel.configureBlocking(false); + Socket socket = channel.socket(); + setSocketAttributes(socket); + TcpNioConnection connection = createTcpNioConnection(channel); + if (connection == null) { + return; + } + connection.setTaskExecutor(getTaskExecutor()); + connection.setLastRead(now); + if (getSslHandshakeTimeout() != null && connection instanceof TcpNioSSLConnection) { + ((TcpNioSSLConnection) connection).setHandshakeTimeout(getSslHandshakeTimeout()); + } + this.channelMap.put(channel, connection); + channel.register(selector, SelectionKey.OP_READ, connection); + connection.publishConnectionOpenEvent(); } - connection.setTaskExecutor(getTaskExecutor()); - connection.setLastRead(now); - if (getSslHandshakeTimeout() != null && connection instanceof TcpNioSSLConnection) { - ((TcpNioSSLConnection) connection).setHandshakeTimeout(getSslHandshakeTimeout()); + catch (Exception e) { + logger.error("Exception accepting new connection from " + + channel.socket().getInetAddress().getHostAddress() + + ":" + channel.socket().getPort(), e); + channel.close(); } - this.channelMap.put(channel, connection); - channel.register(selector, SelectionKey.OP_READ, connection); - connection.publishConnectionOpenEvent(); - } - catch (Exception e) { - logger.error("Exception accepting new connection from " - + channel.socket().getInetAddress().getHostAddress() - + ":" + channel.socket().getPort(), e); - channel.close(); } } } From 34c73364e8496a96f82f27ed4600a11537c07270 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E9=91=AB?= Date: Fri, 22 Mar 2019 17:43:25 +0800 Subject: [PATCH 2/3] Fix code style --- .../ip/tcp/connection/TcpNioServerConnectionFactory.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioServerConnectionFactory.java b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioServerConnectionFactory.java index c6a095e65b2..3bc47713cea 100644 --- a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioServerConnectionFactory.java +++ b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioServerConnectionFactory.java @@ -203,10 +203,11 @@ protected void doAccept(final Selector selector, ServerSocketChannel server, lon logger.debug("New accept"); // when many new connections arrive, we should // accept connections in a for loop until no new connection is ready - for(;;){ + for (;;) { SocketChannel channel = server.accept(); - if(channel == null) + if(channel == null) { return; + } if (isShuttingDown()) { if (logger.isInfoEnabled()) { logger.info("New connection from " + channel.socket().getInetAddress().getHostAddress() From dd1b3430766159e1f4e9501fc2d24e85ef84ef00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E9=91=AB?= Date: Mon, 25 Mar 2019 15:13:49 +0800 Subject: [PATCH 3/3] Fix code style --- .../ip/tcp/connection/TcpNioServerConnectionFactory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioServerConnectionFactory.java b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioServerConnectionFactory.java index 3bc47713cea..a2f18196961 100644 --- a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioServerConnectionFactory.java +++ b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioServerConnectionFactory.java @@ -205,7 +205,7 @@ protected void doAccept(final Selector selector, ServerSocketChannel server, lon // accept connections in a for loop until no new connection is ready for (;;) { SocketChannel channel = server.accept(); - if(channel == null) { + if (channel == null) { return; } if (isShuttingDown()) {