Skip to content

Commit e3f3526

Browse files
stephencoleg-nenashev
authored andcommitted
[FIXED JENKINS-38539] Turn on SO_KEEPALIVE and provide CLI option to turn it off again (#110)
* [FIXED JENKINS-38539] Turn on SO_KEEPALIVE and provide CLI option to turn it off again - Most OSes have a default SO_KEEPALIVE of 2 hours, and perform keepalive without generating any significant traffic The master side of the connection already has SO_KEEPALIVE enabled, this just allows both OSes to keep their own guidance and therefore assist when tuning the agent side is more appropriate than changing the kernel parameters on the master side (as the master is handling the HTTP requests of users) - Would probably be perfectly safe to not even expose the -noKeepAlive option as the SO_KEEPALIVE should be invisible But there may be users that have the requirement to disable, so safer to provide the option - Change was developed against the stable-2.x branch * [JENKINS-38541] Tweak help text for CLI -noKeepAlive option
1 parent 4f01088 commit e3f3526

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

src/main/java/hudson/remoting/Engine.java

+26
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@ public void run() {
138138

139139
private boolean noReconnect;
140140

141+
/**
142+
* Determines whether the socket will have {@link Socket#setKeepAlive(boolean)} set or not.
143+
*
144+
* @since TODO
145+
*/
146+
private boolean keepAlive = true;
147+
141148
private JarCache jarCache = new FileSystemJarCache(new File(System.getProperty("user.home"),".jenkins/cache/jars"),true);
142149

143150
public Engine(EngineListener listener, List<URL> hudsonUrls, String secretKey, String slaveName) {
@@ -178,6 +185,24 @@ public void setNoReconnect(boolean noReconnect) {
178185
this.noReconnect = noReconnect;
179186
}
180187

188+
/**
189+
* Returns {@code true} if and only if the socket to the master will have {@link Socket#setKeepAlive(boolean)} set.
190+
*
191+
* @return {@code true} if and only if the socket to the master will have {@link Socket#setKeepAlive(boolean)} set.
192+
*/
193+
public boolean isKeepAlive() {
194+
return keepAlive;
195+
}
196+
197+
/**
198+
* Sets the {@link Socket#setKeepAlive(boolean)} to use for the connection to the master.
199+
*
200+
* @param keepAlive the {@link Socket#setKeepAlive(boolean)} to use for the connection to the master.
201+
*/
202+
public void setKeepAlive(boolean keepAlive) {
203+
this.keepAlive = keepAlive;
204+
}
205+
181206
public void setCandidateCertificates(List<X509Certificate> candidateCertificates) {
182207
this.candidateCertificates = candidateCertificates == null
183208
? null
@@ -409,6 +434,7 @@ private Socket connect(@CheckForNull String host, @CheckForNull String port) thr
409434
s.connect(targetAddress);
410435

411436
s.setTcpNoDelay(true); // we'll do buffering by ourselves
437+
s.setKeepAlive(keepAlive);
412438

413439
// set read time out to avoid infinite hang. the time out should be long enough so as not
414440
// to interfere with normal operation. the main purpose of this is that when the other peer dies

src/main/java/hudson/remoting/Launcher.java

+7
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ public boolean verify(String s, SSLSession sslSession) {
189189
@Option(name="-noReconnect",usage="Doesn't try to reconnect when a communication fail, and exit instead")
190190
public boolean noReconnect = false;
191191

192+
@Option(name = "-noKeepAlive",
193+
usage = "Disable TCP socket keep alive on connection to the master.")
194+
public boolean noKeepAlive = false;
195+
192196
public static void main(String... args) throws Exception {
193197
Launcher launcher = new Launcher();
194198
CmdLineParser parser = new CmdLineParser(launcher);
@@ -229,6 +233,9 @@ public void run() throws Exception {
229233
if (this.noReconnect) {
230234
jnlpArgs.add("-noreconnect");
231235
}
236+
if (this.noKeepAlive) {
237+
jnlpArgs.add("-noKeepAlive");
238+
}
232239
try {
233240
hudson.remoting.jnlp.Main._main(jnlpArgs.toArray(new String[jnlpArgs.size()]));
234241
} catch (CmdLineException e) {

src/main/java/hudson/remoting/jnlp/Main.java

+5
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ public class Main {
8585
usage="If the connection ends, don't retry and just exit.")
8686
public boolean noReconnect = false;
8787

88+
@Option(name="-noKeepAlive",
89+
usage="Disable TCP socket keep alive on connection to the master.")
90+
public boolean noKeepAlive = false;
91+
8892
@Option(name = "-cert",
8993
usage = "Specify additional X.509 encoded PEM certificates to trust when connecting to Jenkins " +
9094
"root URLs. If starting with @ then the remainder is assumed to be the name of the " +
@@ -173,6 +177,7 @@ public Engine createEngine() {
173177
if(jarCache!=null)
174178
engine.setJarCache(new FileSystemJarCache(jarCache,true));
175179
engine.setNoReconnect(noReconnect);
180+
engine.setKeepAlive(!noKeepAlive);
176181
if (candidateCertificates != null && !candidateCertificates.isEmpty()) {
177182
CertificateFactory factory;
178183
try {

0 commit comments

Comments
 (0)