grpc-ecosystem / grpc-ecosystem/grpc-spring
The interceptor is initialized twice
- Dominant language
- Java
- Stars
- 3.7k
- Forks
- 858
- PR merge metrics
- No merged PRs in 30d
Description
### The interceptor is initialized twice:
#### Branch : master
#### Source: local-grpc-client/local-grpc-server
Let's take a look at the picture below:

>This is what happened when I used the case
#### My use case:
```java
@GrpcClient(value = "local-grpc-client", interceptors = LogGrpcInterceptor.class )
private SimpleBlockingStub simpleStub;
```
#### The actual effect is as follows:

#### The code show as below:
```java
public class LogGrpcInterceptor implements ClientInterceptor {
private static final Logger log = LoggerFactory.getLogger(LogGrpcInterceptor.class);
@Override
public ClientCall interceptCall(MethodDescriptor method,
CallOptions callOptions, Channel next) {
log.info(method.getFullMethodName()+"window");
return next.newCall(method, callOptions);
}
}
```
#### Suggest:
```java
@Override
public Channel createChannel(final String name, final List customInterceptors,
final boolean sortInterceptors) {
final Channel channel;
synchronized (this) {
if (this.shutdown) {
throw new IllegalStateException("GrpcChannelFactory is already closed!");
}
channel = this.channels.computeIfAbsent(name, this::newManagedChannel);
}
final List interceptors =
Lists.newArrayList(this.globalClientInterceptorRegistry.getClientInterceptors());
interceptors.addAll(customInterceptors);//In fact, there is no need to add once, if you must add, you can use HashSet to remove duplicates
if (sortInterceptors) {
this.globalClientInterceptorRegistry.sortInterceptors(interceptors);
}
return ClientInterceptors.interceptForward(channel, interceptors);
}
```
Contributor guide
Research direction
Start with GrpcChannelFactory#createChannel and the local-grpc-client/local-grpc-server use case described in the issue. Reproduce the @GrpcClient configuration with LogGrpcInterceptor and trace how global and custom interceptors are collected; done means the interceptor is invoked only once for a call.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring-boot
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100