OLD | NEW |
(Empty) | |
| 1 #region Copyright notice and license |
| 2 // Copyright 2015-2016, Google Inc. |
| 3 // All rights reserved. |
| 4 // |
| 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are |
| 7 // met: |
| 8 // |
| 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. |
| 11 // * Redistributions in binary form must reproduce the above |
| 12 // copyright notice, this list of conditions and the following disclaimer |
| 13 // in the documentation and/or other materials provided with the |
| 14 // distribution. |
| 15 // * Neither the name of Google Inc. nor the names of its |
| 16 // contributors may be used to endorse or promote products derived from |
| 17 // this software without specific prior written permission. |
| 18 // |
| 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 #endregion |
| 31 |
| 32 using System; |
| 33 using System.Collections.Generic; |
| 34 using System.Linq; |
| 35 using System.Text; |
| 36 using System.Threading.Tasks; |
| 37 |
| 38 using Grpc.Core; |
| 39 using Grpc.Core.Utils; |
| 40 using Grpc.Health.V1; |
| 41 |
| 42 namespace Grpc.HealthCheck |
| 43 { |
| 44 /// <summary> |
| 45 /// Implementation of a simple Health service. Useful for health checking. |
| 46 /// |
| 47 /// Registering service with a server: |
| 48 /// <code> |
| 49 /// var serviceImpl = new HealthServiceImpl(); |
| 50 /// server = new Server(); |
| 51 /// server.AddServiceDefinition(Grpc.Health.V1.Health.BindService(serviceImp
l)); |
| 52 /// </code> |
| 53 /// </summary> |
| 54 public class HealthServiceImpl : Grpc.Health.V1.Health.IHealth |
| 55 { |
| 56 private readonly object myLock = new object(); |
| 57 private readonly Dictionary<string, HealthCheckResponse.Types.ServingSta
tus> statusMap = |
| 58 new Dictionary<string, HealthCheckResponse.Types.ServingStatus>(); |
| 59 |
| 60 /// <summary> |
| 61 /// Sets the health status for given service. |
| 62 /// </summary> |
| 63 /// <param name="service">The service. Cannot be null.</param> |
| 64 /// <param name="status">the health status</param> |
| 65 public void SetStatus(string service, HealthCheckResponse.Types.ServingS
tatus status) |
| 66 { |
| 67 lock (myLock) |
| 68 { |
| 69 statusMap[service] = status; |
| 70 } |
| 71 } |
| 72 |
| 73 /// <summary> |
| 74 /// Clears health status for given service. |
| 75 /// </summary> |
| 76 /// <param name="service">The service. Cannot be null.</param> |
| 77 public void ClearStatus(string service) |
| 78 { |
| 79 lock (myLock) |
| 80 { |
| 81 statusMap.Remove(service); |
| 82 } |
| 83 } |
| 84 |
| 85 /// <summary> |
| 86 /// Clears statuses for all services. |
| 87 /// </summary> |
| 88 public void ClearAll() |
| 89 { |
| 90 lock (myLock) |
| 91 { |
| 92 statusMap.Clear(); |
| 93 } |
| 94 } |
| 95 |
| 96 /// <summary> |
| 97 /// Performs a health status check. |
| 98 /// </summary> |
| 99 /// <param name="request">The check request.</param> |
| 100 /// <param name="context">The call context.</param> |
| 101 /// <returns>The asynchronous response.</returns> |
| 102 public Task<HealthCheckResponse> Check(HealthCheckRequest request, Serve
rCallContext context) |
| 103 { |
| 104 lock (myLock) |
| 105 { |
| 106 var service = request.Service; |
| 107 |
| 108 HealthCheckResponse.Types.ServingStatus status; |
| 109 if (!statusMap.TryGetValue(service, out status)) |
| 110 { |
| 111 // TODO(jtattermusch): returning specific status from server
handler is not supported yet. |
| 112 throw new RpcException(new Status(StatusCode.NotFound, "")); |
| 113 } |
| 114 return Task.FromResult(new HealthCheckResponse { Status = status
}); |
| 115 } |
| 116 } |
| 117 } |
| 118 } |
OLD | NEW |