/** * From IMessagingClient */ @Override public ListenableFuture<RapidResponse> sendMessage(final Endpoint remote, final RapidRequest msg) { Objects.requireNonNull(remote); Objects.requireNonNull(msg); final Supplier<ListenableFuture<RapidResponse>> call = () -> { final MembershipServiceFutureStub stub = getFutureStub(remote) .withDeadlineAfter(getTimeoutForMessageMs(msg), TimeUnit.MILLISECONDS); return stub.sendRequest(msg); }; final Runnable onCallFailure = () -> channelMap.invalidate(remote); return Retries.callWithRetries(call, remote, settings.getGrpcDefaultRetries(), onCallFailure, backgroundExecutor); }
public GrpcClient(final Endpoint address, final SharedResources sharedResources, final ISettings settings) { this.address = address; this.settings = settings; this.grpcExecutor = sharedResources.getClientChannelExecutor(); this.backgroundExecutor = sharedResources.getBackgroundExecutor(); this.eventLoopGroup = settings.getUseInProcessTransport() ? null : sharedResources.getEventLoopGroup(); final RemovalListener<Endpoint, Channel> removalListener = removal -> shutdownChannel((ManagedChannel) removal.getValue()); this.channelMap = CacheBuilder.newBuilder() .expireAfterAccess(30, TimeUnit.SECONDS) .removalListener(removalListener) .build(new CacheLoader<Endpoint, Channel>() { @Override public Channel load(final Endpoint endpoint) { return getChannel(endpoint); } }); }
private Channel getChannel(final Endpoint remote) { // TODO: allow configuring SSL/TLS Channel channel; LOG.debug("Creating channel from {} to {}", address, remote); if (settings.getUseInProcessTransport()) { channel = InProcessChannelBuilder .forName(remote.toString()) .executor(grpcExecutor) .usePlaintext(true) .idleTimeout(10, TimeUnit.SECONDS) .build(); } else { channel = NettyChannelBuilder .forAddress(remote.getHostname(), remote.getPort()) .executor(grpcExecutor) .eventLoopGroup(eventLoopGroup) .usePlaintext(true) .idleTimeout(10, TimeUnit.SECONDS) .withOption(ChannelOption.SO_REUSEADDR, true) .withOption(ChannelOption.SO_SNDBUF, DEFAULT_BUF_SIZE) .withOption(ChannelOption.SO_RCVBUF, DEFAULT_BUF_SIZE) .build(); } return channel; }
/** * TODO: These timeouts should be on the Rapid side of the IMessagingClient API. * * @param msg RapidRequest * @return timeout to use for the RapidRequest message */ private int getTimeoutForMessageMs(final RapidRequest msg) { switch (msg.getContentCase()) { case PROBEMESSAGE: return settings.getGrpcProbeTimeoutMs(); case JOINMESSAGE: return settings.getGrpcJoinTimeoutMs(); default: return settings.getGrpcTimeoutMs(); } }