use of com.google.protobuf.Duration in project grpc-java by grpc.
the class BinlogHelper method getClientInterceptor.
public ClientInterceptor getClientInterceptor(final long callId) {
return new ClientInterceptor() {
boolean trailersOnlyResponse = true;
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(final MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
final AtomicLong seq = new AtomicLong(1);
final String methodName = method.getFullMethodName();
final String authority = next.authority();
// The timeout should reflect the time remaining when the call is started, so do not
// compute remaining time here.
final Deadline deadline = min(callOptions.getDeadline(), Context.current().getDeadline());
return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
@Override
public void start(final ClientCall.Listener<RespT> responseListener, Metadata headers) {
final Duration timeout = deadline == null ? null : Durations.fromNanos(deadline.timeRemaining(TimeUnit.NANOSECONDS));
writer.logClientHeader(seq.getAndIncrement(), methodName, authority, timeout, headers, GrpcLogEntry.Logger.LOGGER_CLIENT, callId, /*peerAddress=*/
null);
ClientCall.Listener<RespT> wListener = new SimpleForwardingClientCallListener<RespT>(responseListener) {
@Override
public void onMessage(RespT message) {
writer.logRpcMessage(seq.getAndIncrement(), EventType.EVENT_TYPE_SERVER_MESSAGE, method.getResponseMarshaller(), message, GrpcLogEntry.Logger.LOGGER_CLIENT, callId);
super.onMessage(message);
}
@Override
public void onHeaders(Metadata headers) {
trailersOnlyResponse = false;
writer.logServerHeader(seq.getAndIncrement(), headers, GrpcLogEntry.Logger.LOGGER_CLIENT, callId, getPeerSocket(getAttributes()));
super.onHeaders(headers);
}
@Override
public void onClose(Status status, Metadata trailers) {
SocketAddress peer = trailersOnlyResponse ? getPeerSocket(getAttributes()) : null;
writer.logTrailer(seq.getAndIncrement(), status, trailers, GrpcLogEntry.Logger.LOGGER_CLIENT, callId, peer);
super.onClose(status, trailers);
}
};
super.start(wListener, headers);
}
@Override
public void sendMessage(ReqT message) {
writer.logRpcMessage(seq.getAndIncrement(), EventType.EVENT_TYPE_CLIENT_MESSAGE, method.getRequestMarshaller(), message, GrpcLogEntry.Logger.LOGGER_CLIENT, callId);
super.sendMessage(message);
}
@Override
public void halfClose() {
writer.logHalfClose(seq.getAndIncrement(), GrpcLogEntry.Logger.LOGGER_CLIENT, callId);
super.halfClose();
}
@Override
public void cancel(String message, Throwable cause) {
writer.logCancel(seq.getAndIncrement(), GrpcLogEntry.Logger.LOGGER_CLIENT, callId);
super.cancel(message, cause);
}
};
}
};
}
use of com.google.protobuf.Duration in project grpc-java by grpc.
the class BinlogHelper method getServerInterceptor.
public ServerInterceptor getServerInterceptor(final long callId) {
return new ServerInterceptor() {
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(final ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
final AtomicLong seq = new AtomicLong(1);
SocketAddress peer = getPeerSocket(call.getAttributes());
String methodName = call.getMethodDescriptor().getFullMethodName();
String authority = call.getAuthority();
Deadline deadline = Context.current().getDeadline();
final Duration timeout = deadline == null ? null : Durations.fromNanos(deadline.timeRemaining(TimeUnit.NANOSECONDS));
writer.logClientHeader(seq.getAndIncrement(), methodName, authority, timeout, headers, GrpcLogEntry.Logger.LOGGER_SERVER, callId, peer);
ServerCall<ReqT, RespT> wCall = new SimpleForwardingServerCall<ReqT, RespT>(call) {
@Override
public void sendMessage(RespT message) {
writer.logRpcMessage(seq.getAndIncrement(), EventType.EVENT_TYPE_SERVER_MESSAGE, call.getMethodDescriptor().getResponseMarshaller(), message, GrpcLogEntry.Logger.LOGGER_SERVER, callId);
super.sendMessage(message);
}
@Override
public void sendHeaders(Metadata headers) {
writer.logServerHeader(seq.getAndIncrement(), headers, GrpcLogEntry.Logger.LOGGER_SERVER, callId, /*peerAddress=*/
null);
super.sendHeaders(headers);
}
@Override
public void close(Status status, Metadata trailers) {
writer.logTrailer(seq.getAndIncrement(), status, trailers, GrpcLogEntry.Logger.LOGGER_SERVER, callId, /*peerAddress=*/
null);
super.close(status, trailers);
}
};
return new SimpleForwardingServerCallListener<ReqT>(next.startCall(wCall, headers)) {
@Override
public void onMessage(ReqT message) {
writer.logRpcMessage(seq.getAndIncrement(), EventType.EVENT_TYPE_CLIENT_MESSAGE, call.getMethodDescriptor().getRequestMarshaller(), message, GrpcLogEntry.Logger.LOGGER_SERVER, callId);
super.onMessage(message);
}
@Override
public void onHalfClose() {
writer.logHalfClose(seq.getAndIncrement(), GrpcLogEntry.Logger.LOGGER_SERVER, callId);
super.onHalfClose();
}
@Override
public void onCancel() {
writer.logCancel(seq.getAndIncrement(), GrpcLogEntry.Logger.LOGGER_SERVER, callId);
super.onCancel();
}
};
}
};
}
use of com.google.protobuf.Duration in project curiostack by curioswitch.
the class ProtobufUtilTest method testDurationStringFormat.
@Test
void testDurationStringFormat() throws Exception {
// Generated output should contain 3, 6, or 9 fractional digits.
Duration duration = Duration.newBuilder().setSeconds(1).build();
assertThat(ProtobufUtil.formatDuration(duration)).isEqualTo("1s");
duration = Duration.newBuilder().setNanos(10000000).build();
assertThat(ProtobufUtil.formatDuration(duration)).isEqualTo("0.010s");
duration = Duration.newBuilder().setNanos(10000).build();
assertThat(ProtobufUtil.formatDuration(duration)).isEqualTo("0.000010s");
duration = Duration.newBuilder().setNanos(10).build();
assertThat(ProtobufUtil.formatDuration(duration)).isEqualTo("0.000000010s");
// Parsing accepts an fractional digits as long as they fit into nano
// precision.
duration = ProtobufUtil.parseDuration("0.1s");
assertThat(duration.getNanos()).isEqualTo(100000000);
duration = ProtobufUtil.parseDuration("0.0001s");
assertThat(duration.getNanos()).isEqualTo(100000);
duration = ProtobufUtil.parseDuration("0.0000001s");
assertThat(duration.getNanos()).isEqualTo(100);
// Duration must support range from -315,576,000,000s to +315576000000s
// which includes negative values.
duration = ProtobufUtil.parseDuration("315576000000.999999999s");
assertThat(duration.getSeconds()).isEqualTo(315576000000L);
assertThat(duration.getNanos()).isEqualTo(999999999);
duration = ProtobufUtil.parseDuration("-315576000000.999999999s");
assertThat(duration.getSeconds()).isEqualTo(-315576000000L);
assertThat(duration.getNanos()).isEqualTo(-999999999);
}
Aggregations