HealthService.swift 4.4 KB

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