ClientRPCExecutionConfigurationCollection.swift 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright 2023, 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 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
  17. /// A collection of ``ClientRPCExecutionConfiguration``s, mapped to specific methods or services.
  18. ///
  19. /// When creating a new instance, you must provide a default configuration to be used when getting
  20. /// a configuration for a method that has not been given a specific override.
  21. /// Use ``setDefaultConfiguration(_:forService:)`` to set a specific override for a whole
  22. /// service.
  23. ///
  24. /// Use the subscript to get and set configurations for methods.
  25. public struct ClientRPCExecutionConfigurationCollection: Sendable, Hashable {
  26. private var elements: [MethodDescriptor: ClientRPCExecutionConfiguration]
  27. private let defaultConfiguration: ClientRPCExecutionConfiguration
  28. public init(
  29. defaultConfiguration: ClientRPCExecutionConfiguration = ClientRPCExecutionConfiguration(
  30. executionPolicy: nil,
  31. timeout: nil
  32. )
  33. ) {
  34. self.elements = [:]
  35. self.defaultConfiguration = defaultConfiguration
  36. }
  37. public subscript(_ descriptor: MethodDescriptor) -> ClientRPCExecutionConfiguration {
  38. get {
  39. if let methodLevelOverride = self.elements[descriptor] {
  40. return methodLevelOverride
  41. }
  42. var serviceLevelDescriptor = descriptor
  43. serviceLevelDescriptor.method = ""
  44. return self.elements[serviceLevelDescriptor, default: self.defaultConfiguration]
  45. }
  46. set {
  47. precondition(
  48. !descriptor.service.isEmpty,
  49. "Method descriptor's service cannot be empty."
  50. )
  51. self.elements[descriptor] = newValue
  52. }
  53. }
  54. /// Set a default configuration for a service.
  55. ///
  56. /// If getting a configuration for a method that's part of a service, and the method itself doesn't have an
  57. /// override, then this configuration will be used instead of the default configuration passed when creating
  58. /// this instance of ``ClientRPCExecutionConfigurationCollection``.
  59. ///
  60. /// - Parameters:
  61. /// - configuration: The default configuration for the service.
  62. /// - service: The name of the service for which this override applies.
  63. public mutating func setDefaultConfiguration(
  64. _ configuration: ClientRPCExecutionConfiguration,
  65. forService service: String
  66. ) {
  67. self[MethodDescriptor(service: service, method: "")] = configuration
  68. }
  69. }