Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 1 | #region Copyright notice and license |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 2 | // Copyright 2015 gRPC authors. |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 3 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 7 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 9 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 15 | #endregion |
| 16 | |
| 17 | using System; |
| 18 | using System.Collections.Generic; |
| 19 | using System.Linq; |
| 20 | using System.Text; |
| 21 | using System.Threading.Tasks; |
| 22 | |
| 23 | using Grpc.Core; |
| 24 | using Grpc.Core.Utils; |
yang-g | e171162 | 2016-02-19 13:06:37 -0800 | [diff] [blame] | 25 | using Grpc.Health.V1; |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 26 | |
| 27 | namespace Grpc.HealthCheck |
| 28 | { |
| 29 | /// <summary> |
| 30 | /// Implementation of a simple Health service. Useful for health checking. |
| 31 | /// |
| 32 | /// Registering service with a server: |
| 33 | /// <code> |
| 34 | /// var serviceImpl = new HealthServiceImpl(); |
| 35 | /// server = new Server(); |
yang-g | e171162 | 2016-02-19 13:06:37 -0800 | [diff] [blame] | 36 | /// server.AddServiceDefinition(Grpc.Health.V1.Health.BindService(serviceImpl)); |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 37 | /// </code> |
| 38 | /// </summary> |
Jan Tattermusch | d39426d | 2016-03-14 16:07:52 -0700 | [diff] [blame] | 39 | public class HealthServiceImpl : Grpc.Health.V1.Health.HealthBase |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 40 | { |
| 41 | private readonly object myLock = new object(); |
yang-g | a4598b4 | 2016-02-19 13:30:23 -0800 | [diff] [blame] | 42 | private readonly Dictionary<string, HealthCheckResponse.Types.ServingStatus> statusMap = |
| 43 | new Dictionary<string, HealthCheckResponse.Types.ServingStatus>(); |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 44 | |
| 45 | /// <summary> |
yang-g | a4598b4 | 2016-02-19 13:30:23 -0800 | [diff] [blame] | 46 | /// Sets the health status for given service. |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 47 | /// </summary> |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 48 | /// <param name="service">The service. Cannot be null.</param> |
| 49 | /// <param name="status">the health status</param> |
yang-g | a4598b4 | 2016-02-19 13:30:23 -0800 | [diff] [blame] | 50 | public void SetStatus(string service, HealthCheckResponse.Types.ServingStatus status) |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 51 | { |
| 52 | lock (myLock) |
| 53 | { |
yang-g | a4598b4 | 2016-02-19 13:30:23 -0800 | [diff] [blame] | 54 | statusMap[service] = status; |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
| 58 | /// <summary> |
yang-g | a4598b4 | 2016-02-19 13:30:23 -0800 | [diff] [blame] | 59 | /// Clears health status for given service. |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 60 | /// </summary> |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 61 | /// <param name="service">The service. Cannot be null.</param> |
yang-g | a4598b4 | 2016-02-19 13:30:23 -0800 | [diff] [blame] | 62 | public void ClearStatus(string service) |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 63 | { |
| 64 | lock (myLock) |
| 65 | { |
yang-g | a4598b4 | 2016-02-19 13:30:23 -0800 | [diff] [blame] | 66 | statusMap.Remove(service); |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
| 70 | /// <summary> |
yang-g | a4598b4 | 2016-02-19 13:30:23 -0800 | [diff] [blame] | 71 | /// Clears statuses for all services. |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 72 | /// </summary> |
| 73 | public void ClearAll() |
| 74 | { |
| 75 | lock (myLock) |
| 76 | { |
| 77 | statusMap.Clear(); |
| 78 | } |
| 79 | } |
| 80 | |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 81 | /// <summary> |
| 82 | /// Performs a health status check. |
| 83 | /// </summary> |
| 84 | /// <param name="request">The check request.</param> |
| 85 | /// <param name="context">The call context.</param> |
| 86 | /// <returns>The asynchronous response.</returns> |
Jan Tattermusch | d39426d | 2016-03-14 16:07:52 -0700 | [diff] [blame] | 87 | public override Task<HealthCheckResponse> Check(HealthCheckRequest request, ServerCallContext context) |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 88 | { |
| 89 | lock (myLock) |
| 90 | { |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 91 | var service = request.Service; |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 92 | |
| 93 | HealthCheckResponse.Types.ServingStatus status; |
yang-g | a4598b4 | 2016-02-19 13:30:23 -0800 | [diff] [blame] | 94 | if (!statusMap.TryGetValue(service, out status)) |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 95 | { |
| 96 | // TODO(jtattermusch): returning specific status from server handler is not supported yet. |
| 97 | throw new RpcException(new Status(StatusCode.NotFound, "")); |
| 98 | } |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 99 | return Task.FromResult(new HealthCheckResponse { Status = status }); |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 100 | } |
| 101 | } |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 102 | } |
| 103 | } |