grpc-ecosystem / grpc-ecosystem/grpc-spring

The interceptor is initialized twice

Open
#505 5 comments 0 reactions 0 assignees View on GitHub
bug feedback required
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:

![image](https://user-images.githubusercontent.com/24503935/110298266-60c0d300-802f-11eb-8c3a-b2f966f13c24.png)

>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:

![image](https://user-images.githubusercontent.com/24503935/110299058-3b809480-8030-11eb-9dec-46f8ddf04527.png)

#### 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

Open the contributing 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.