test_health_check_service_impl.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2018 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
20 
21 #include <grpc/grpc.h>
22 
23 using grpc::health::v1::HealthCheckRequest;
24 using grpc::health::v1::HealthCheckResponse;
25 
26 namespace grpc {
27 namespace testing {
28 
30  const HealthCheckRequest* request,
31  HealthCheckResponse* response) {
32  std::lock_guard<std::mutex> lock(mu_);
33  auto iter = status_map_.find(request->service());
34  if (iter == status_map_.end()) {
35  return Status(StatusCode::NOT_FOUND, "");
36  }
37  response->set_status(iter->second);
38  return Status::OK;
39 }
40 
42  ServerContext* context, const HealthCheckRequest* request,
44  auto last_state = HealthCheckResponse::UNKNOWN;
45  while (!context->IsCancelled()) {
46  {
47  std::lock_guard<std::mutex> lock(mu_);
48  HealthCheckResponse response;
49  auto iter = status_map_.find(request->service());
50  if (iter == status_map_.end()) {
51  response.set_status(response.SERVICE_UNKNOWN);
52  } else {
53  response.set_status(iter->second);
54  }
55  if (response.status() != last_state) {
57  last_state = response.status();
58  }
59  }
62  }
63  return Status::OK;
64 }
65 
67  const std::string& service_name,
68  HealthCheckResponse::ServingStatus status) {
69  std::lock_guard<std::mutex> lock(mu_);
70  if (shutdown_) {
71  status = HealthCheckResponse::NOT_SERVING;
72  }
73  status_map_[service_name] = status;
74 }
75 
76 void HealthCheckServiceImpl::SetAll(HealthCheckResponse::ServingStatus status) {
77  std::lock_guard<std::mutex> lock(mu_);
78  if (shutdown_) {
79  return;
80  }
81  for (auto iter = status_map_.begin(); iter != status_map_.end(); ++iter) {
82  iter->second = status;
83  }
84 }
85 
87  std::lock_guard<std::mutex> lock(mu_);
88  if (shutdown_) {
89  return;
90  }
91  shutdown_ = true;
92  for (auto iter = status_map_.begin(); iter != status_map_.end(); ++iter) {
93  iter->second = HealthCheckResponse::NOT_SERVING;
94  }
95 }
96 
97 } // namespace testing
98 } // namespace grpc
GPR_TIMESPAN
@ GPR_TIMESPAN
Definition: gpr_types.h:45
testing
Definition: aws_request_signer_test.cc:25
grpc::status
auto status
Definition: cpp/client/credentials_test.cc:200
grpc::ServerWriter
Definition: include/grpcpp/impl/codegen/completion_queue.h:60
grpc::ServerContext
Definition: grpcpp/impl/codegen/server_context.h:566
grpc
Definition: grpcpp/alarm.h:33
benchmark.request
request
Definition: benchmark.py:77
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc::testing::HealthCheckServiceImpl::status_map_
std::map< const std::string, health::v1::HealthCheckResponse::ServingStatus > status_map_
Definition: test_health_check_service_impl.h:52
grpc::WriteOptions
Per-message write options.
Definition: call_op_set.h:81
grpc::testing::HealthCheckServiceImpl::shutdown_
bool shutdown_
Definition: test_health_check_service_impl.h:50
grpc.StatusCode.NOT_FOUND
tuple NOT_FOUND
Definition: src/python/grpcio/grpc/__init__.py:266
gpr_sleep_until
GPRAPI void gpr_sleep_until(gpr_timespec until)
grpc.h
grpc::testing::HealthCheckServiceImpl::SetAll
void SetAll(health::v1::HealthCheckResponse::ServingStatus status)
Definition: test_health_check_service_impl.cc:76
GPR_CLOCK_MONOTONIC
@ GPR_CLOCK_MONOTONIC
Definition: gpr_types.h:36
grpc::Status::OK
static const Status & OK
An OK pre-defined instance.
Definition: include/grpcpp/impl/codegen/status.h:113
grpc::testing::HealthCheckServiceImpl::Shutdown
void Shutdown()
Definition: test_health_check_service_impl.cc:86
gpr_now
GPRAPI gpr_timespec gpr_now(gpr_clock_type clock)
writer
void writer(void *n)
Definition: libuv/docs/code/locks/main.c:22
gpr_time_add
GPRAPI gpr_timespec gpr_time_add(gpr_timespec a, gpr_timespec b)
Definition: src/core/lib/gpr/time.cc:135
grpc::testing::HealthCheckServiceImpl::Watch
Status Watch(ServerContext *context, const health::v1::HealthCheckRequest *request, ServerWriter< health::v1::HealthCheckResponse > *writer) override
Definition: test_health_check_service_impl.cc:41
asyncio_get_stats.response
response
Definition: asyncio_get_stats.py:28
grpc::testing::HealthCheckServiceImpl::SetStatus
void SetStatus(const std::string &service_name, health::v1::HealthCheckResponse::ServingStatus status)
Definition: test_health_check_service_impl.cc:66
grpc::protobuf::util::Status
GRPC_CUSTOM_UTIL_STATUS Status
Definition: include/grpcpp/impl/codegen/config_protobuf.h:93
grpc::Status
Definition: include/grpcpp/impl/codegen/status.h:35
gpr_time_from_millis
GPRAPI gpr_timespec gpr_time_from_millis(int64_t ms, gpr_clock_type clock_type)
Definition: src/core/lib/gpr/time.cc:119
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
iter
Definition: test_winkernel.cpp:47
grpc::testing::HealthCheckServiceImpl::Check
Status Check(ServerContext *context, const health::v1::HealthCheckRequest *request, health::v1::HealthCheckResponse *response) override
Definition: test_health_check_service_impl.cc:29
grpc::testing::HealthCheckServiceImpl::mu_
std::mutex mu_
Definition: test_health_check_service_impl.h:49
test_health_check_service_impl.h


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:28