2
0

Health.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. public import GRPCCore
  17. /// ``Health`` is gRPC’s mechanism for checking whether a server is able to handle RPCs. Its semantics are documented in
  18. /// https://github.com/grpc/grpc/blob/master/doc/health-checking.md.
  19. ///
  20. /// `Health` initializes a new ``Health/Service-swift.struct`` and ``Health/Provider-swift.struct``.
  21. /// - `Health.Service` implements the Health service from the `grpc.health.v1` package and can be registered with a server
  22. /// like any other service.
  23. /// - `Health.Provider` provides status updates to `Health.Service`. `Health.Service` doesn't know about the other
  24. /// services running on a server so it must be provided with status updates via `Health.Provider`. To make specifying the service
  25. /// being updated easier, the generated code for services includes an extension to `ServiceDescriptor`.
  26. ///
  27. /// The following shows an example of initializing a Health service and updating the status of the `Foo` service in the `bar` package.
  28. ///
  29. /// ```swift
  30. /// let health = Health()
  31. /// let server = GRPCServer(
  32. /// transport: transport,
  33. /// services: [health.service, FooService()]
  34. /// )
  35. ///
  36. /// health.provider.updateStatus(
  37. /// .serving,
  38. /// forService: .bar_Foo
  39. /// )
  40. /// ```
  41. @available(macOS 15.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
  42. public struct Health: Sendable {
  43. /// An implementation of the `grpc.health.v1.Health` service.
  44. public let service: Health.Service
  45. /// Provides status updates to the Health service.
  46. public let provider: Health.Provider
  47. /// Constructs a new ``Health``, initializing a ``Health/Service-swift.struct`` and a
  48. /// ``Health/Provider-swift.struct``.
  49. public init() {
  50. let healthService = HealthService()
  51. self.service = Health.Service(healthService: healthService)
  52. self.provider = Health.Provider(healthService: healthService)
  53. }
  54. }
  55. @available(macOS 15.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
  56. extension Health {
  57. /// An implementation of the `grpc.health.v1.Health` service.
  58. public struct Service: RegistrableRPCService, Sendable {
  59. private let healthService: HealthService
  60. public func registerMethods(with router: inout RPCRouter) {
  61. self.healthService.registerMethods(with: &router)
  62. }
  63. fileprivate init(healthService: HealthService) {
  64. self.healthService = healthService
  65. }
  66. }
  67. /// Provides status updates to ``Health/Service-swift.struct``.
  68. public struct Provider: Sendable {
  69. private let healthService: HealthService
  70. /// Updates the status of a service.
  71. ///
  72. /// - Parameters:
  73. /// - status: The status of the service.
  74. /// - service: The description of the service.
  75. public func updateStatus(
  76. _ status: ServingStatus,
  77. forService service: ServiceDescriptor
  78. ) {
  79. self.healthService.updateStatus(
  80. Grpc_Health_V1_HealthCheckResponse.ServingStatus(status),
  81. forService: service.fullyQualifiedService
  82. )
  83. }
  84. /// Updates the status of a service.
  85. ///
  86. /// - Parameters:
  87. /// - status: The status of the service.
  88. /// - service: The fully qualified service name in the format:
  89. /// - "package.service": if the service is part of a package. For example, "helloworld.Greeter".
  90. /// - "service": if the service is not part of a package. For example, "Greeter".
  91. public func updateStatus(
  92. _ status: ServingStatus,
  93. forService service: String
  94. ) {
  95. self.healthService.updateStatus(
  96. Grpc_Health_V1_HealthCheckResponse.ServingStatus(status),
  97. forService: service
  98. )
  99. }
  100. fileprivate init(healthService: HealthService) {
  101. self.healthService = healthService
  102. }
  103. }
  104. }
  105. extension Grpc_Health_V1_HealthCheckResponse.ServingStatus {
  106. package init(_ status: ServingStatus) {
  107. switch status.value {
  108. case .serving:
  109. self = .serving
  110. case .notServing:
  111. self = .notServing
  112. }
  113. }
  114. }