Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions core/src/main/java/io/grpc/Metadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,16 @@ public <T> Iterable<T> removeAll(Key<T> key) {
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.
*
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/io/grpc/internal/AbstractServerStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ public final void close(Status status, Metadata trailers) {
}

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());
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/io/grpc/internal/ClientCallImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ ClientCallImpl<ReqT, RespT> setCompressorRegistry(CompressorRegistry compressorR
@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);
Expand Down Expand Up @@ -251,7 +251,7 @@ public void runInContext() {
*/
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;
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/io/grpc/internal/Http2ClientStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ private static Charset extractCharset(Metadata headers) {
* 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);
}
}
4 changes: 2 additions & 2 deletions core/src/main/java/io/grpc/internal/ServerCallImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void sendHeaders(Metadata headers) {
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 {
Expand All @@ -125,7 +125,7 @@ public void sendHeaders(Metadata headers) {

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);
Expand Down
11 changes: 11 additions & 0 deletions core/src/test/java/io/grpc/MetadataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ public void testMutations() {
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);
Expand Down
2 changes: 1 addition & 1 deletion netty/src/main/java/io/grpc/netty/NettyClientStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void start(ClientStreamListener listener) {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public void setAuthority(String authority) {
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;
Expand Down