test_health_check_service_impl.cc 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #include "test/cpp/end2end/test_health_check_service_impl.h"
  19. #include <grpc/grpc.h>
  20. using grpc::health::v1::HealthCheckRequest;
  21. using grpc::health::v1::HealthCheckResponse;
  22. namespace grpc {
  23. namespace testing {
  24. Status HealthCheckServiceImpl::Check(ServerContext* /*context*/,
  25. const HealthCheckRequest* request,
  26. HealthCheckResponse* response) {
  27. std::lock_guard<std::mutex> lock(mu_);
  28. auto iter = status_map_.find(request->service());
  29. if (iter == status_map_.end()) {
  30. return Status(StatusCode::NOT_FOUND, "");
  31. }
  32. response->set_status(iter->second);
  33. return Status::OK;
  34. }
  35. Status HealthCheckServiceImpl::Watch(
  36. ServerContext* context, const HealthCheckRequest* request,
  37. grpc::ServerWriter<HealthCheckResponse>* writer) {
  38. auto last_state = HealthCheckResponse::UNKNOWN;
  39. while (!context->IsCancelled()) {
  40. {
  41. std::lock_guard<std::mutex> lock(mu_);
  42. HealthCheckResponse response;
  43. auto iter = status_map_.find(request->service());
  44. if (iter == status_map_.end()) {
  45. response.set_status(response.SERVICE_UNKNOWN);
  46. } else {
  47. response.set_status(iter->second);
  48. }
  49. if (response.status() != last_state) {
  50. writer->Write(response, grpc::WriteOptions());
  51. last_state = response.status();
  52. }
  53. }
  54. gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC),
  55. gpr_time_from_millis(1000, GPR_TIMESPAN)));
  56. }
  57. return Status::OK;
  58. }
  59. void HealthCheckServiceImpl::SetStatus(
  60. const std::string& service_name,
  61. HealthCheckResponse::ServingStatus status) {
  62. std::lock_guard<std::mutex> lock(mu_);
  63. if (shutdown_) {
  64. status = HealthCheckResponse::NOT_SERVING;
  65. }
  66. status_map_[service_name] = status;
  67. }
  68. void HealthCheckServiceImpl::SetAll(HealthCheckResponse::ServingStatus status) {
  69. std::lock_guard<std::mutex> lock(mu_);
  70. if (shutdown_) {
  71. return;
  72. }
  73. for (auto iter = status_map_.begin(); iter != status_map_.end(); ++iter) {
  74. iter->second = status;
  75. }
  76. }
  77. void HealthCheckServiceImpl::Shutdown() {
  78. std::lock_guard<std::mutex> lock(mu_);
  79. if (shutdown_) {
  80. return;
  81. }
  82. shutdown_ = true;
  83. for (auto iter = status_map_.begin(); iter != status_map_.end(); ++iter) {
  84. iter->second = HealthCheckResponse::NOT_SERVING;
  85. }
  86. }
  87. } // namespace testing
  88. } // namespace grpc