From 5055776cd81ec553fe4898c96361f8ea99896099 Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Fri, 27 Mar 2015 14:20:02 -0700 Subject: [PATCH] Set proper socket parameters suitable for CLI communication Keep-alive option ensures that the connection won't hang forever. NoDelay option improves performance as the communication over the socket is already datagram oriented. See: https://developer.cloudbees.com/bin/view/DEV/On-Premise+Executors --- cli/src/main/java/hudson/cli/CLI.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/cli/src/main/java/hudson/cli/CLI.java b/cli/src/main/java/hudson/cli/CLI.java index 6a3a05c287..fcabd3351e 100644 --- a/cli/src/main/java/hudson/cli/CLI.java +++ b/cli/src/main/java/hudson/cli/CLI.java @@ -169,12 +169,18 @@ public class CLI { private Channel connectViaCliPort(URL jenkins, CliPort clip) throws IOException { LOGGER.log(FINE, "Trying to connect directly via TCP/IP to {0}", clip.endpoint); - final Socket s; + final Socket s = new Socket(); + // this prevents a connection from silently terminated by the router in between or the other peer + // and that goes without unnoticed. However, the time out is often very long (for example 2 hours + // by default in Linux) that this alone is enough to prevent that. + s.setKeepAlive(true); + // we take care of buffering on our own + s.setTcpNoDelay(true); OutputStream out; if (httpsProxyTunnel!=null) { String[] tokens = httpsProxyTunnel.split(":"); - s = new Socket(tokens[0], Integer.parseInt(tokens[1])); + s.connect(new InetSocketAddress(tokens[0], Integer.parseInt(tokens[1]))); PrintStream o = new PrintStream(s.getOutputStream()); o.print("CONNECT " + clip.endpoint.getHostName() + ":" + clip.endpoint.getPort() + " HTTP/1.0\r\n\r\n"); @@ -199,7 +205,6 @@ public class CLI { } }; } else { - s = new Socket(); s.connect(clip.endpoint,3000); out = SocketChannelStream.out(s); } -- GitLab