Issue #5018 - add request timeout onto ClientUpgradeRequest

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2020-07-04 11:37:45 +10:00
parent ae43b70a9f
commit 6a8a6efd1c
2 changed files with 25 additions and 1 deletions

View File

@ -30,6 +30,7 @@ import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.http.HttpField;
@ -68,6 +69,7 @@ public class ClientUpgradeRequest extends UpgradeRequestAdapter
private final String key;
private Object localEndpoint;
private long timeout;
public ClientUpgradeRequest()
{
@ -179,6 +181,27 @@ public class ClientUpgradeRequest extends UpgradeRequestAdapter
}
}
/**
* @param timeout the total timeout for the request/response conversation of the WebSocket handshake;
* use zero or a negative value to disable the timeout
* @param unit the timeout unit
* @return this request object
*/
public ClientUpgradeRequest timeout(long timeout, TimeUnit unit)
{
this.timeout = unit.toMillis(timeout);
return this;
}
/**
* @return the total timeout for this request, in milliseconds;
* zero or negative if the timeout is disabled
*/
public long getTimeout()
{
return timeout;
}
public void setLocalEndpoint(Object websocket)
{
this.localEndpoint = websocket;

View File

@ -30,6 +30,7 @@ import java.util.Objects;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import org.eclipse.jetty.client.HttpClient;
@ -374,7 +375,7 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont
init();
WebSocketUpgradeRequest wsReq = new WebSocketUpgradeRequest(this, httpClient, request);
wsReq.timeout(request.getTimeout() , TimeUnit.MILLISECONDS);
wsReq.setUpgradeListener(upgradeListener);
return wsReq.sendAsync();
}