mirror of https://github.com/grpc/grpc-java.git
core: add Metadata.discardAll()
Metadata.removeAll creates an iterator for looking through removed values even if the call doens't use it. This change adds a similar method which doesn't create garbage. This change makes it easier in the future to alter the internals of Metadata where it may be expensive to return removed values.
This commit is contained in:
parent
5379de726d
commit
f78644d762
|
|
@ -271,6 +271,16 @@ public final class Metadata {
|
|||
return new ValueIterable<T>(key, values);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all values for the given key without returning them. This is a minor performance
|
||||
* optimization if you do not need the previous values.
|
||||
*/
|
||||
@ExperimentalApi
|
||||
public <T> void discardAll(Key<T> key) {
|
||||
List<MetadataEntry> removed = store.remove(key.name());
|
||||
storeCount -= removed != null ? removed.size() : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize all the metadata entries.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -145,8 +145,8 @@ public abstract class AbstractServerStream extends AbstractStream2
|
|||
}
|
||||
|
||||
private void addStatusToTrailers(Metadata trailers, Status status) {
|
||||
trailers.removeAll(Status.CODE_KEY);
|
||||
trailers.removeAll(Status.MESSAGE_KEY);
|
||||
trailers.discardAll(Status.CODE_KEY);
|
||||
trailers.discardAll(Status.MESSAGE_KEY);
|
||||
trailers.put(Status.CODE_KEY, status);
|
||||
if (status.getDescription() != null) {
|
||||
trailers.put(Status.MESSAGE_KEY, status.getDescription());
|
||||
|
|
|
|||
|
|
@ -140,12 +140,12 @@ final class ClientCallImpl<ReqT, RespT> extends ClientCall<ReqT, RespT>
|
|||
@VisibleForTesting
|
||||
static void prepareHeaders(Metadata headers, DecompressorRegistry decompressorRegistry,
|
||||
Compressor compressor) {
|
||||
headers.removeAll(MESSAGE_ENCODING_KEY);
|
||||
headers.discardAll(MESSAGE_ENCODING_KEY);
|
||||
if (compressor != Codec.Identity.NONE) {
|
||||
headers.put(MESSAGE_ENCODING_KEY, compressor.getMessageEncoding());
|
||||
}
|
||||
|
||||
headers.removeAll(MESSAGE_ACCEPT_ENCODING_KEY);
|
||||
headers.discardAll(MESSAGE_ACCEPT_ENCODING_KEY);
|
||||
String advertisedEncodings = decompressorRegistry.getRawAdvertisedMessageEncodings();
|
||||
if (!advertisedEncodings.isEmpty()) {
|
||||
headers.put(MESSAGE_ACCEPT_ENCODING_KEY, advertisedEncodings);
|
||||
|
|
@ -251,7 +251,7 @@ final class ClientCallImpl<ReqT, RespT> extends ClientCall<ReqT, RespT>
|
|||
*/
|
||||
private static void updateTimeoutHeaders(@Nullable Deadline effectiveDeadline,
|
||||
@Nullable Deadline callDeadline, @Nullable Deadline outerCallDeadline, Metadata headers) {
|
||||
headers.removeAll(TIMEOUT_KEY);
|
||||
headers.discardAll(TIMEOUT_KEY);
|
||||
|
||||
if (effectiveDeadline == null) {
|
||||
return;
|
||||
|
|
|
|||
|
|
@ -238,8 +238,8 @@ public abstract class Http2ClientStream extends AbstractClientStream<Integer> {
|
|||
* the application layer.
|
||||
*/
|
||||
private static void stripTransportDetails(Metadata metadata) {
|
||||
metadata.removeAll(HTTP2_STATUS);
|
||||
metadata.removeAll(Status.CODE_KEY);
|
||||
metadata.removeAll(Status.MESSAGE_KEY);
|
||||
metadata.discardAll(HTTP2_STATUS);
|
||||
metadata.discardAll(Status.CODE_KEY);
|
||||
metadata.discardAll(Status.MESSAGE_KEY);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -104,7 +104,7 @@ final class ServerCallImpl<ReqT, RespT> extends ServerCall<ReqT, RespT> {
|
|||
checkState(!sendHeadersCalled, "sendHeaders has already been called");
|
||||
checkState(!closeCalled, "call is closed");
|
||||
|
||||
headers.removeAll(MESSAGE_ENCODING_KEY);
|
||||
headers.discardAll(MESSAGE_ENCODING_KEY);
|
||||
if (compressor == null) {
|
||||
compressor = Codec.Identity.NONE;
|
||||
} else {
|
||||
|
|
@ -125,7 +125,7 @@ final class ServerCallImpl<ReqT, RespT> extends ServerCall<ReqT, RespT> {
|
|||
|
||||
stream.setCompressor(compressor);
|
||||
|
||||
headers.removeAll(MESSAGE_ACCEPT_ENCODING_KEY);
|
||||
headers.discardAll(MESSAGE_ACCEPT_ENCODING_KEY);
|
||||
String advertisedEncodings = decompressorRegistry.getRawAdvertisedMessageEncodings();
|
||||
if (!advertisedEncodings.isEmpty()) {
|
||||
headers.put(MESSAGE_ACCEPT_ENCODING_KEY, advertisedEncodings);
|
||||
|
|
|
|||
|
|
@ -114,6 +114,17 @@ public class MetadataTest {
|
|||
assertEquals(null, metadata.get(KEY));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void discardAll() {
|
||||
Fish lance = new Fish(LANCE);
|
||||
Metadata metadata = new Metadata();
|
||||
|
||||
metadata.put(KEY, lance);
|
||||
metadata.discardAll(KEY);
|
||||
assertEquals(null, metadata.getAll(KEY));
|
||||
assertEquals(null, metadata.get(KEY));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAllNoRemove() {
|
||||
Fish lance = new Fish(LANCE);
|
||||
|
|
|
|||
|
|
@ -105,7 +105,7 @@ abstract class NettyClientStream extends Http2ClientStream implements StreamIdHo
|
|||
defaultPath = new AsciiString("/" + method.getFullMethodName());
|
||||
methodDescriptorAccessor.setRawMethodName(method, defaultPath);
|
||||
}
|
||||
headers.removeAll(GrpcUtil.USER_AGENT_KEY);
|
||||
headers.discardAll(GrpcUtil.USER_AGENT_KEY);
|
||||
Http2Headers http2Headers
|
||||
= Utils.convertClientHeaders(headers, scheme, defaultPath, authority, userAgent);
|
||||
headers = null;
|
||||
|
|
|
|||
|
|
@ -139,7 +139,7 @@ class OkHttpClientStream extends Http2ClientStream {
|
|||
public void start(ClientStreamListener listener) {
|
||||
super.start(listener);
|
||||
String defaultPath = "/" + method.getFullMethodName();
|
||||
headers.removeAll(GrpcUtil.USER_AGENT_KEY);
|
||||
headers.discardAll(GrpcUtil.USER_AGENT_KEY);
|
||||
List<Header> requestHeaders =
|
||||
Headers.createRequestHeaders(headers, defaultPath, authority, userAgent);
|
||||
headers = null;
|
||||
|
|
|
|||
Loading…
Reference in New Issue