Skip to content

Instantly share code, notes, and snippets.

@nsivabalan
Created August 21, 2020 14:38
Show Gist options
  • Save nsivabalan/7afcfb0e0074bc37f310b8ec240ec873 to your computer and use it in GitHub Desktop.
Save nsivabalan/7afcfb0e0074bc37f310b8ec240ec873 to your computer and use it in GitHub Desktop.
/**
* Adaptor for Grpc which does all the translation from generic uber types to grpc entities and
* places the call to a grpc ManagedChannel. This class manages multiple channels and routes new
* calls based on host fetched from UberInternalCallOptions.
*/
public class UbergRPCAdaptor implements UberChannel {
private static final Logger LOGGER = Logger.getLogger(UbergRPCAdaptor.class.getName());
// static variable singleInstance of type Singleton
@Nullable private static UbergRPCAdaptor singleInstance;
private final Map<HostPortPair, ManagedChannel> channelMap = new HashMap<>();
private HostPortPair curHostPortPair;
private UbergRPCAdaptor(HostPortPair hostPortPair) {
createAndAddChannel(hostPortPair);
}
private void createAndAddChannel(HostPortPair hostPortPair) {
ManagedChannel channel =
ManagedChannelBuilder.forAddress(hostPortPair.getHost(), hostPortPair.getPort())
.usePlaintext()
.build();
channelMap.put(hostPortPair, channel);
curHostPortPair = hostPortPair;
}
/**
* Instantiates {@link UbergRPCAdaptor}.
*
* @param hostPortPair initial hostport pair to initialize {@link UbergRPCAdaptor} with.
* @return the singleton instance of {@link UbergRPCAdaptor}.
*/
public static UbergRPCAdaptor getInstance(HostPortPair hostPortPair) {
if (singleInstance == null) {
singleInstance = new UbergRPCAdaptor(hostPortPair);
}
return singleInstance;
}
@Override
public synchronized <RequestT, ResponseT, T> UberClientCall<RequestT, ResponseT> newCall(
MethodInfo<T> methodInfo, UberInternalCallOptions uberInternalCallOptions) {
// fetch channel corresponding to host and port in uberInternalCallOptions from internal channel map
return new UberClientCall<>(
channel.newCall(
(MethodDescriptor<RequestT, ResponseT>) methodInfo.getTransportMethodInfo(),
uberInternalCallOptions.getCallOptions()));
}
@Override
public String authority() {
// to be filled
}
@Override
@Nullable
public ManagedChannel shutdown() {
// to be filled
}
@Override
public boolean isShutdown() {
// to be filled
}
@Override
public boolean isTerminated() {
// to be filled
}
@Override
@Nullable
public ManagedChannel shutdownNow() {
// to be filled
}
@Override
public boolean awaitTermination(long timeout, TimeUnit unit) {
// to be filled
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment