From ed2ea884d87b1133704ee33a486292bd5d8f6a53 Mon Sep 17 00:00:00 2001 From: Julien Viet Date: Tue, 29 Oct 2019 21:43:42 +0100 Subject: [PATCH] Avoid using Promise.promise in HTTP/2 ping/pong pong frame handler --- .../core/http/impl/Http2ConnectionBase.java | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/main/java/io/vertx/core/http/impl/Http2ConnectionBase.java b/src/main/java/io/vertx/core/http/impl/Http2ConnectionBase.java index d5089a284..59229700c 100644 --- a/src/main/java/io/vertx/core/http/impl/Http2ConnectionBase.java +++ b/src/main/java/io/vertx/core/http/impl/Http2ConnectionBase.java @@ -79,7 +79,7 @@ abstract class Http2ConnectionBase extends ConnectionBase implements Http2FrameL private boolean shutdown; private Handler remoteSettingsHandler; private final ArrayDeque> updateSettingsHandlers = new ArrayDeque<>(); - private final ArrayDeque>> pongHandlers = new ArrayDeque<>(); + private final ArrayDeque> pongHandlers = new ArrayDeque<>(); private Http2Settings localSettings = new Http2Settings(); private Http2Settings remoteSettings; private Handler goAwayHandler; @@ -275,11 +275,11 @@ abstract class Http2ConnectionBase extends ConnectionBase implements Http2FrameL } @Override - public void onPingAckRead(ChannelHandlerContext ctx, long data) throws Http2Exception { - Handler> handler = pongHandlers.poll(); + public void onPingAckRead(ChannelHandlerContext ctx, long data) { + Promise handler = pongHandlers.poll(); if (handler != null) { Buffer buff = Buffer.buffer().appendLong(data); - context.dispatch(Future.succeededFuture(buff), handler); + handler.complete(buff); } } @@ -476,25 +476,28 @@ abstract class Http2ConnectionBase extends ConnectionBase implements Http2FrameL @Override public Future ping(Buffer data) { - Promise promise = Promise.promise(); - ping(data, promise); + if (data.length() != 8) { + throw new IllegalArgumentException("Ping data must be exactly 8 bytes"); + } + Promise promise = context.promise(); + handler.writePing(data.getLong(0)).addListener(fut -> { + if (fut.isSuccess()) { + synchronized (Http2ConnectionBase.this) { + pongHandlers.add(promise); + } + } else { + promise.fail(fut.cause()); + } + }); return promise.future(); } @Override public HttpConnection ping(Buffer data, Handler> pongHandler) { - if (data.length() != 8) { - throw new IllegalArgumentException("Ping data must be exactly 8 bytes"); + Future fut = ping(data); + if (pongHandler != null) { + fut.setHandler(pongHandler); } - handler.writePing(data.getLong(0)).addListener(fut -> { - if (fut.isSuccess()) { - synchronized (Http2ConnectionBase.this) { - pongHandlers.add(pongHandler); - } - } else { - pongHandler.handle(Future.failedFuture(fut.cause())); - } - }); return this; }