MethodConfigs.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. package import GRPCCore
  17. /// A collection of ``MethodConfig``s, mapped to specific methods or services.
  18. ///
  19. /// When creating a new instance, no overrides and no default will be set for using when getting
  20. /// a configuration for a method that has not been given a specific override.
  21. /// Use ``setDefaultConfig(_:forService:)`` to set a specific override for a whole
  22. /// service, or set a default configuration for all methods by calling ``setDefaultConfig(_:)``.
  23. ///
  24. /// Use the subscript to get and set configurations for specific methods.
  25. package struct MethodConfigs: Sendable, Hashable {
  26. private var elements: [MethodConfig.Name: MethodConfig]
  27. /// Create a new ``_MethodConfigs``.
  28. ///
  29. /// - Parameter serviceConfig: The configuration to read ``MethodConfig`` from.
  30. package init(serviceConfig: ServiceConfig = ServiceConfig()) {
  31. self.elements = [:]
  32. for configuration in serviceConfig.methodConfig {
  33. for name in configuration.names {
  34. self.elements[name] = configuration
  35. }
  36. }
  37. }
  38. /// Get or set the corresponding ``MethodConfig`` for the given ``MethodDescriptor``.
  39. ///
  40. /// Configuration is hierarchical and can be set per-method, per-service
  41. /// (``setDefaultConfig(_:forService:)``) and globally (``setDefaultConfig(_:)``).
  42. /// This subscript sets the per-method configuration but retrieves a configuration respecting
  43. /// the hierarchy. If no per-method configuration is present, the per-service configuration is
  44. /// checked and returned if present. If the per-service configuration isn't present then the
  45. /// global configuration is returned, if present.
  46. ///
  47. /// - Parameters:
  48. /// - descriptor: The ``MethodDescriptor`` for which to get or set a ``MethodConfig``.
  49. package subscript(_ descriptor: MethodDescriptor) -> MethodConfig? {
  50. get {
  51. var name = MethodConfig.Name(
  52. service: descriptor.service.fullyQualifiedService,
  53. method: descriptor.method
  54. )
  55. if let configuration = self.elements[name] {
  56. return configuration
  57. }
  58. // Check if the config is set at the service level by clearing the method.
  59. name.method = ""
  60. if let configuration = self.elements[name] {
  61. return configuration
  62. }
  63. // Check if the config is set at the global level by clearing the service and method.
  64. name.service = ""
  65. return self.elements[name]
  66. }
  67. set {
  68. let name = MethodConfig.Name(
  69. service: descriptor.service.fullyQualifiedService,
  70. method: descriptor.method
  71. )
  72. self.elements[name] = newValue
  73. }
  74. }
  75. /// Set a default configuration for all methods that have no overrides.
  76. ///
  77. /// - Parameter config: The default configuration.
  78. package mutating func setDefaultConfig(_ config: MethodConfig?) {
  79. let name = MethodConfig.Name(service: "", method: "")
  80. self.elements[name] = config
  81. }
  82. /// Set a default configuration for a service.
  83. ///
  84. /// If getting a configuration for a method that's part of a service, and the method itself doesn't have an
  85. /// override, then this configuration will be used instead of the default configuration passed when creating
  86. /// this instance of ``MethodConfigs``.
  87. ///
  88. /// - Parameters:
  89. /// - config: The default configuration for the service.
  90. /// - service: The name of the service for which this override applies.
  91. package mutating func setDefaultConfig(
  92. _ config: MethodConfig?,
  93. forService service: String
  94. ) {
  95. let name = MethodConfig.Name(service: "", method: "")
  96. self.elements[name] = config
  97. }
  98. }