Skip to content

Replace Lock with NIOLock #628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ let package = Package(
.library(name: "AsyncHTTPClient", targets: ["AsyncHTTPClient"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-nio.git", from: "2.41.1"),
.package(url: "https://github.com/apple/swift-nio.git", from: "2.42.0"),
.package(url: "https://github.com/apple/swift-nio-ssl.git", from: "2.22.0"),
.package(url: "https://github.com/apple/swift-nio-http2.git", from: "1.19.0"),
.package(url: "https://github.com/apple/swift-nio-extras.git", from: "1.13.0"),
Expand Down
2 changes: 1 addition & 1 deletion Sources/AsyncHTTPClient/AsyncAwait/Transaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class Transaction: @unchecked Sendable {
let preferredEventLoop: EventLoop
let requestOptions: RequestOptions

private let stateLock = Lock()
private let stateLock = NIOLock()
private var state: StateMachine

init(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ extension HTTPConnectionPool {

private var state: State = .active
private var _pools: [Key: HTTPConnectionPool] = [:]
private let lock = Lock()
private let lock = NIOLock()

private let sslContextCache = SSLContextCache()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protocol HTTPConnectionPoolDelegate {
}

final class HTTPConnectionPool {
private let stateLock = Lock()
private let stateLock = NIOLock()
private var _state: StateMachine
/// The connection idle timeout timers. Protected by the stateLock
private var _idleTimer = [Connection.ID: Scheduled<Void>]()
Expand Down
8 changes: 4 additions & 4 deletions Sources/AsyncHTTPClient/HTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ public class HTTPClient {

/// Shared thread pool used for file IO. It is lazily created on first access of ``Task/fileIOThreadPool``.
private var fileIOThreadPool: NIOThreadPool?
private let fileIOThreadPoolLock = Lock()
private let fileIOThreadPoolLock = NIOLock()

private var state: State
private let stateLock = Lock()
private let stateLock = NIOLock()

internal static let loggingDisabled = Logger(label: "AHC-do-not-log", factory: { _ in SwiftLogNoOpLogHandler() })

Expand Down Expand Up @@ -169,7 +169,7 @@ public class HTTPClient {
Current eventLoop: \(eventLoop)
""")
}
let errorStorageLock = Lock()
let errorStorageLock = NIOLock()
let errorStorage: UnsafeMutableTransferBox<Error?> = .init(nil)
let continuation = DispatchWorkItem {}
self.shutdown(requiresCleanClose: requiresCleanClose, queue: DispatchQueue(label: "async-http-client.shutdown")) { error in
Expand Down Expand Up @@ -256,7 +256,7 @@ public class HTTPClient {
}

private func shutdownFileIOThreadPool(queue: DispatchQueue, _ callback: @escaping ShutdownCallback) {
self.fileIOThreadPoolLock.withLockVoid {
self.fileIOThreadPoolLock.withLock {
guard let fileIOThreadPool = fileIOThreadPool else {
callback(nil)
return
Expand Down
2 changes: 1 addition & 1 deletion Sources/AsyncHTTPClient/HTTPHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ extension HTTPClient {

private var _isCancelled: Bool = false
private var _taskDelegate: HTTPClientTaskDelegate?
private let lock = Lock()
private let lock = NIOLock()
private let makeOrGetFileIOThreadPool: () -> NIOThreadPool

/// The shared thread pool of a ``HTTPClient`` used for file IO. It is lazily created on first access.
Expand Down
2 changes: 1 addition & 1 deletion Sources/AsyncHTTPClient/SSLContextCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import NIOCore
import NIOSSL

final class SSLContextCache {
private let lock = Lock()
private let lock = NIOLock()
private var sslContextCache = LRUCache<BestEffortHashableTLSConfiguration, NIOSSLContext>()
private let offloadQueue = DispatchQueue(label: "io.github.swift-server.AsyncHTTPClient.SSLContextCache")
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/AsyncHTTPClientTests/AsyncTestHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ final class AsyncSequenceWriter<Element>: AsyncSequence, @unchecked Sendable {
}

private var _state = State.buffering(.init(), nil)
private let lock = Lock()
private let lock = NIOLock()

public var hasDemand: Bool {
self.lock.withLock {
Expand Down
14 changes: 7 additions & 7 deletions Tests/AsyncHTTPClientTests/HTTP1ConnectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -595,12 +595,12 @@ class HTTP1ConnectionTests: XCTestCase {
var _reads = 0
var _channel: Channel?

let lock: Lock
let lock: NIOLock
let backpressurePromise: EventLoopPromise<Void>
let messageReceived: EventLoopPromise<Void>

init(eventLoop: EventLoop) {
self.lock = Lock()
self.lock = NIOLock()
self.backpressurePromise = eventLoop.makePromise()
self.messageReceived = eventLoop.makePromise()
}
Expand All @@ -612,7 +612,7 @@ class HTTP1ConnectionTests: XCTestCase {
}

func willExecuteOnChannel(_ channel: Channel) {
self.lock.withLockVoid {
self.lock.withLock {
self._channel = channel
}
}
Expand All @@ -623,7 +623,7 @@ class HTTP1ConnectionTests: XCTestCase {

func didReceiveBodyPart(task: HTTPClient.Task<Response>, _ buffer: ByteBuffer) -> EventLoopFuture<Void> {
// We count a number of reads received.
self.lock.withLockVoid {
self.lock.withLock {
self._reads += 1
}
// We need to notify the test when first byte of the message is arrived.
Expand Down Expand Up @@ -805,7 +805,7 @@ class AfterRequestCloseConnectionChannelHandler: ChannelInboundHandler {
}

class MockConnectionDelegate: HTTP1ConnectionDelegate {
private var lock = Lock()
private var lock = NIOLock()

private var _hitConnectionReleased = 0
private var _hitConnectionClosed = 0
Expand All @@ -821,13 +821,13 @@ class MockConnectionDelegate: HTTP1ConnectionDelegate {
init() {}

func http1ConnectionReleased(_: HTTP1Connection) {
self.lock.withLockVoid {
self.lock.withLock {
self._hitConnectionReleased += 1
}
}

func http1ConnectionClosed(_: HTTP1Connection) {
self.lock.withLockVoid {
self.lock.withLock {
self._hitConnectionClosed += 1
}
}
Expand Down
10 changes: 5 additions & 5 deletions Tests/AsyncHTTPClientTests/HTTP2ConnectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ class TestConnectionCreator {
}

private var state: State = .idle
private let lock = Lock()
private let lock = NIOLock()

init() {}

Expand Down Expand Up @@ -428,7 +428,7 @@ class TestHTTP2ConnectionDelegate: HTTP2ConnectionDelegate {
self.lock.withLock { self._maxStreamSetting }
}

private let lock = Lock()
private let lock = NIOLock()
private var _hitStreamClosed: Int = 0
private var _hitGoAwayReceived: Int = 0
private var _hitConnectionClosed: Int = 0
Expand All @@ -439,19 +439,19 @@ class TestHTTP2ConnectionDelegate: HTTP2ConnectionDelegate {
func http2Connection(_: HTTP2Connection, newMaxStreamSetting: Int) {}

func http2ConnectionStreamClosed(_: HTTP2Connection, availableStreams: Int) {
self.lock.withLockVoid {
self.lock.withLock {
self._hitStreamClosed += 1
}
}

func http2ConnectionGoAwayReceived(_: HTTP2Connection) {
self.lock.withLockVoid {
self.lock.withLock {
self._hitGoAwayReceived += 1
}
}

func http2ConnectionClosed(_: HTTP2Connection) {
self.lock.withLockVoid {
self.lock.withLock {
self._hitConnectionClosed += 1
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/AsyncHTTPClientTests/HTTPClientTestUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,7 @@ struct CollectEverythingLogHandler: LogHandler {
var metadata: [String: String]
}

var lock = Lock()
var lock = NIOLock()
var logs: [Entry] = []

var allEntries: [Entry] {
Expand Down