-
Notifications
You must be signed in to change notification settings - Fork 656
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
254 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftNIO open source project | ||
// | ||
// Copyright (c) 2023 Apple Inc. and the SwiftNIO project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#if canImport(Darwin) | ||
import Darwin | ||
#elseif os(Windows) | ||
import ucrt | ||
import WinSDK | ||
#elseif canImport(Glibc) | ||
import Glibc | ||
#elseif canImport(Musl) | ||
import Musl | ||
#else | ||
#error("The concurrency NIOLock module was unable to identify your C library.") | ||
#endif | ||
import NIOCore | ||
|
||
/// Singleton resources provided by SwiftNIO for programs & libraries that don't need full control over all operating system resources. | ||
/// | ||
/// SwiftNIO allows and encourages the precise management of all operating system resources such as threads and file descriptors. | ||
/// Certain resources (such as the main `EventLoopGroup`) however are usually globally shared across the program. This means | ||
/// that many programs have to carry around an `EventLoopGroup` despite the fact the don't require the ability to fully return | ||
/// all the operating resources which would imply shutting down the `EventLoopGroup`. This type is the global handle for singleton | ||
/// resources that applications (and some libraries) can use to obtain never-shut-down singleton resources. | ||
/// | ||
/// Programs and libraries that do not use these singletons will not incur extra resource usage, these resources are lazily initialized on | ||
/// first use. | ||
public enum NIOSingletons {} | ||
|
||
extension NIOSingletons { | ||
/// How many threads will be used for the multi-threaded `EventLoopGroup`s. | ||
/// | ||
/// Uses `NIO_SINGLETONS_MULTI_THREADED_ELG_THREAD_COUNT`. | ||
public static var suggestedMultiThreadedEventLoopGroupThreadCount: Int { | ||
return globalSuggestedMultiThreadedEventLoopGroupThreadCount | ||
} | ||
|
||
/// How many threads will be used for the blocking `NIOThreadPool`s. | ||
/// | ||
/// Uses `NIO_SINGLETONS_BLOCKING_POOL_THREAD_COUNT`. | ||
public static var suggestedBlockingPoolThreadCount: Int { | ||
return globalSuggestedBlockingPoolThreadCount | ||
} | ||
} | ||
|
||
private let globalSuggestedMultiThreadedEventLoopGroupThreadCount: Int = { | ||
if let threadCountEnv = getenv("NIO_SINGLETONS_MULTI_THREADED_ELG_THREAD_COUNT"), | ||
let threadCount = Int(String(cString: threadCountEnv)), threadCount > 0 { | ||
return threadCount | ||
} else { | ||
return System.coreCount | ||
} | ||
}() | ||
|
||
private let globalSuggestedBlockingPoolThreadCount: Int = { | ||
if let threadCountEnv = getenv("NIO_SINGLETONS_BLOCKING_POOL_THREAD_COUNT"), | ||
let threadCount = Int(String(cString: threadCountEnv)), threadCount > 0 { | ||
return threadCount | ||
} else { | ||
return System.coreCount * 2 | ||
} | ||
}() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftNIO open source project | ||
// | ||
// Copyright (c) 2023 Apple Inc. and the SwiftNIO project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
@_exported import NIOSingletonResources | ||
import NIOPosix | ||
import NIOCore | ||
|
||
extension NIOSingletons { | ||
/// A globally shared, lazily intialized EventLoopGroup that uses `epll` as the selector mechanism. | ||
public static var multiThreadedPosixEventLoopGroup: MultiThreadedEventLoopGroup { | ||
return globalMultiThreadedPosixEventLoopGroup | ||
} | ||
|
||
/// A globally shared, lazily intialized EventLoopGroup with just one thread that uses `epoll` as the selector mechanism. | ||
public static var singleThreadedPosixEventLoopGroup: MultiThreadedEventLoopGroup { | ||
return globalSingleThreadedPosixEventLoopGroup | ||
} | ||
|
||
/// A globally shared, lazily intialized EventLoop that uses `epoll` as the selector mechanism. Backed by `NIOSingletons.singleThreadedPosixEventLoopGroup`. | ||
public static var posixEventLoop: EventLoop { | ||
return Self.singleThreadedPosixEventLoopGroup.next() | ||
} | ||
|
||
/// A globally shared, lazily intialized `NIOThreadPool` that can be used for blocking I/O and other blocking operations. | ||
public static var posixBlockingPool: NIOThreadPool { | ||
return globalPosixBlockingPool | ||
} | ||
} | ||
|
||
private let globalMultiThreadedPosixEventLoopGroup: MultiThreadedEventLoopGroup = { | ||
let group = MultiThreadedEventLoopGroup(_canBeShutDown: false, | ||
numberOfThreads: NIOSingletons.suggestedMultiThreadedEventLoopGroupThreadCount) | ||
_ = Unmanaged.passUnretained(group).retain() // Never gonna give you up, | ||
return group | ||
}() | ||
|
||
private let globalSingleThreadedPosixEventLoopGroup: MultiThreadedEventLoopGroup = { | ||
let group = MultiThreadedEventLoopGroup(_canBeShutDown: false, numberOfThreads: 1) | ||
_ = Unmanaged.passUnretained(group).retain() // never gonna let you down. | ||
return group | ||
}() | ||
|
||
private let globalPosixBlockingPool: NIOThreadPool = { | ||
let pool = NIOThreadPool(numberOfThreads: NIOSingletons.suggestedBlockingPoolThreadCount) | ||
pool.start() | ||
_ = Unmanaged.passUnretained(pool).retain() // Make sure this is never deallocated (not strictly necessary). | ||
return pool | ||
}() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftNIO open source project | ||
// | ||
// Copyright (c) 2023 Apple Inc. and the SwiftNIO project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import XCTest | ||
import NIOSingletonResources | ||
import NIOSingletonsPosix | ||
import NIOCore | ||
|
||
final class NIOSingletonsTests: XCTestCase { | ||
func testSingleThreadedPosixGroupHasOneThread() { | ||
XCTAssertEqual(1, Array(NIOSingletons.singleThreadedPosixEventLoopGroup.makeIterator()).count) | ||
} | ||
|
||
func testSingleThreadedAndMultiThreadedPosixGroupsAreDifferent() { | ||
XCTAssert(NIOSingletons.singleThreadedPosixEventLoopGroup !== NIOSingletons.multiThreadedPosixEventLoopGroup) | ||
} | ||
|
||
func testSingletonEventLoopWorks() async throws { | ||
let works = try await NIOSingletons.posixEventLoop.submit { "yes" }.get() | ||
XCTAssertEqual(works, "yes") | ||
} | ||
|
||
func testSingletonMultiThreadedEventLoopWorks() async throws { | ||
let works = try await NIOSingletons.multiThreadedPosixEventLoopGroup.any().submit { "yes" }.get() | ||
XCTAssertEqual(works, "yes") | ||
} | ||
|
||
func testSingletonSingleThreadedEventLoopWorks() async throws { | ||
let works = try await NIOSingletons.singleThreadedPosixEventLoopGroup.any().submit { "yes" }.get() | ||
XCTAssertEqual(works, "yes") | ||
} | ||
|
||
func testSingletonBlockingPoolWorks() async throws { | ||
let works = try await NIOSingletons.posixBlockingPool.runIfActive(eventLoop: NIOSingletons.posixEventLoop) { | ||
"yes" | ||
}.get() | ||
XCTAssertEqual(works, "yes") | ||
} | ||
|
||
func testCannotShutdownMultiGroup() { | ||
XCTAssertThrowsError(try NIOSingletons.multiThreadedPosixEventLoopGroup.syncShutdownGracefully()) { error in | ||
XCTAssertEqual(.unsupportedOperation, error as? EventLoopError) | ||
} | ||
} | ||
|
||
func testCannotShutdownSingleGroup() { | ||
XCTAssertThrowsError(try NIOSingletons.singleThreadedPosixEventLoopGroup.syncShutdownGracefully()) { error in | ||
XCTAssertEqual(.unsupportedOperation, error as? EventLoopError) | ||
} | ||
} | ||
|
||
func testCannotShutdownSingleLoop() { | ||
XCTAssertThrowsError(try NIOSingletons.posixEventLoop.syncShutdownGracefully()) { error in | ||
XCTAssertEqual(.unsupportedOperation, error as? EventLoopError) | ||
} | ||
} | ||
} |