LoadBalancer.swift 1.7 KB

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