io.quarkus.grpc.runtime.stork.StorkMeasuringGrpcInterceptor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of quarkus-grpc Show documentation
Show all versions of quarkus-grpc Show documentation
Serve and consume gRPC services
package io.quarkus.grpc.runtime.stork;
import java.util.concurrent.atomic.AtomicReference;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.spi.Prioritized;
import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ClientCall;
import io.grpc.ClientInterceptor;
import io.grpc.Context;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import io.smallrye.stork.api.ServiceInstance;
@ApplicationScoped
public class StorkMeasuringGrpcInterceptor implements ClientInterceptor, Prioritized {
@Override
public ClientCall interceptCall(MethodDescriptor method, CallOptions callOptions,
Channel next) {
return new StorkMeasuringCall<>(next.newCall(method, callOptions), method.getType());
}
@Override
public int getPriority() {
return Integer.MAX_VALUE - 100;
}
private static class StorkMeasuringCall extends AbstractStorkMeasuringCall {
ServiceInstance serviceInstance;
protected StorkMeasuringCall(ClientCall delegate, MethodDescriptor.MethodType type) {
super(delegate, type == MethodDescriptor.MethodType.UNARY);
}
@Override
protected ServiceInstance serviceInstance() {
return serviceInstance;
}
@Override
public void start(final ClientCall.Listener responseListener, final Metadata metadata) {
Context context = Context.current().withValues(
STORK_SERVICE_INSTANCE, new AtomicReference<>(),
STORK_MEASURE_TIME, recordTime);
Context oldContext = context.attach();
try {
super.start(new StorkMeasuringCallListener<>(responseListener, this), metadata);
serviceInstance = STORK_SERVICE_INSTANCE.get().get();
} finally {
context.detach(oldContext);
}
}
}
}