Skip to content

Commit 2dbd3ef

Browse files
committed
closes #276
1 parent c85845e commit 2dbd3ef

File tree

5 files changed

+47
-18
lines changed

5 files changed

+47
-18
lines changed

README.adoc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,14 +269,13 @@ public class B implements ServerInterceptor{
269269
The starter uses built-in interceptors to implement error handling, Spring `Security`, `Validation` and `Metrics` integration.
270270
Their order can also be controlled by below properties :
271271

272+
* `grpc.recovery.interceptor-order` (error handling interceptor order, defaults to `Ordered.HIGHEST_PRECEDENCE`)
272273
* `grpc.security.auth.interceptor-order` ( defaults to `Ordered.HIGHEST_PRECEDENCE+1`)
273274
* `grpc.validation.interceptor-order` ( defaults to `Ordered.HIGHEST_PRECEDENCE+10`)
274275
* `grpc.metrics.interceptor-order` ( defaults to `Ordered.HIGHEST_PRECEDENCE+20`)
275276

276277
This gives you the ability to set up the desired order of built-in and your custom interceptors.
277278

278-
Error handling interceptor has the `Ordered.HIGHEST_PRECEDENCE`.
279-
280279
*Keep on reading !!! There is more*
281280

282281
The way grpc interceptor works is that it intercepts the call and returns the server call listener, which in turn can intercept the request message as well, before forwarding it to the actual service call handler :

grpc-spring-boot-starter-demo/src/test/java/org/lognet/springboot/grpc/CustomInterceptorsOrderTest.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.junit.Test;
1717
import org.junit.runner.RunWith;
1818
import org.lognet.springboot.grpc.demo.DemoApp;
19+
import org.lognet.springboot.grpc.recovery.GRpcExceptionHandlerInterceptor;
1920
import org.lognet.springboot.grpc.security.AuthClientInterceptor;
2021
import org.lognet.springboot.grpc.security.AuthHeader;
2122
import org.lognet.springboot.grpc.security.GrpcSecurity;
@@ -28,6 +29,7 @@
2829
import org.springframework.context.annotation.Bean;
2930
import org.springframework.context.annotation.Import;
3031
import org.springframework.context.annotation.Lazy;
32+
import org.springframework.core.Ordered;
3133
import org.springframework.core.annotation.Order;
3234
import org.springframework.security.authentication.AuthenticationProvider;
3335
import org.springframework.security.core.Authentication;
@@ -39,7 +41,9 @@
3941

4042
import java.util.ArrayList;
4143
import java.util.Arrays;
44+
import java.util.Collections;
4245
import java.util.List;
46+
import java.util.function.Function;
4347
import java.util.stream.Collectors;
4448

4549
import static org.hamcrest.MatcherAssert.assertThat;
@@ -51,9 +55,10 @@
5155
@SpringBootTest(classes = DemoApp.class,
5256
properties = {
5357
"grpc.security.auth.fail-fast=false", // give validator a chance to run before failing auth
54-
"grpc.security.auth.interceptor-order=3", //third
55-
"grpc.metrics.interceptor-order=2", //second
56-
"grpc.validation.interceptor-order=1" //first
58+
"grpc.security.auth.interceptor-order=3",
59+
"grpc.metrics.interceptor-order=2",
60+
"grpc.validation.interceptor-order=1",
61+
"grpc.recovery.interceptor-order=-1"
5762
}
5863
,webEnvironment = SpringBootTest.WebEnvironment.NONE
5964
)
@@ -76,10 +81,12 @@ enum Interceptor {
7681
@Before
7782
public void setUp() throws Exception {
7883
calledInterceptors.clear();
79-
final List<Class<? extends ServerInterceptor>> orderedInterceptorsClasses = Arrays.asList( //according the order define by properties
84+
final List<Class<? extends ServerInterceptor>> orderedInterceptorsClasses = Arrays.asList( //according to the order defined by properties
85+
GRpcExceptionHandlerInterceptor.class,
8086
TestCfg.UserDefinedInterceptor.class,
8187
ValidatingInterceptor.class,
8288
SecurityInterceptor.class
89+
8390
);
8491
final List<Class<?>> orderedInterceptors = interceptors
8592
.stream()
@@ -89,6 +96,20 @@ public void setUp() throws Exception {
8996

9097
assertThat(orderedInterceptors, Matchers.is(orderedInterceptorsClasses));
9198

99+
Function<Class<? extends Ordered>,Integer> getOrder = ordered->
100+
interceptors
101+
.stream()
102+
.filter(ordered::isInstance)
103+
.map(ordered::cast)
104+
.map(Ordered::getOrder)
105+
.findFirst()
106+
.orElse(null);
107+
108+
assertThat(getOrder.apply(GRpcExceptionHandlerInterceptor.class),Matchers.is(-1));
109+
assertThat(getOrder.apply(ValidatingInterceptor.class),Matchers.is(1));
110+
assertThat(getOrder.apply(SecurityInterceptor.class),Matchers.is(3));
111+
112+
92113
}
93114

94115
@Test

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/autoconfigure/GRpcAutoConfiguration.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.lognet.springboot.grpc.recovery.GRpcServiceAdvice;
1515
import org.springframework.beans.factory.BeanCreationException;
1616
import org.springframework.beans.factory.annotation.Qualifier;
17+
import org.springframework.beans.factory.annotation.Value;
1718
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
1819
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
1920
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
@@ -85,8 +86,9 @@ public FailureHandlingSupport failureHandlingSupport(GRpcExceptionHandlerMethodR
8586
@Bean
8687
@GRpcGlobalInterceptor
8788
public GRpcExceptionHandlerInterceptor exceptionHandlerInterceptor(FailureHandlingSupport failureHandlingSupport,
88-
GRpcExceptionHandlerMethodResolver methodResolver) {
89-
return new GRpcExceptionHandlerInterceptor(methodResolver,failureHandlingSupport);
89+
GRpcExceptionHandlerMethodResolver methodResolver,
90+
GRpcServerProperties serverProperties) {
91+
return new GRpcExceptionHandlerInterceptor(methodResolver,failureHandlingSupport,serverProperties);
9092
}
9193

9294
@Bean

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/autoconfigure/GRpcServerProperties.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public class GRpcServerProperties {
3434

3535
private SecurityProperties security;
3636

37+
private RecoveryProperties recovery;
38+
3739
private NettyServerProperties nettyServer;
3840

3941
private ConsulProperties consul = new ConsulProperties();
@@ -81,6 +83,11 @@ public Integer getRunningPort() {
8183

8284
}
8385

86+
@Getter
87+
@Setter
88+
public static class RecoveryProperties{
89+
private Integer interceptorOrder;
90+
}
8491
@Getter
8592
@Setter
8693
public static class SecurityProperties {

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/recovery/GRpcExceptionHandlerInterceptor.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
package org.lognet.springboot.grpc.recovery;
22

3-
import io.grpc.Context;
4-
import io.grpc.ForwardingServerCall;
5-
import io.grpc.Metadata;
6-
import io.grpc.ServerCall;
7-
import io.grpc.ServerCallHandler;
8-
import io.grpc.ServerInterceptor;
9-
import io.grpc.Status;
3+
import io.grpc.*;
104
import org.lognet.springboot.grpc.FailureHandlingSupport;
115
import org.lognet.springboot.grpc.MessageBlockingServerCallListener;
6+
import org.lognet.springboot.grpc.autoconfigure.GRpcServerProperties;
127
import org.springframework.core.Ordered;
138

9+
import java.util.Optional;
1410
import java.util.concurrent.atomic.AtomicBoolean;
1511

1612
public class GRpcExceptionHandlerInterceptor implements ServerInterceptor, Ordered {
17-
private static final Context.Key<AtomicBoolean> CALL_IS_CLOSED = Context.key("CALL_IS_CLOSED");
13+
1814
private final GRpcExceptionHandlerMethodResolver methodResolver;
1915
private final FailureHandlingSupport failureHandlingSupport;
2016

17+
private Integer order;
2118

22-
public GRpcExceptionHandlerInterceptor(GRpcExceptionHandlerMethodResolver methodResolver, FailureHandlingSupport failureHandlingSupport) {
19+
public GRpcExceptionHandlerInterceptor(GRpcExceptionHandlerMethodResolver methodResolver, FailureHandlingSupport failureHandlingSupport, GRpcServerProperties serverProperties) {
2320
this.methodResolver = methodResolver;
2421
this.failureHandlingSupport = failureHandlingSupport;
22+
this.order = Optional.ofNullable(serverProperties.getRecovery())
23+
.map(GRpcServerProperties.RecoveryProperties::getInterceptorOrder)
24+
.orElse(Ordered.HIGHEST_PRECEDENCE);
2525
}
2626

2727

@@ -98,7 +98,7 @@ public void onHalfClose() {
9898

9999
@Override
100100
public int getOrder() {
101-
return Ordered.HIGHEST_PRECEDENCE;
101+
return order;
102102
}
103103

104104

0 commit comments

Comments
 (0)