blob: 38c4900346ec252aa66f2483c257da5e607c78e9 [file] [log] [blame]
Jan Tattermusch52534672015-07-14 20:29:21 -07001#region Copyright notice and license
Jan Tattermusch7897ae92017-06-07 22:57:36 +02002// Copyright 2015 gRPC authors.
Jan Tattermusch52534672015-07-14 20:29:21 -07003//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02004// 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 Tattermusch52534672015-07-14 20:29:21 -07007//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02008// http://www.apache.org/licenses/LICENSE-2.0
Jan Tattermusch52534672015-07-14 20:29:21 -07009//
Jan Tattermusch7897ae92017-06-07 22:57:36 +020010// 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 Tattermusch52534672015-07-14 20:29:21 -070015#endregion
16
17using System;
18using System.Collections.Generic;
19using System.Linq;
20using System.Text;
21using System.Threading.Tasks;
22
23using Grpc.Core;
24using Grpc.Core.Utils;
yang-ge1711622016-02-19 13:06:37 -080025using Grpc.Health.V1;
Jan Tattermusch52534672015-07-14 20:29:21 -070026
27namespace 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-ge1711622016-02-19 13:06:37 -080036 /// server.AddServiceDefinition(Grpc.Health.V1.Health.BindService(serviceImpl));
Jan Tattermusch52534672015-07-14 20:29:21 -070037 /// </code>
38 /// </summary>
Jan Tattermuschd39426d2016-03-14 16:07:52 -070039 public class HealthServiceImpl : Grpc.Health.V1.Health.HealthBase
Jan Tattermusch52534672015-07-14 20:29:21 -070040 {
41 private readonly object myLock = new object();
yang-ga4598b42016-02-19 13:30:23 -080042 private readonly Dictionary<string, HealthCheckResponse.Types.ServingStatus> statusMap =
43 new Dictionary<string, HealthCheckResponse.Types.ServingStatus>();
Jan Tattermusch52534672015-07-14 20:29:21 -070044
45 /// <summary>
yang-ga4598b42016-02-19 13:30:23 -080046 /// Sets the health status for given service.
Jan Tattermusch52534672015-07-14 20:29:21 -070047 /// </summary>
Jan Tattermusch52534672015-07-14 20:29:21 -070048 /// <param name="service">The service. Cannot be null.</param>
49 /// <param name="status">the health status</param>
yang-ga4598b42016-02-19 13:30:23 -080050 public void SetStatus(string service, HealthCheckResponse.Types.ServingStatus status)
Jan Tattermusch52534672015-07-14 20:29:21 -070051 {
52 lock (myLock)
53 {
yang-ga4598b42016-02-19 13:30:23 -080054 statusMap[service] = status;
Jan Tattermusch52534672015-07-14 20:29:21 -070055 }
56 }
57
58 /// <summary>
yang-ga4598b42016-02-19 13:30:23 -080059 /// Clears health status for given service.
Jan Tattermusch52534672015-07-14 20:29:21 -070060 /// </summary>
Jan Tattermusch52534672015-07-14 20:29:21 -070061 /// <param name="service">The service. Cannot be null.</param>
yang-ga4598b42016-02-19 13:30:23 -080062 public void ClearStatus(string service)
Jan Tattermusch52534672015-07-14 20:29:21 -070063 {
64 lock (myLock)
65 {
yang-ga4598b42016-02-19 13:30:23 -080066 statusMap.Remove(service);
Jan Tattermusch52534672015-07-14 20:29:21 -070067 }
68 }
69
70 /// <summary>
yang-ga4598b42016-02-19 13:30:23 -080071 /// Clears statuses for all services.
Jan Tattermusch52534672015-07-14 20:29:21 -070072 /// </summary>
73 public void ClearAll()
74 {
75 lock (myLock)
76 {
77 statusMap.Clear();
78 }
79 }
80
Jan Tattermusch12855fc2015-08-24 16:43:23 -070081 /// <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 Tattermuschd39426d2016-03-14 16:07:52 -070087 public override Task<HealthCheckResponse> Check(HealthCheckRequest request, ServerCallContext context)
Jan Tattermusch52534672015-07-14 20:29:21 -070088 {
89 lock (myLock)
90 {
Jan Tattermusch8644aea2015-08-03 10:21:18 -070091 var service = request.Service;
Jan Tattermusch52534672015-07-14 20:29:21 -070092
93 HealthCheckResponse.Types.ServingStatus status;
yang-ga4598b42016-02-19 13:30:23 -080094 if (!statusMap.TryGetValue(service, out status))
Jan Tattermusch52534672015-07-14 20:29:21 -070095 {
96 // TODO(jtattermusch): returning specific status from server handler is not supported yet.
97 throw new RpcException(new Status(StatusCode.NotFound, ""));
98 }
Jan Tattermusch8644aea2015-08-03 10:21:18 -070099 return Task.FromResult(new HealthCheckResponse { Status = status });
Jan Tattermusch52534672015-07-14 20:29:21 -0700100 }
101 }
Jan Tattermusch52534672015-07-14 20:29:21 -0700102 }
103}