LoadBalancer.swift 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. @available(macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *)
  17. package enum LoadBalancer: Sendable {
  18. case roundRobin(RoundRobinLoadBalancer)
  19. case pickFirst(PickFirstLoadBalancer)
  20. }
  21. @available(macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *)
  22. extension LoadBalancer {
  23. package init(_ loadBalancer: RoundRobinLoadBalancer) {
  24. self = .roundRobin(loadBalancer)
  25. }
  26. var id: LoadBalancerID {
  27. switch self {
  28. case .roundRobin(let loadBalancer):
  29. return loadBalancer.id
  30. case .pickFirst(let loadBalancer):
  31. return loadBalancer.id
  32. }
  33. }
  34. package var events: AsyncStream<LoadBalancerEvent> {
  35. switch self {
  36. case .roundRobin(let loadBalancer):
  37. return loadBalancer.events
  38. case .pickFirst(let loadBalancer):
  39. return loadBalancer.events
  40. }
  41. }
  42. package func run() async {
  43. switch self {
  44. case .roundRobin(let loadBalancer):
  45. await loadBalancer.run()
  46. case .pickFirst(let loadBalancer):
  47. await loadBalancer.run()
  48. }
  49. }
  50. package func close() {
  51. switch self {
  52. case .roundRobin(let loadBalancer):
  53. loadBalancer.close()
  54. case .pickFirst(let loadBalancer):
  55. loadBalancer.close()
  56. }
  57. }
  58. package func pickSubchannel() -> Subchannel? {
  59. switch self {
  60. case .roundRobin(let loadBalancer):
  61. return loadBalancer.pickSubchannel()
  62. case .pickFirst(let loadBalancer):
  63. return loadBalancer.pickSubchannel()
  64. }
  65. }
  66. }