ConditionalInterceptor.swift 4.3 KB

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