Quellcode durchsuchen

Make TransportResponse.Empty a singleton (#104526)

We use `TransportResponse.Empty.INSTANCE` almost everywhere anyway, but
there's a couple of places where we create new instances of this class
unnecessarily. This commit makes the constructor private so that
`INSTANCE` is the only possible instance.
David Turner vor 1 Jahr
Ursprung
Commit
46f922aa66

+ 2 - 0
server/src/main/java/org/elasticsearch/transport/TransportResponse.java

@@ -32,6 +32,8 @@ public abstract class TransportResponse extends TransportMessage {
     public static class Empty extends TransportResponse {
         public static final Empty INSTANCE = new Empty();
 
+        private Empty() {/* singleton */}
+
         @Override
         public String toString() {
             return "Empty{}";

+ 1 - 1
server/src/test/java/org/elasticsearch/cluster/coordination/NodeJoinTests.java

@@ -195,7 +195,7 @@ public class NodeJoinTests extends ESTestCase {
                     );
                 } else if (action.equals(JoinValidationService.JOIN_VALIDATE_ACTION_NAME)
                     || action.equals(JoinHelper.JOIN_PING_ACTION_NAME)) {
-                        handleResponse(requestId, new TransportResponse.Empty());
+                        handleResponse(requestId, TransportResponse.Empty.INSTANCE);
                     } else {
                         super.onSendRequest(requestId, action, request, destination);
                     }

+ 1 - 1
x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/exchange/ExchangeService.java

@@ -169,7 +169,7 @@ public final class ExchangeService extends AbstractLifecycleComponent {
         @Override
         public void messageReceived(OpenExchangeRequest request, TransportChannel channel, Task task) throws Exception {
             createSinkHandler(request.sessionId, request.exchangeBuffer);
-            channel.sendResponse(new TransportResponse.Empty());
+            channel.sendResponse(TransportResponse.Empty.INSTANCE);
         }
     }