Last active
August 21, 2020 15:23
-
-
Save nsivabalan/1ccd6d0dcf3998edfacef40f2c19f87e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Public interface for Realtime clients, ramen and other clients to send and receive network | |
* requests. Clients need to instantiate {@link UberNetworkingClient} once using singleton method | |
* {@link #getInstance(List)} and then for new grpc calls, can call the following. | |
* uberNetworkingClient.newCall(MethodInfo, UberInternalCallOptions) This will return an instance of | |
* {@link UberClientCall} and any further interaction to the request will be done in via {@link | |
* UberClientCall}. | |
* | |
* <p>For every new call, UberNetworkingClient will instantiate a new chain of {@link | |
* UberInterceptor}s and attachs them to the {@link UbergRPCAdaptor} at the end. {@link | |
* UbergRPCAdaptor} is just one instance per {@link UberNetworkingClient}. | |
* | |
* @param <ReqT> Request msg of Generic Type. | |
* @param <RespT> Response msg of Generic Type. | |
* @param <T> MethodInfo of generic Type. | |
*/ | |
public class UberNetworkingClient<ReqT, RespT, T> extends UberInterceptor<ReqT, RespT, T> { | |
private static final Logger LOGGER = Logger.getLogger(UberNetworkingClient.class.getName()); | |
@Nullable private static UberNetworkingClient singleInstance = null; | |
private final UbergRPCAdaptor ubergRPCAdaptor; | |
private final List<UberInterceptor> clientInterceptors; | |
/** | |
* Instantiates {@link UberNetworkingClient} with a list of interceptors. | |
* | |
* @param clientInterceptors list of client interceptors. | |
*/ | |
private UberNetworkingClient(List<UberInterceptor> clientInterceptors) { | |
super(""); | |
// create grpc adaptor | |
ubergRPCAdaptor = | |
UbergRPCAdaptor.getInstance( | |
new HostPortPair( | |
UberInternalCallOptions.DEFAULT_USER_DEFINED_HOST, | |
UberInternalCallOptions.DEFAULT_USER_DEFINED_PORT)); | |
this.clientInterceptors = clientInterceptors; | |
} | |
/** | |
* Singleton instantiation of {@link UberNetworkingClient}. | |
* | |
* @param clientInterceptors list of client interceptors. | |
* @return the {@link UberNetworkingClient} thus instantiated. | |
*/ | |
public static UberNetworkingClient getInstance(List<UberInterceptor> clientInterceptors) { | |
if (singleInstance == null) { | |
singleInstance = new UberNetworkingClient(clientInterceptors); | |
} | |
return singleInstance; | |
} | |
@Override | |
public UberInterceptor newCall( | |
MethodInfo<T> method, UberInternalCallOptions uberInternalCallOptions) { | |
// create chain of interceptors. | |
UberInterceptor baseInter = new UberCallRouterInterceptor("CallRouter", ubergRPCAdaptor); | |
// add core interceptors in reverse ordering | |
// add user passed in interceptors | |
for (UberInterceptor interceptor : clientInterceptors) { | |
interceptor.setNext(baseInter); | |
baseInter = interceptor; | |
} | |
return baseInter.newCall(method, uberInternalCallOptions); | |
} | |
/** @return the authority of {@link UberNetworkingClient}. */ | |
public String authority() { | |
return ubergRPCAdaptor.authority(); | |
} | |
/** | |
* Shutsdown the managed channel in use. | |
* | |
* @return the managed channel thus shutdown. | |
*/ | |
@Nullable | |
public ManagedChannel shutdown() { | |
return ubergRPCAdaptor.shutdown(); | |
} | |
/** @return true if the networking client is shutdown. else false. */ | |
public boolean isShutdown() { | |
return false; | |
} | |
/** @return true if networking client is termined. else false. */ | |
public boolean isTerminated() { | |
return false; | |
} | |
/** | |
* Shuts down the networking client immediately. | |
* | |
* @return the managed channel thus shutdown. | |
*/ | |
@Nullable | |
public ManagedChannel shutdownNow() { | |
return ubergRPCAdaptor.shutdownNow(); | |
} | |
/** | |
* Awaits termination of networking client. | |
* | |
* @param timeout max timeout to await for termination of networking client. | |
* @param unit timeunit for timeout. | |
* @return true if termination succeeded. else false. | |
* @throws InterruptedException on any exception during termination. | |
*/ | |
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { | |
return ubergRPCAdaptor.awaitTermination(timeout, unit); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment