HealthService.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright 2024, gRPC Authors All rights reserved.
  3. *
  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
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  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.
  15. */
  16. internal import GRPCCore
  17. @available(macOS 15.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
  18. internal struct HealthService: Grpc_Health_V1_HealthServiceProtocol {
  19. private let state = HealthService.State()
  20. func check(
  21. request: ServerRequest.Single<Grpc_Health_V1_HealthCheckRequest>
  22. ) async throws -> ServerResponse.Single<Grpc_Health_V1_HealthCheckResponse> {
  23. let service = request.message.service
  24. guard let status = self.state.currentStatus(ofService: service) else {
  25. throw RPCError(code: .notFound, message: "Requested service unknown.")
  26. }
  27. var response = Grpc_Health_V1_HealthCheckResponse()
  28. response.status = status
  29. return ServerResponse.Single(message: response)
  30. }
  31. func watch(
  32. request: ServerRequest.Single<Grpc_Health_V1_HealthCheckRequest>
  33. ) async -> ServerResponse.Stream<Grpc_Health_V1_HealthCheckResponse> {
  34. let service = request.message.service
  35. let statuses = AsyncStream.makeStream(of: Grpc_Health_V1_HealthCheckResponse.ServingStatus.self)
  36. self.state.addContinuation(statuses.continuation, forService: service)
  37. return ServerResponse.Stream(
  38. of: Grpc_Health_V1_HealthCheckResponse.self
  39. ) { writer in
  40. var response = Grpc_Health_V1_HealthCheckResponse()
  41. for await status in statuses.stream {
  42. response.status = status
  43. try await writer.write(response)
  44. }
  45. return [:]
  46. }
  47. }
  48. func updateStatus(
  49. _ status: Grpc_Health_V1_HealthCheckResponse.ServingStatus,
  50. forService service: String
  51. ) {
  52. self.state.updateStatus(status, forService: service)
  53. }
  54. }
  55. @available(macOS 15.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
  56. extension HealthService {
  57. private struct State: Sendable {
  58. // The state of each service keyed by the fully qualified service name.
  59. private let lockedStorage = LockedValueBox([String: ServiceState]())
  60. fileprivate func currentStatus(
  61. ofService service: String
  62. ) -> Grpc_Health_V1_HealthCheckResponse.ServingStatus? {
  63. return self.lockedStorage.withLockedValue { $0[service]?.currentStatus }
  64. }
  65. fileprivate func updateStatus(
  66. _ status: Grpc_Health_V1_HealthCheckResponse.ServingStatus,
  67. forService service: String
  68. ) {
  69. self.lockedStorage.withLockedValue { storage in
  70. storage[service, default: ServiceState(status: status)].updateStatus(status)
  71. }
  72. }
  73. fileprivate func addContinuation(
  74. _ continuation: AsyncStream<Grpc_Health_V1_HealthCheckResponse.ServingStatus>.Continuation,
  75. forService service: String
  76. ) {
  77. self.lockedStorage.withLockedValue { storage in
  78. storage[service, default: ServiceState(status: .serviceUnknown)]
  79. .addContinuation(continuation)
  80. }
  81. }
  82. }
  83. // Encapsulates the current status of a service and the continuations of its watch streams.
  84. private struct ServiceState: Sendable {
  85. private(set) var currentStatus: Grpc_Health_V1_HealthCheckResponse.ServingStatus
  86. private var continuations:
  87. [AsyncStream<Grpc_Health_V1_HealthCheckResponse.ServingStatus>.Continuation]
  88. fileprivate mutating func updateStatus(
  89. _ status: Grpc_Health_V1_HealthCheckResponse.ServingStatus
  90. ) {
  91. guard status != self.currentStatus else {
  92. return
  93. }
  94. self.currentStatus = status
  95. for continuation in self.continuations {
  96. continuation.yield(status)
  97. }
  98. }
  99. fileprivate mutating func addContinuation(
  100. _ continuation: AsyncStream<Grpc_Health_V1_HealthCheckResponse.ServingStatus>.Continuation
  101. ) {
  102. self.continuations.append(continuation)
  103. continuation.yield(self.currentStatus)
  104. }
  105. fileprivate init(status: Grpc_Health_V1_HealthCheckResponse.ServingStatus = .unknown) {
  106. self.currentStatus = status
  107. self.continuations = []
  108. }
  109. }
  110. }