Search in sources :

Example 56 with Duration

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);
                }
            };
        }
    };
}
Also used : Status(io.grpc.Status) SimpleForwardingServerCallListener(io.grpc.ForwardingServerCallListener.SimpleForwardingServerCallListener) SimpleForwardingClientCallListener(io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener) Channel(io.grpc.Channel) Deadline(io.grpc.Deadline) Metadata(io.grpc.Metadata) InternalMetadata(io.grpc.InternalMetadata) Duration(com.google.protobuf.Duration) CallOptions(io.grpc.CallOptions) ByteString(com.google.protobuf.ByteString) MethodDescriptor(io.grpc.MethodDescriptor) AtomicLong(java.util.concurrent.atomic.AtomicLong) SimpleForwardingClientCall(io.grpc.ForwardingClientCall.SimpleForwardingClientCall) ClientCall(io.grpc.ClientCall) ClientInterceptor(io.grpc.ClientInterceptor) SimpleForwardingClientCallListener(io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) SimpleForwardingClientCall(io.grpc.ForwardingClientCall.SimpleForwardingClientCall)

Example 57 with Duration

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();
                }
            };
        }
    };
}
Also used : Status(io.grpc.Status) ServerCallHandler(io.grpc.ServerCallHandler) Deadline(io.grpc.Deadline) Metadata(io.grpc.Metadata) InternalMetadata(io.grpc.InternalMetadata) Duration(com.google.protobuf.Duration) ByteString(com.google.protobuf.ByteString) SimpleForwardingServerCall(io.grpc.ForwardingServerCall.SimpleForwardingServerCall) SimpleForwardingServerCallListener(io.grpc.ForwardingServerCallListener.SimpleForwardingServerCallListener) AtomicLong(java.util.concurrent.atomic.AtomicLong) SimpleForwardingServerCall(io.grpc.ForwardingServerCall.SimpleForwardingServerCall) ServerCall(io.grpc.ServerCall) ServerInterceptor(io.grpc.ServerInterceptor) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 58 with Duration

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);
}
Also used : Duration(com.google.protobuf.Duration) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Duration (com.google.protobuf.Duration)58 Test (org.junit.Test)37 Timestamp (com.google.protobuf.Timestamp)25 AbstractZonedTimeTest (io.spine.time.AbstractZonedTimeTest)8 LocalTime (io.spine.time.LocalTime)8 ByteString (com.google.protobuf.ByteString)7 Metadata (io.grpc.Metadata)7 InetSocketAddress (java.net.InetSocketAddress)7 SocketAddress (java.net.SocketAddress)6 Command (io.spine.core.Command)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 CallOptions (io.grpc.CallOptions)4 Channel (io.grpc.Channel)4 ClientCall (io.grpc.ClientCall)4 MethodDescriptor (io.grpc.MethodDescriptor)4 Status (io.grpc.Status)4 Message (com.google.protobuf.Message)3 ServerCall (io.grpc.ServerCall)3 DomainSocketAddress (io.netty.channel.unix.DomainSocketAddress)3 Schedule (io.spine.core.CommandContext.Schedule)3