grpc / grpc-java

The Java gRPC implementation. HTTP/2 based RPC
https://grpc.io/docs/languages/java/
Apache License 2.0
11.48k stars 3.85k forks source link

Expose Metadata.size #11686

Open He-Pin opened 1 week ago

He-Pin commented 1 week ago

Motivication:

I want to get the metadata's size, but currently I can only get it with metadata#keys#size, access it directly will reduce some overhead.

NOTE: It was reverted in PR: https://github.com/grpc/grpc-java/pull/3008

My currently usage:

        //https://github.com/grpc/grpc-java/pull/3008
        if (!headersSent && !extraHeaders.keys().isEmpty()) {
            //trailers.merge(extraHeaders);
            sendHeaders(new Metadata());
        }
kannanjgithub commented 3 days ago

Is your use case not doable using client interceptors or server interceptors for adding custom header (examples)?

He-Pin commented 3 days ago

Thanks, the code is already inside a intercepter, I want to use extraHeaders.isNotEmpty() instead of extraHeaders.keys().isEmpty()

ejona86 commented 2 days ago

It isn't clear what you're trying to achieve. If there are extra headers you want to ignore them? How is the isEmpty() check significant? What if you just didn't call isEmpty()? (I do assume you are doing it for a reason, I just can't tell what it is.)