mirror of
https://github.com/signalapp/Signal-Server
synced 2026-04-19 09:38:02 +01:00
Switch websocket-resources from ListenableFuture to CompletableFuture
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
/**
|
||||
/*
|
||||
* Copyright (C) 2014 Open WhisperSystems
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
@@ -16,8 +16,6 @@
|
||||
*/
|
||||
package org.whispersystems.websocket;
|
||||
|
||||
import com.google.common.util.concurrent.ListenableFuture;
|
||||
import com.google.common.util.concurrent.SettableFuture;
|
||||
import org.eclipse.jetty.websocket.api.RemoteEndpoint;
|
||||
import org.eclipse.jetty.websocket.api.Session;
|
||||
import org.eclipse.jetty.websocket.api.WebSocketException;
|
||||
@@ -34,20 +32,21 @@ import java.security.SecureRandom;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
|
||||
public class WebSocketClient {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(WebSocketClient.class);
|
||||
|
||||
private final Session session;
|
||||
private final RemoteEndpoint remoteEndpoint;
|
||||
private final WebSocketMessageFactory messageFactory;
|
||||
private final Map<Long, SettableFuture<WebSocketResponseMessage>> pendingRequestMapper;
|
||||
private final Session session;
|
||||
private final RemoteEndpoint remoteEndpoint;
|
||||
private final WebSocketMessageFactory messageFactory;
|
||||
private final Map<Long, CompletableFuture<WebSocketResponseMessage>> pendingRequestMapper;
|
||||
|
||||
public WebSocketClient(Session session, RemoteEndpoint remoteEndpoint,
|
||||
WebSocketMessageFactory messageFactory,
|
||||
Map<Long, SettableFuture<WebSocketResponseMessage>> pendingRequestMapper)
|
||||
Map<Long, CompletableFuture<WebSocketResponseMessage>> pendingRequestMapper)
|
||||
{
|
||||
this.session = session;
|
||||
this.remoteEndpoint = remoteEndpoint;
|
||||
@@ -55,12 +54,12 @@ public class WebSocketClient {
|
||||
this.pendingRequestMapper = pendingRequestMapper;
|
||||
}
|
||||
|
||||
public ListenableFuture<WebSocketResponseMessage> sendRequest(String verb, String path,
|
||||
List<String> headers,
|
||||
Optional<byte[]> body)
|
||||
public CompletableFuture<WebSocketResponseMessage> sendRequest(String verb, String path,
|
||||
List<String> headers,
|
||||
Optional<byte[]> body)
|
||||
{
|
||||
final long requestId = generateRequestId();
|
||||
final SettableFuture<WebSocketResponseMessage> future = SettableFuture.create();
|
||||
final long requestId = generateRequestId();
|
||||
final CompletableFuture<WebSocketResponseMessage> future = new CompletableFuture<>();
|
||||
|
||||
pendingRequestMapper.put(requestId, future);
|
||||
|
||||
@@ -72,7 +71,7 @@ public class WebSocketClient {
|
||||
public void writeFailed(Throwable x) {
|
||||
logger.debug("Write failed", x);
|
||||
pendingRequestMapper.remove(requestId);
|
||||
future.setException(x);
|
||||
future.completeExceptionally(x);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -81,7 +80,7 @@ public class WebSocketClient {
|
||||
} catch (WebSocketException e) {
|
||||
logger.debug("Write", e);
|
||||
pendingRequestMapper.remove(requestId);
|
||||
future.setException(e);
|
||||
future.completeExceptionally(e);
|
||||
}
|
||||
|
||||
return future;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/**
|
||||
/*
|
||||
* Copyright (C) 2014 Open WhisperSystems
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
@@ -17,7 +17,6 @@
|
||||
package org.whispersystems.websocket;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.google.common.util.concurrent.SettableFuture;
|
||||
import org.eclipse.jetty.server.RequestLog;
|
||||
import org.eclipse.jetty.websocket.api.RemoteEndpoint;
|
||||
import org.eclipse.jetty.websocket.api.Session;
|
||||
@@ -48,6 +47,7 @@ import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
|
||||
@@ -56,7 +56,7 @@ public class WebSocketResourceProvider implements WebSocketListener {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(WebSocketResourceProvider.class);
|
||||
|
||||
private final Map<Long, SettableFuture<WebSocketResponseMessage>> requestMap = new ConcurrentHashMap<>();
|
||||
private final Map<Long, CompletableFuture<WebSocketResponseMessage>> requestMap = new ConcurrentHashMap<>();
|
||||
|
||||
private final Object authenticated;
|
||||
private final WebSocketMessageFactory messageFactory;
|
||||
@@ -131,10 +131,10 @@ public class WebSocketResourceProvider implements WebSocketListener {
|
||||
context.notifyClosed(statusCode, reason);
|
||||
|
||||
for (long requestId : requestMap.keySet()) {
|
||||
SettableFuture outstandingRequest = requestMap.remove(requestId);
|
||||
CompletableFuture outstandingRequest = requestMap.remove(requestId);
|
||||
|
||||
if (outstandingRequest != null) {
|
||||
outstandingRequest.setException(new IOException("Connection closed!"));
|
||||
outstandingRequest.completeExceptionally(new IOException("Connection closed!"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -160,10 +160,10 @@ public class WebSocketResourceProvider implements WebSocketListener {
|
||||
}
|
||||
|
||||
private void handleResponse(WebSocketResponseMessage responseMessage) {
|
||||
SettableFuture<WebSocketResponseMessage> future = requestMap.remove(responseMessage.getRequestId());
|
||||
CompletableFuture<WebSocketResponseMessage> future = requestMap.remove(responseMessage.getRequestId());
|
||||
|
||||
if (future != null) {
|
||||
future.set(responseMessage);
|
||||
future.complete(responseMessage);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -197,7 +197,7 @@ public class WebSocketResourceProvider implements WebSocketListener {
|
||||
error.getStatus(),
|
||||
"Error response",
|
||||
headers,
|
||||
Optional.<byte[]>empty());
|
||||
Optional.empty());
|
||||
|
||||
remoteEndpoint.sendBytesByFuture(ByteBuffer.wrap(response.toByteArray()));
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/**
|
||||
/*
|
||||
* Copyright (C) 2014 Open WhisperSystems
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
|
||||
Reference in New Issue
Block a user