Ver Fonte

Fix sendable warnings (#1513)

Motivation:

Warnings were emitted for `debugChannelInitializer`s as they were not
`Sendable` even though their definition via a typealias was.

Modifications:

Expand out typealiases and add appropriate `@Sendable` and
`@preconcurrency` annotations.

Results:

No warnings.
George Barnett há 3 anos atrás
pai
commit
a6e90574ec

+ 44 - 2
Sources/GRPC/ClientConnection.swift

@@ -454,9 +454,15 @@ extension ClientConnection {
     /// used to add additional handlers to the pipeline and is intended for debugging.
     ///
     /// - Warning: The initializer closure may be invoked *multiple times*.
-    public var debugChannelInitializer: GRPCChannelInitializer?
+    #if compiler(>=5.6)
+    @preconcurrency
+    public var debugChannelInitializer: (@Sendable (Channel) -> EventLoopFuture<Void>)?
+    #else
+    public var debugChannelInitializer: ((Channel) -> EventLoopFuture<Void>)?
+    #endif
 
     #if canImport(NIOSSL)
+    #if compiler(>=5.6)
     /// Create a `Configuration` with some pre-defined defaults. Prefer using
     /// `ClientConnection.secure(group:)` to build a connection secured with TLS or
     /// `ClientConnection.insecure(group:)` to build a plaintext connection.
@@ -480,6 +486,7 @@ extension ClientConnection {
     /// - Parameter debugChannelInitializer: A channel initializer will be called after gRPC has
     ///     initialized the channel. Defaults to `nil`.
     @available(*, deprecated, renamed: "default(target:eventLoopGroup:)")
+    @preconcurrency
     public init(
       target: ConnectionTarget,
       eventLoopGroup: EventLoopGroup,
@@ -496,7 +503,7 @@ extension ClientConnection {
         label: "io.grpc",
         factory: { _ in SwiftLogNoOpLogHandler() }
       ),
-      debugChannelInitializer: GRPCChannelInitializer? = nil
+      debugChannelInitializer: (@Sendable (Channel) -> EventLoopFuture<Void>)? = nil
     ) {
       self.target = target
       self.eventLoopGroup = eventLoopGroup
@@ -512,6 +519,41 @@ extension ClientConnection {
       self.backgroundActivityLogger = backgroundActivityLogger
       self.debugChannelInitializer = debugChannelInitializer
     }
+    #else
+    @available(*, deprecated, renamed: "default(target:eventLoopGroup:)")
+    public init(
+      target: ConnectionTarget,
+      eventLoopGroup: EventLoopGroup,
+      errorDelegate: ClientErrorDelegate? = LoggingClientErrorDelegate(),
+      connectivityStateDelegate: ConnectivityStateDelegate? = nil,
+      connectivityStateDelegateQueue: DispatchQueue? = nil,
+      tls: Configuration.TLS? = nil,
+      connectionBackoff: ConnectionBackoff? = ConnectionBackoff(),
+      connectionKeepalive: ClientConnectionKeepalive = ClientConnectionKeepalive(),
+      connectionIdleTimeout: TimeAmount = .minutes(30),
+      callStartBehavior: CallStartBehavior = .waitsForConnectivity,
+      httpTargetWindowSize: Int = 8 * 1024 * 1024,
+      backgroundActivityLogger: Logger = Logger(
+        label: "io.grpc",
+        factory: { _ in SwiftLogNoOpLogHandler() }
+      ),
+      debugChannelInitializer: ((Channel) -> EventLoopFuture<Void>)? = nil
+    ) {
+      self.target = target
+      self.eventLoopGroup = eventLoopGroup
+      self.errorDelegate = errorDelegate
+      self.connectivityStateDelegate = connectivityStateDelegate
+      self.connectivityStateDelegateQueue = connectivityStateDelegateQueue
+      self.tlsConfiguration = tls.map { GRPCTLSConfiguration(transforming: $0) }
+      self.connectionBackoff = connectionBackoff
+      self.connectionKeepalive = connectionKeepalive
+      self.connectionIdleTimeout = connectionIdleTimeout
+      self.callStartBehavior = callStartBehavior
+      self.httpTargetWindowSize = httpTargetWindowSize
+      self.backgroundActivityLogger = backgroundActivityLogger
+      self.debugChannelInitializer = debugChannelInitializer
+    }
+    #endif // compiler(>=5.6)
     #endif // canImport(NIOSSL)
 
     private init(eventLoopGroup: EventLoopGroup, target: ConnectionTarget) {

+ 6 - 1
Sources/GRPC/ConnectionPool/GRPCChannelPool.swift

@@ -167,7 +167,12 @@ extension GRPCChannelPool {
     /// This may be used to add additional handlers to the pipeline and is intended for debugging.
     ///
     /// - Warning: The initializer closure may be invoked *multiple times*.
-    public var debugChannelInitializer: GRPCChannelInitializer?
+    #if compiler(>=5.6)
+    @preconcurrency
+    public var debugChannelInitializer: (@Sendable (Channel) -> EventLoopFuture<Void>)?
+    #else
+    public var debugChannelInitializer: ((Channel) -> EventLoopFuture<Void>)?
+    #endif
 
     /// An error delegate which is called when errors are caught.
     public var errorDelegate: ClientErrorDelegate?

+ 12 - 1
Sources/GRPC/GRPCChannel/GRPCChannelBuilder.swift

@@ -315,13 +315,24 @@ extension ClientConnection.Builder {
   /// used to add additional handlers to the pipeline and is intended for debugging.
   ///
   /// - Warning: The initializer closure may be invoked *multiple times*.
+  #if compiler(>=5.6)
   @discardableResult
+  @preconcurrency
   public func withDebugChannelInitializer(
-    _ debugChannelInitializer: @escaping GRPCChannelInitializer
+    _ debugChannelInitializer: @Sendable @escaping (Channel) -> EventLoopFuture<Void>
   ) -> Self {
     self.configuration.debugChannelInitializer = debugChannelInitializer
     return self
   }
+  #else
+  @discardableResult
+  public func withDebugChannelInitializer(
+    _ debugChannelInitializer: @escaping (Channel) -> EventLoopFuture<Void>
+  ) -> Self {
+    self.configuration.debugChannelInitializer = debugChannelInitializer
+    return self
+  }
+  #endif
 }
 
 extension Double {