use of com.google.protobuf.MessageLite in project MSEC by Tencent.
the class RequestEncoder method encodeBody.
protected int encodeBody(RpcRequest rpcRequest, ChannelBufferOutputStream stream) throws IOException {
MessageLite message = (MessageLite) rpcRequest.getParameter();
stream.write(message.toByteArray());
return message.getSerializedSize();
}
use of com.google.protobuf.MessageLite in project grpc-java by grpc.
the class AbstractInteropTest method checkCensus.
/**
* Check information recorded by Census.
*/
private void checkCensus(MetricsRecord record, boolean isServer, Collection<? extends MessageLite> requests, Collection<? extends MessageLite> responses) {
int uncompressedRequestsSize = 0;
for (MessageLite request : requests) {
uncompressedRequestsSize += request.getSerializedSize();
}
int uncompressedResponsesSize = 0;
for (MessageLite response : responses) {
uncompressedResponsesSize += response.getSerializedSize();
}
if (isServer) {
assertEquals(requests.size(), record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_SERVER_REQUEST_COUNT));
assertEquals(responses.size(), record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_SERVER_RESPONSE_COUNT));
assertEquals(uncompressedRequestsSize, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_SERVER_UNCOMPRESSED_REQUEST_BYTES));
assertEquals(uncompressedResponsesSize, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_SERVER_UNCOMPRESSED_RESPONSE_BYTES));
assertNotNull(record.getMetric(DeprecatedCensusConstants.RPC_SERVER_SERVER_LATENCY));
// It's impossible to get the expected wire sizes because it may be compressed, so we just
// check if they are recorded.
assertNotNull(record.getMetric(DeprecatedCensusConstants.RPC_SERVER_REQUEST_BYTES));
assertNotNull(record.getMetric(DeprecatedCensusConstants.RPC_SERVER_RESPONSE_BYTES));
} else {
assertEquals(requests.size(), record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_REQUEST_COUNT));
assertEquals(responses.size(), record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_RESPONSE_COUNT));
assertEquals(uncompressedRequestsSize, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_UNCOMPRESSED_REQUEST_BYTES));
assertEquals(uncompressedResponsesSize, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_UNCOMPRESSED_RESPONSE_BYTES));
assertNotNull(record.getMetric(DeprecatedCensusConstants.RPC_CLIENT_ROUNDTRIP_LATENCY));
// It's impossible to get the expected wire sizes because it may be compressed, so we just
// check if they are recorded.
assertNotNull(record.getMetric(DeprecatedCensusConstants.RPC_CLIENT_REQUEST_BYTES));
assertNotNull(record.getMetric(DeprecatedCensusConstants.RPC_CLIENT_RESPONSE_BYTES));
}
}
use of com.google.protobuf.MessageLite in project grpc-java by grpc.
the class AbstractInteropTest method checkTracers.
/**
* Check information recorded by tracers.
*/
private void checkTracers(TestStreamTracer tracer, Collection<? extends MessageLite> sentMessages, Collection<? extends MessageLite> receivedMessages) {
long uncompressedSentSize = 0;
int seqNo = 0;
for (MessageLite msg : sentMessages) {
assertThat(tracer.nextOutboundEvent()).isEqualTo(String.format("outboundMessage(%d)", seqNo));
assertThat(tracer.nextOutboundEvent()).matches(String.format("outboundMessageSent\\(%d, -?[0-9]+, -?[0-9]+\\)", seqNo));
seqNo++;
uncompressedSentSize += msg.getSerializedSize();
}
assertNull(tracer.nextOutboundEvent());
long uncompressedReceivedSize = 0;
seqNo = 0;
for (MessageLite msg : receivedMessages) {
assertThat(tracer.nextInboundEvent()).isEqualTo(String.format("inboundMessage(%d)", seqNo));
assertThat(tracer.nextInboundEvent()).matches(String.format("inboundMessageRead\\(%d, -?[0-9]+, -?[0-9]+\\)", seqNo));
uncompressedReceivedSize += msg.getSerializedSize();
seqNo++;
}
assertNull(tracer.nextInboundEvent());
if (metricsExpected()) {
assertEquals(uncompressedSentSize, tracer.getOutboundUncompressedSize());
assertEquals(uncompressedReceivedSize, tracer.getInboundUncompressedSize());
}
}
use of com.google.protobuf.MessageLite in project grpc-java by grpc.
the class SafeMethodCachingInterceptor method interceptCall.
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(final MethodDescriptor<ReqT, RespT> method, final CallOptions callOptions, Channel next) {
// Currently only unary methods can be marked safe, but check anyways.
if (!method.isSafe() || method.getType() != MethodDescriptor.MethodType.UNARY) {
return next.newCall(method, callOptions);
}
final String fullMethodName = method.getFullMethodName();
return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
private Listener<RespT> interceptedListener;
private Key requestKey;
private boolean cacheResponse = true;
private volatile String cacheOptionsErrorMsg;
@Override
public void start(Listener<RespT> responseListener, Metadata headers) {
interceptedListener = new ForwardingClientCallListener.SimpleForwardingClientCallListener<RespT>(responseListener) {
private Deadline deadline;
private int maxAge = -1;
@Override
public void onHeaders(Metadata headers) {
Iterable<String> cacheControlHeaders = headers.getAll(CACHE_CONTROL_KEY);
if (cacheResponse && cacheControlHeaders != null) {
for (String cacheControlHeader : cacheControlHeaders) {
for (String directive : CACHE_CONTROL_SPLITTER.split(cacheControlHeader)) {
if (directive.equalsIgnoreCase("no-cache")) {
cacheResponse = false;
break;
} else if (directive.equalsIgnoreCase("no-store")) {
cacheResponse = false;
break;
} else if (directive.equalsIgnoreCase("no-transform")) {
cacheResponse = false;
break;
} else if (directive.toLowerCase(Locale.US).startsWith("max-age")) {
String[] parts = directive.split("=");
if (parts.length == 2) {
try {
maxAge = Integer.parseInt(parts[1]);
} catch (NumberFormatException e) {
Log.e(TAG, "max-age directive failed to parse", e);
continue;
}
}
}
}
}
}
if (cacheResponse) {
if (maxAge > -1) {
deadline = Deadline.after(maxAge, TimeUnit.SECONDS);
} else {
deadline = Deadline.after(defaultMaxAge, TimeUnit.SECONDS);
}
}
super.onHeaders(headers);
}
@Override
public void onMessage(RespT message) {
if (cacheResponse && !deadline.isExpired()) {
Value value = new Value((MessageLite) message, deadline);
internalCache.put(requestKey, value);
}
super.onMessage(message);
}
@Override
public void onClose(Status status, Metadata trailers) {
if (cacheOptionsErrorMsg != null) {
// UNAVAILABLE is the canonical gRPC mapping for HTTP response code 504 (as used
// by the built-in Android HTTP request cache).
super.onClose(Status.UNAVAILABLE.withDescription(cacheOptionsErrorMsg), new Metadata());
} else {
super.onClose(status, trailers);
}
}
};
delegate().start(interceptedListener, headers);
}
@Override
public void sendMessage(ReqT message) {
boolean noCache = callOptions.getOption(NO_CACHE_CALL_OPTION);
boolean onlyIfCached = callOptions.getOption(ONLY_IF_CACHED_CALL_OPTION);
if (noCache) {
if (onlyIfCached) {
cacheOptionsErrorMsg = "Unsatisfiable Request (no-cache and only-if-cached conflict)";
super.cancel(cacheOptionsErrorMsg, null);
return;
}
cacheResponse = false;
super.sendMessage(message);
return;
}
// Check the cache
requestKey = new Key(fullMethodName, (MessageLite) message);
Value cachedResponse = internalCache.get(requestKey);
if (cachedResponse != null) {
if (cachedResponse.maxAgeDeadline.isExpired()) {
internalCache.remove(requestKey);
} else {
// already cached
cacheResponse = false;
interceptedListener.onMessage((RespT) cachedResponse.response);
Metadata metadata = new Metadata();
interceptedListener.onClose(Status.OK, metadata);
return;
}
}
if (onlyIfCached) {
cacheOptionsErrorMsg = "Unsatisfiable Request (only-if-cached set, but value not in cache)";
super.cancel(cacheOptionsErrorMsg, null);
return;
}
super.sendMessage(message);
}
@Override
public void halfClose() {
if (cacheOptionsErrorMsg != null) {
// already canceled
return;
}
super.halfClose();
}
};
}
use of com.google.protobuf.MessageLite in project j2objc by google.
the class CompatibilityTest method testMessageLite.
public void testMessageLite() throws Exception {
// Mainly a compilation test for the Lite classes.
MessageLite.Builder builder = TypicalData.newBuilder();
MessageLite message = builder.build();
assertTrue(message instanceof MessageLite);
message.toByteString();
}
Aggregations