Package io.grpc.internal
Class Http2Ping
java.lang.Object
io.grpc.internal.Http2Ping
Represents an outstanding PING operation on an HTTP/2 channel. This can be used by HTTP/2-based
transports to implement
ClientTransport.ping(io.grpc.internal.ClientTransport.PingCallback, java.util.concurrent.Executor)
.
A typical transport need only support one outstanding ping at a time. So, if a ping is requested while an operation is already in progress, the given callback is notified when the existing operation completes.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Map
<ClientTransport.PingCallback, Executor> The registered callbacks and the executor used to invoke them.private boolean
False until the operation completes, either successfully (other side sent acknowledgement) or unsuccessfully.private final long
The PING frame includes 8 octets of payload data, e.g.private Throwable
If non-null, indicates the ping failed.private static final Logger
private long
The round-trip time for the ping, in nanoseconds.private final com.google.common.base.Stopwatch
Used to measure elapsed time. -
Constructor Summary
ConstructorsConstructorDescriptionHttp2Ping
(long data, com.google.common.base.Stopwatch stopwatch) Creates a new ping operation. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addCallback
(ClientTransport.PingCallback callback, Executor executor) Registers a callback that is invoked when the ping operation completes.private static Runnable
asRunnable
(ClientTransport.PingCallback callback, long roundTripTimeNanos) Returns a runnable that, when run, invokes the given callback, providing the given round-trip duration.private static Runnable
asRunnable
(ClientTransport.PingCallback callback, Throwable failureCause) Returns a runnable that, when run, invokes the given callback, providing the given cause of failure.boolean
complete()
Completes this operation successfully.private static void
Executes the given runnable.void
Completes this operation exceptionally.static void
notifyFailed
(ClientTransport.PingCallback callback, Executor executor, Throwable cause) Notifies the given callback that the ping operation failed.long
payload()
Returns the expected ping payload for this outstanding operation.
-
Field Details
-
log
-
data
private final long dataThe PING frame includes 8 octets of payload data, e.g. 64 bits. -
stopwatch
private final com.google.common.base.Stopwatch stopwatchUsed to measure elapsed time. -
callbacks
The registered callbacks and the executor used to invoke them. -
completed
private boolean completedFalse until the operation completes, either successfully (other side sent acknowledgement) or unsuccessfully. -
failureCause
If non-null, indicates the ping failed. -
roundTripTimeNanos
private long roundTripTimeNanosThe round-trip time for the ping, in nanoseconds. This value is only meaningful whencompleted
is true andfailureCause
is null.
-
-
Constructor Details
-
Http2Ping
public Http2Ping(long data, com.google.common.base.Stopwatch stopwatch) Creates a new ping operation. The caller is responsible for sending a ping on an HTTP/2 channel using the given payload. The caller is also responsible for starting the stopwatch when the PING frame is sent.- Parameters:
data
- the ping payloadstopwatch
- a stopwatch for measuring round-trip time
-
-
Method Details
-
addCallback
Registers a callback that is invoked when the ping operation completes. If this ping operation is already completed, the callback is invoked immediately.- Parameters:
callback
- the callback to invokeexecutor
- the executor to use
-
payload
public long payload()Returns the expected ping payload for this outstanding operation.- Returns:
- the expected payload for this outstanding ping
-
complete
public boolean complete()Completes this operation successfully. The stopwatch given during construction is used to measure the elapsed time. Registered callbacks are invoked and provided the measured elapsed time.- Returns:
- true if the operation is marked as complete; false if it was already complete
-
failed
Completes this operation exceptionally. Registered callbacks are invoked and provided the given throwable as the cause of failure.- Parameters:
failureCause
- the cause of failure
-
notifyFailed
public static void notifyFailed(ClientTransport.PingCallback callback, Executor executor, Throwable cause) Notifies the given callback that the ping operation failed.- Parameters:
callback
- the callbackexecutor
- the executor used to invoke the callbackcause
- the cause of failure
-
doExecute
Executes the given runnable. This prevents exceptions from propagating so that an exception thrown by one callback won't prevent subsequent callbacks from being executed. -
asRunnable
Returns a runnable that, when run, invokes the given callback, providing the given round-trip duration. -
asRunnable
Returns a runnable that, when run, invokes the given callback, providing the given cause of failure.
-