ConditionalInterceptor.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. /// Describes the conditions under which an interceptor should be applied.
  17. ///
  18. /// You can configure interceptors to be applied to:
  19. /// - all RPCs and services;
  20. /// - requests directed only to specific services; or
  21. /// - requests directed only to specific methods (of a specific service).
  22. ///
  23. /// - SeeAlso: ``ClientInterceptor`` and ``ServerInterceptor`` for more information on client and
  24. /// server interceptors, respectively.
  25. public struct ConditionalInterceptor<Interceptor: Sendable>: Sendable {
  26. public struct Subject: Sendable {
  27. internal enum Wrapped: Sendable {
  28. case all
  29. case services(Set<ServiceDescriptor>)
  30. case methods(Set<MethodDescriptor>)
  31. }
  32. private let wrapped: Wrapped
  33. /// An operation subject specifying an interceptor that applies to all RPCs across all services will be registered with this client.
  34. public static var all: Self { .init(wrapped: .all) }
  35. /// An operation subject specifying an interceptor that will be applied only to RPCs directed to the specified services.
  36. /// - Parameters:
  37. /// - services: The list of service names for which this interceptor should intercept RPCs.
  38. public static func services(_ services: Set<ServiceDescriptor>) -> Self {
  39. Self(wrapped: .services(services))
  40. }
  41. /// An operation subject specifying an interceptor that will be applied only to RPCs directed to the specified service methods.
  42. /// - Parameters:
  43. /// - methods: The list of method descriptors for which this interceptor should intercept RPCs.
  44. public static func methods(_ methods: Set<MethodDescriptor>) -> Self {
  45. Self(wrapped: .methods(methods))
  46. }
  47. @usableFromInline
  48. package func applies(to descriptor: MethodDescriptor) -> Bool {
  49. switch self.wrapped {
  50. case .all:
  51. return true
  52. case .services(let services):
  53. return services.contains(descriptor.service)
  54. case .methods(let methods):
  55. return methods.contains(descriptor)
  56. }
  57. }
  58. }
  59. /// The interceptor.
  60. public let interceptor: Interceptor
  61. @usableFromInline
  62. internal let subject: Subject
  63. fileprivate init(interceptor: Interceptor, subject: Subject) {
  64. self.interceptor = interceptor
  65. self.subject = subject
  66. }
  67. /// Returns whether this ``ClientInterceptorPipelineOperation`` applies to the given `descriptor`.
  68. /// - Parameter descriptor: A ``MethodDescriptor`` for which to test whether this interceptor applies.
  69. /// - Returns: `true` if this interceptor applies to the given `descriptor`, or `false` otherwise.
  70. @inlinable
  71. internal func applies(to descriptor: MethodDescriptor) -> Bool {
  72. self.subject.applies(to: descriptor)
  73. }
  74. }
  75. extension ConditionalInterceptor where Interceptor == any ClientInterceptor {
  76. /// Create an operation, specifying which ``ClientInterceptor`` to apply and to which ``Subject``.
  77. /// - Parameters:
  78. /// - interceptor: The ``ClientInterceptor`` to register with the client.
  79. /// - subject: The ``Subject`` to which the `interceptor` applies.
  80. public static func apply(
  81. _ interceptor: any ClientInterceptor,
  82. to subject: Subject
  83. ) -> Self {
  84. Self(interceptor: interceptor, subject: subject)
  85. }
  86. }
  87. extension ConditionalInterceptor where Interceptor == any ServerInterceptor {
  88. /// Create an operation, specifying which ``ServerInterceptor`` to apply and to which ``Subject``.
  89. /// - Parameters:
  90. /// - interceptor: The ``ServerInterceptor`` to register with the server.
  91. /// - subject: The ``Subject`` to which the `interceptor` applies.
  92. public static func apply(
  93. _ interceptor: any ServerInterceptor,
  94. to subject: Subject
  95. ) -> Self {
  96. Self(interceptor: interceptor, subject: subject)
  97. }
  98. }