HealthServiceImpl.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #region Copyright notice and license
  2. // Copyright 2015 gRPC authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Linq;
  19. using System.Text;
  20. using System.Threading.Tasks;
  21. using Grpc.Core;
  22. using Grpc.Core.Utils;
  23. using Grpc.Health.V1;
  24. namespace Grpc.HealthCheck
  25. {
  26. /// <summary>
  27. /// Implementation of a simple Health service. Useful for health checking.
  28. ///
  29. /// Registering service with a server:
  30. /// <code>
  31. /// var serviceImpl = new HealthServiceImpl();
  32. /// server = new Server();
  33. /// server.AddServiceDefinition(Grpc.Health.V1.Health.BindService(serviceImpl));
  34. /// </code>
  35. /// </summary>
  36. public class HealthServiceImpl : Grpc.Health.V1.Health.HealthBase
  37. {
  38. private readonly object myLock = new object();
  39. private readonly Dictionary<string, HealthCheckResponse.Types.ServingStatus> statusMap =
  40. new Dictionary<string, HealthCheckResponse.Types.ServingStatus>();
  41. /// <summary>
  42. /// Sets the health status for given service.
  43. /// </summary>
  44. /// <param name="service">The service. Cannot be null.</param>
  45. /// <param name="status">the health status</param>
  46. public void SetStatus(string service, HealthCheckResponse.Types.ServingStatus status)
  47. {
  48. lock (myLock)
  49. {
  50. statusMap[service] = status;
  51. }
  52. }
  53. /// <summary>
  54. /// Clears health status for given service.
  55. /// </summary>
  56. /// <param name="service">The service. Cannot be null.</param>
  57. public void ClearStatus(string service)
  58. {
  59. lock (myLock)
  60. {
  61. statusMap.Remove(service);
  62. }
  63. }
  64. /// <summary>
  65. /// Clears statuses for all services.
  66. /// </summary>
  67. public void ClearAll()
  68. {
  69. lock (myLock)
  70. {
  71. statusMap.Clear();
  72. }
  73. }
  74. /// <summary>
  75. /// Performs a health status check.
  76. /// </summary>
  77. /// <param name="request">The check request.</param>
  78. /// <param name="context">The call context.</param>
  79. /// <returns>The asynchronous response.</returns>
  80. public override Task<HealthCheckResponse> Check(HealthCheckRequest request, ServerCallContext context)
  81. {
  82. lock (myLock)
  83. {
  84. var service = request.Service;
  85. HealthCheckResponse.Types.ServingStatus status;
  86. if (!statusMap.TryGetValue(service, out status))
  87. {
  88. // TODO(jtattermusch): returning specific status from server handler is not supported yet.
  89. throw new RpcException(new Status(StatusCode.NotFound, ""));
  90. }
  91. return Task.FromResult(new HealthCheckResponse { Status = status });
  92. }
  93. }
  94. }
  95. }