Skip to content

Commit

Permalink
[JENKINS-69955] Make websocket connection idleTimeout configurable (j…
Browse files Browse the repository at this point in the history
…enkinsci#7670)

* [JENKINS-69955] Make websocket connection idleTimeout configurable and adjust the default

* [JENKINS-69955] Do not cache JettyWebSocketServerContainer

* [JENKINS-69955] AtomicBoolean is not needed

* [JENKINS-69955] remove unused import

---------

Co-authored-by: Tim Jacomb <[email protected]>
Co-authored-by: Alexander Brandes <[email protected]>
  • Loading branch information
3 people authored Mar 12, 2023
1 parent f54693d commit 6c076b9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/src/main/java/jenkins/websocket/WebSocketSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public abstract class WebSocketSession {
/**
* Number of seconds between server-sent pings.
* Zero to disable.
* This value must be lower than the <code>jenkins.websocket.idleTimeout</code>.
* <p><a href="https://nginx.org/en/docs/http/websocket.html">nginx docs</a> claim 60s timeout and this seems to match experiments.
* <a href="https://cloud.google.com/kubernetes-engine/docs/concepts/ingress#support_for_websocket">GKE docs</a> says 30s
* but this is a total timeout, not inactivity, so you need to set {@code BackendConfigSpec.timeoutSec} anyway.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.io.IOException;
import java.nio.ByteBuffer;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import javax.servlet.http.HttpServletRequest;
Expand All @@ -44,14 +45,34 @@
@MetaInfServices(Provider.class)
public class Jetty10Provider implements Provider {

/**
* Number of seconds a WebsocketConnection may stay idle until it expires.
* Zero to disable.
* This value must be higher than the <code>jenkins.websocket.pingInterval</code>.
* Per <a href=https://www.eclipse.org/jetty/documentation/jetty-10/programming-guide/index.html#pg-websocket-session-ping>Jetty 10 documentation</a>
* a ping mechanism should keep the websocket active. Therefore, the idle timeout must be higher than the ping
* interval to avoid timeout issues.
*/
private static long IDLE_TIMEOUT_SECONDS = Long.getLong("jenkins.websocket.idleTimeout", 60L);

private static final String ATTR_LISTENER = Jetty10Provider.class.getName() + ".listener";

private boolean initialized = false;

public Jetty10Provider() {
JettyWebSocketServerContainer.class.hashCode();
}

private void init(HttpServletRequest req) {
if (!initialized) {
JettyWebSocketServerContainer.getContainer(req.getServletContext()).setIdleTimeout(Duration.ofSeconds(IDLE_TIMEOUT_SECONDS));
initialized = true;
}
}

@Override
public Handler handle(HttpServletRequest req, HttpServletResponse rsp, Listener listener) throws Exception {
init(req);
req.setAttribute(ATTR_LISTENER, listener);
// TODO Jetty 10 has no obvious equivalent to WebSocketServerFactory.isUpgradeRequest; RFC6455Negotiation?
if (!"websocket".equalsIgnoreCase(req.getHeader("Upgrade"))) {
Expand Down

0 comments on commit 6c076b9

Please sign in to comment.