Skip to content

Commit

Permalink
Implement Sendable (#160)
Browse files Browse the repository at this point in the history
Make every non-channel class conform to Sendable (or @unchecked Sendable) to prepare for NIO.
  • Loading branch information
Davidde94 authored May 4, 2022
1 parent 7958631 commit e4dc3c8
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Sources/NIOExtras/PCAPRingBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public class NIOPCAPRingBuffer {
self.popFirst()
}
precondition(self.pcapFragments.count < self.maximumFragments)

// Add the new fragment
self.append(buffer)

Expand Down
11 changes: 11 additions & 0 deletions Sources/NIOExtras/QuiescingHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ private final class ChannelCollector {
case shuttingDown
case shutdownCompleted
}

private var openChannels: [ObjectIdentifier: Channel] = [:]
private let serverChannel: Channel
private var fullyShutdownPromise: EventLoopPromise<Void>? = nil
Expand Down Expand Up @@ -143,6 +144,12 @@ private final class ChannelCollector {
}
}

#if swift(>=5.5) && canImport(_Concurrency)
extension ChannelCollector: @unchecked Sendable {

}
#endif

/// A `ChannelHandler` that adds all channels that it receives through the `ChannelPipeline` to a `ChannelCollector`.
///
/// - note: This is only useful to be added to a server `Channel` in `ServerBootstrap.serverChannelInitializer`.
Expand Down Expand Up @@ -250,3 +257,7 @@ public final class ServerQuiescingHelper {
}
}
}

extension ServerQuiescingHelper: NIOSendable {

}
8 changes: 7 additions & 1 deletion Sources/NIOExtras/WritePCAPHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ extension NIOWritePCAPHandler {
/// A `SynchronizedFileSink` is thread-safe so can be used from any thread/`EventLoop`. After use, you
/// _must_ call `syncClose` on the `SynchronizedFileSink` to shut it and all the associated resources down. Failing
/// to do so triggers undefined behaviour.
public class SynchronizedFileSink {
public final class SynchronizedFileSink {
private let fileHandle: NIOFileHandle
private let workQueue: DispatchQueue
private let writesGroup = DispatchGroup()
Expand Down Expand Up @@ -715,3 +715,9 @@ extension NIOWritePCAPHandler {
}
}
}

#if swift(>=5.5) && canImport(_Concurrency)
extension NIOWritePCAPHandler.SynchronizedFileSink: @unchecked Sendable {

}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ class HTTP1ThreadedPerformanceTest: Benchmark {

let head: HTTPRequestHead

var group: MultiThreadedEventLoopGroup!
var serverChannel: Channel!

init(numberOfRepeats: Int,
numberOfClients: Int,
requestsPerClient: Int,
Expand All @@ -142,9 +145,6 @@ class HTTP1ThreadedPerformanceTest: Benchmark {
self.head = head
}

var group: MultiThreadedEventLoopGroup!
var serverChannel: Channel!

func setUp() throws {
self.group = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
self.serverChannel = try ServerBootstrap(group: self.group)
Expand Down
8 changes: 3 additions & 5 deletions Sources/NIOExtrasPerformanceTester/PCAPPerformanceTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,17 @@ import NIOEmbedded
import NIOExtras
import Foundation

class PCAPPerformanceTest: Benchmark {
final class PCAPPerformanceTest: Benchmark {
let numberOfRepeats: Int

let byteBuffer = ByteBuffer(repeating: 0x65, count: 1000)
let outputFile = NSTemporaryDirectory() + "/" + UUID().uuidString

init(numberOfRepeats: Int) {
self.numberOfRepeats = numberOfRepeats
}

var outputFile: String!

func setUp() throws {
self.outputFile = NSTemporaryDirectory() + "/" + UUID().uuidString

}

func tearDown() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import NIOCore
import NIOEmbedded
import NIOExtras

class RollingPCAPPerformanceTest: Benchmark {
final class RollingPCAPPerformanceTest: Benchmark {
let numberOfRepeats: Int

let byteBuffer = ByteBuffer(repeating: 0x65, count: 1000)
Expand Down

0 comments on commit e4dc3c8

Please sign in to comment.