2525import feast .proto .serving .ServingServiceGrpc ;
2626import feast .proto .serving .ServingServiceGrpc .ServingServiceBlockingStub ;
2727import io .grpc .CallCredentials ;
28+ import io .grpc .Channel ;
2829import io .grpc .ManagedChannel ;
29- import io .grpc .ManagedChannelBuilder ;
30- import io .grpc .netty .shaded .io .grpc .netty .GrpcSslContexts ;
31- import io .grpc .netty .shaded .io .grpc .netty .NettyChannelBuilder ;
32- import io .opentracing .contrib .grpc .TracingClientInterceptor ;
33- import io .opentracing .util .GlobalTracer ;
34- import java .io .File ;
3530import java .util .HashSet ;
3631import java .util .List ;
3732import java .util .Optional ;
38- import java .util .concurrent .TimeUnit ;
3933import java .util .stream .Collectors ;
40- import javax .net .ssl .SSLException ;
4134import org .slf4j .Logger ;
4235import org .slf4j .LoggerFactory ;
4336
4437@ SuppressWarnings ("WeakerAccess" )
45- public class FeastClient implements AutoCloseable {
38+ public class FeastClient extends GrpcManager < ServingServiceBlockingStub > {
4639 Logger logger = LoggerFactory .getLogger (FeastClient .class );
4740
48- private static final int CHANNEL_SHUTDOWN_TIMEOUT_SEC = 5 ;
49-
50- private final ManagedChannel channel ;
51- private final ServingServiceBlockingStub stub ;
52-
5341 /**
5442 * Create a client to access Feast Serving.
5543 *
@@ -73,33 +61,7 @@ public static FeastClient create(String host, int port) {
7361 * @return {@link FeastClient}
7462 */
7563 public static FeastClient createSecure (String host , int port , SecurityConfig securityConfig ) {
76- // Configure client TLS
77- ManagedChannel channel = null ;
78- if (securityConfig .isTLSEnabled ()) {
79- if (securityConfig .getCertificatePath ().isPresent ()) {
80- String certificatePath = securityConfig .getCertificatePath ().get ();
81- // Use custom certificate for TLS
82- File certificateFile = new File (certificatePath );
83- try {
84- channel =
85- NettyChannelBuilder .forAddress (host , port )
86- .useTransportSecurity ()
87- .sslContext (GrpcSslContexts .forClient ().trustManager (certificateFile ).build ())
88- .build ();
89- } catch (SSLException e ) {
90- throw new IllegalArgumentException (
91- String .format ("Invalid Certificate provided at path: %s" , certificatePath ), e );
92- }
93- } else {
94- // Use system certificates for TLS
95- channel = ManagedChannelBuilder .forAddress (host , port ).useTransportSecurity ().build ();
96- }
97- } else {
98- // Disable TLS
99- channel = ManagedChannelBuilder .forAddress (host , port ).usePlaintext ().build ();
100- }
101-
102- return new FeastClient (channel , securityConfig .getCredentials ());
64+ return new FeastClient (host , port , securityConfig );
10365 }
10466
10567 /**
@@ -188,24 +150,16 @@ public List<Row> getOnlineFeatures(List<String> featureRefs, List<Row> rows, Str
188150 .collect (Collectors .toList ());
189151 }
190152
191- protected FeastClient (ManagedChannel channel , Optional <CallCredentials > credentials ) {
192- this .channel = channel ;
193- TracingClientInterceptor tracingInterceptor =
194- TracingClientInterceptor .newBuilder ().withTracer (GlobalTracer .get ()).build ();
195-
196- ServingServiceBlockingStub servingStub =
197- ServingServiceGrpc .newBlockingStub (tracingInterceptor .intercept (channel ));
198-
199- if (credentials .isPresent ()) {
200- servingStub = servingStub .withCallCredentials (credentials .get ());
201- }
153+ @ Override
154+ protected ServingServiceBlockingStub getStub (Channel channel ) {
155+ return ServingServiceGrpc .newBlockingStub (channel );
156+ }
202157
203- this .stub = servingStub ;
158+ protected FeastClient (ManagedChannel channel , Optional <CallCredentials > credentials ) {
159+ super (channel , credentials );
204160 }
205161
206- public void close () throws Exception {
207- if (channel != null ) {
208- channel .shutdown ().awaitTermination (CHANNEL_SHUTDOWN_TIMEOUT_SEC , TimeUnit .SECONDS );
209- }
162+ protected FeastClient (String host , int port , SecurityConfig securityConfig ) {
163+ super (host , port , securityConfig );
210164 }
211165}
0 commit comments