protected static Channel TryGetChannelFromLoadBalancer(Channel lbChannel, ChannelCredentials credentials, string serviceName, int maxMesssageLength) { ServiceDiscoveryGrpc.ServiceDiscoveryGrpcClient lbClient = new ServiceDiscoveryGrpc.ServiceDiscoveryGrpcClient(lbChannel); DiscoverServicesResponse lbResponse = lbClient.DiscoverTopServices( new DiscoverServicesRequest { ServiceName = serviceName, MaxCount = 1 }); if (lbResponse.ServiceLocations.Count > 0) { ServiceLocationMsg serviceLocation = lbResponse.ServiceLocations[0]; Channel result = GrpcUtils.CreateChannel(serviceLocation.HostName, serviceLocation.Port, credentials, maxMesssageLength); _msg.DebugFormat("The load balancer is suggesting {0}", result.ResolvedTarget); return(result); } // Assumption: A load balancer is never also serving real requests -> lets not use it at all! _msg.Debug("The load balancer has no service locations available."); return(null); }
protected void OpenChannel(bool useTls, string clientCertificate = null, bool assumeLoadBalancer = false) { if (string.IsNullOrEmpty(HostName)) { _msg.Debug("Host name is null or empty. No channel opened."); return; } ChannelCredentials credentials = GrpcUtils.CreateChannelCredentials(useTls, clientCertificate); var enoughForLargeGeometries = (int)Math.Pow(1024, 3); Channel channel = GrpcUtils.CreateChannel( HostName, Port, credentials, enoughForLargeGeometries); if (assumeLoadBalancer) { ChannelIsLoadBalancer = IsServingLoadBalancerEndpoint(channel, credentials, ServiceName, enoughForLargeGeometries); } Channel = channel; _msg.DebugFormat("Created grpc channel to {0} on port {1}", HostName, Port); _healthClient = new Health.HealthClient(Channel); ChannelOpenedCore(Channel); }
public async Task <bool> CanAcceptCallsAsync() { if (!TryGetHealthClient(out Health.HealthClient healthClient)) { return(false); } string serviceName = ChannelServiceName; StatusCode statusCode = await GrpcUtils.IsServingAsync(healthClient, serviceName); _msg.DebugFormat("Health status for service {0} at {1}: {2}.", serviceName, Channel?.ResolvedTarget, statusCode); return(statusCode == StatusCode.OK); }
public bool CanAcceptCalls() { if (!TryGetHealthClient(out Health.HealthClient healthClient)) { return(false); } string serviceName = ChannelServiceName; bool result = GrpcUtils.IsServing(healthClient, serviceName, out StatusCode statusCode); _msg.DebugFormat("Service {0} is serving at {1}: {2}. Status: {3}", serviceName, Channel?.ResolvedTarget, result, statusCode); return(result); }
private static bool IsServingLoadBalancerEndpoint( [NotNull] Channel channel, [NotNull] ChannelCredentials credentials, string serviceName, int enoughForLargeGeometries) { var channelHealth = new Health.HealthClient(channel); bool isServingEndpoint = GrpcUtils.IsServing(channelHealth, serviceName, out _); if (isServingEndpoint) { return(false); } bool isLoadBalancer = GrpcUtils.IsServing(channelHealth, nameof(ServiceDiscoveryGrpc), out StatusCode lbStatusCode); if (isLoadBalancer) { _msg.DebugFormat("{0} is a load balancer address.", channel.ResolvedTarget); Channel suggestedLocation = TryGetChannelFromLoadBalancer(channel, credentials, serviceName, enoughForLargeGeometries); if (suggestedLocation != null) { _msg.DebugFormat("Using serving load balancer at {0}", channel.ResolvedTarget); return(true); } // Assumption: A load balancer is never also serving real requests -> lets not use it at all! _msg.Debug( "The load balancer has no service locations available. It will not be used."); return(false); } _msg.DebugFormat("No {0} service and no serving load balancer at {1}. Error code: {2}", serviceName, channel.ResolvedTarget, lbStatusCode); return(false); }
protected void OpenChannel(bool useTls, string clientCertificate = null) { if (string.IsNullOrEmpty(HostName)) { _msg.Debug("Host name is null or empty. No channel opened."); return; } var enoughForLargeGeometries = (int)Math.Pow(1024, 3); ChannelCredentials credentials = GrpcUtils.CreateChannelCredentials(useTls, clientCertificate); Channel = GrpcUtils.CreateChannel( HostName, Port, credentials, enoughForLargeGeometries); _msg.DebugFormat("Created grpc channel to {0} on port {1}", HostName, Port); _healthClient = new Health.HealthClient(Channel); ChannelOpenedCore(Channel); }