Skip to content

Commit

Permalink
Fixed gRPC tag condition matching issue
Browse files Browse the repository at this point in the history
  • Loading branch information
chenzhiguo committed Jan 6, 2025
1 parent b27214a commit 679c6f7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@

import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

/**
* An interface representing a gRPC request.
Expand Down Expand Up @@ -81,6 +84,21 @@ public void setHeader(String key, String value) {
request.setHeader(key, value);
}

@Override
public String getHeader(String key) {
return request.getHeaders().get(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER));
}

@Override
public List<String> getHeaders(String key) {
Iterable<String> iterable = request.getHeaders().getAll(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER));
if (iterable == null) {
return null;
}
return StreamSupport.stream(iterable.spliterator(), false)
.collect(Collectors.toList());
}

@SuppressWarnings("unchecked")
@Override
public <E extends Endpoint> E getEndpoint() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,21 @@ public void onEnter(ExecutableContext ctx) {
}

private void attachTag(Metadata metadata) {
RequestContext.cargos(tag -> metadata.put(Metadata.Key.of(tag.getKey(), Metadata.ASCII_STRING_MARSHALLER), tag.getValue()));
RequestContext.cargos(tag -> {
// If the tag key and value already exists in the metadata, do not add it again.
if (metadata.containsKey(Metadata.Key.of(tag.getKey(), Metadata.ASCII_STRING_MARSHALLER))) {
Iterable<String> iterable = metadata.getAll(Metadata.Key.of(tag.getKey(), Metadata.ASCII_STRING_MARSHALLER));
if (iterable != null) {
for (String value : iterable) {
if (value.equals(tag.getValue())) {
return;
}
}
}
}
metadata.put(Metadata.Key.of(tag.getKey(), Metadata.ASCII_STRING_MARSHALLER), tag.getValue());
}
);
}

}

0 comments on commit 679c6f7

Please sign in to comment.