1919import io .grpc .ClientInterceptor ;
2020import io .grpc .ForwardingClientCall .SimpleForwardingClientCall ;
2121import io .grpc .ForwardingClientCallListener ;
22+ import io .grpc .ManagedChannel ;
2223import io .grpc .Metadata ;
2324import io .grpc .MethodDescriptor ;
2425import io .grpc .Status ;
26+ import io .grpc .xds .internal .grpcservice .GrpcServiceChannelCreator ;
27+ import io .grpc .xds .internal .grpcservice .GrpcServiceChannelCreatorImpl ;
2528import java .io .ByteArrayInputStream ;
2629import java .io .IOException ;
2730import java .io .InputStream ;
@@ -33,8 +36,16 @@ public class ExternalProcessorFilter implements Filter {
3336 static final String TYPE_URL = "type.googleapis.com/envoy.extensions.filters.http.ext_proc.v3.ExternalProcessor" ;
3437
3538 final String filterInstanceName ;
39+ // TODO: Make final after the need to replace with a mock from unit tests is removed.
40+ GrpcServiceChannelCreator grpcServiceChannelCreator ;
41+ ManagedChannel grpcServiceChannel ;
42+ ExternalProcessorGrpc .ExternalProcessorStub externalProcessorStub ;
43+ private final Object lock = new Object ();
44+ private GrpcService lastGrpcServiceConfig ;
45+
3646 public ExternalProcessorFilter (String name ) {
3747 filterInstanceName = checkNotNull (name , "name" );
48+ grpcServiceChannelCreator = new GrpcServiceChannelCreatorImpl ();
3849 }
3950
4051 static final class Provider implements Filter .Provider {
@@ -77,7 +88,26 @@ public ConfigOrError<? extends FilterConfig> parseFilterConfigOverride(Message r
7788 @ Override
7889 public ClientInterceptor buildClientInterceptor (FilterConfig filterConfig ,
7990 @ Nullable FilterConfig overrideConfig , ScheduledExecutorService scheduler ) {
80- return new ExternalProcessorInterceptor ((ExternalProcessorFilterConfig ) filterConfig , overrideConfig , scheduler );
91+ return new ExternalProcessorInterceptor (this , (ExternalProcessorFilterConfig ) filterConfig , overrideConfig , scheduler );
92+ }
93+
94+ ExternalProcessorGrpc .ExternalProcessorStub getExternalProcessorStub (ExternalProcessor config ) {
95+ GrpcService newServiceConfig = config .getGrpcService ();
96+ synchronized (lock ) {
97+ // TODO: gRFC only mentions we should recreate channel if target or channel creds changed
98+ // but other fields in grpc service config also do seem relevant to warrant channel
99+ // recreation.
100+ if (grpcServiceChannel == null || !newServiceConfig .equals (lastGrpcServiceConfig )) {
101+ if (grpcServiceChannel != null ) {
102+ // Shutdown the old channel if the config has changed
103+ grpcServiceChannel .shutdown ();
104+ }
105+ grpcServiceChannel = grpcServiceChannelCreator .create (newServiceConfig );
106+ externalProcessorStub = ExternalProcessorGrpc .newStub (grpcServiceChannel );
107+ lastGrpcServiceConfig = newServiceConfig ;
108+ }
109+ return externalProcessorStub ;
110+ }
81111 }
82112
83113 static final class ExternalProcessorFilterConfig implements FilterConfig {
@@ -95,12 +125,15 @@ public String typeUrl() {
95125 }
96126
97127 static final class ExternalProcessorInterceptor implements ClientInterceptor {
128+ private final ExternalProcessorFilter filter ;
98129 private final ExternalProcessorFilterConfig filterConfig ;
99130 private final FilterConfig overrideConfig ;
100131 private final ScheduledExecutorService scheduler ;
101132
102- ExternalProcessorInterceptor (ExternalProcessorFilterConfig filterConfig ,
133+ ExternalProcessorInterceptor (ExternalProcessorFilter filter ,
134+ ExternalProcessorFilterConfig filterConfig ,
103135 @ Nullable FilterConfig overrideConfig , ScheduledExecutorService scheduler ) {
136+ this .filter = filter ;
104137 this .filterConfig = filterConfig ;
105138 this .overrideConfig = overrideConfig ;
106139 this .scheduler = scheduler ;
@@ -111,8 +144,7 @@ public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
111144 MethodDescriptor <ReqT , RespT > method ,
112145 CallOptions callOptions ,
113146 Channel next ) {
114-
115- ExternalProcessorGrpc .ExternalProcessorStub stub = getExternalProcessorStub (filterConfig .externalProcessor .getGrpcService ());
147+ ExternalProcessorGrpc .ExternalProcessorStub stub = filter .getExternalProcessorStub (filterConfig .externalProcessor );
116148 ExternalProcessor config = filterConfig .externalProcessor ;
117149 // Wrap the outgoing call to intercept client events
118150 return new ExtProcClientCall <>(next .newCall (method , callOptions ), stub , method , config );
@@ -539,24 +571,5 @@ void unblockAfterStreamComplete() {
539571 }
540572 }
541573 }
542-
543- @ VisibleForTesting
544- ExternalProcessorGrpc .ExternalProcessorStub getExternalProcessorStub (GrpcService service ) {
545- // TODO: Implement actual stub creation based on the GrpcService configuration.
546- // This will likely involve creating a ManagedChannel and then a stub from it.
547- // For now, returning null as a placeholder.
548- //
549- // This method needs to create a ManagedChannel based on the GrpcService configuration.
550- // The GrpcService contains information like target URI, timeout, and optionally
551- // a Google gRPC service config.
552- // For a full implementation, you would typically use a ManagedChannelBuilder
553- // to construct the channel and then create a stub from it.
554- // Example (simplified, actual implementation would need more details from GrpcService):
555- // ManagedChannel channel = ManagedChannelBuilder.forTarget(service.getEnvoyGrpc().getClusterName())
556- // .usePlaintext() // Or use TLS based on configuration
557- // .build();
558- // return ExternalProcessorGrpc.newStub(channel);
559- return null ;
560- }
561574 }
562575}
0 commit comments