Skip to content

Commit

Permalink
s/globalSingleton/singleton/g
Browse files Browse the repository at this point in the history
  • Loading branch information
weissi committed Jul 26, 2023
1 parent e5a7fe5 commit 1cd1d4e
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 70 deletions.
20 changes: 10 additions & 10 deletions Sources/NIOCore/GlobalSingletons.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ import Musl
/// system resources. This type holds sizing (how many loops/threads) suggestions.
///
/// Users who need very tight control about the exact threads and resources created may decide to set
/// `NIOGlobalSingletons.globalSingletonsEnabledSuggestion = false`. All singleton-creating facilities should check
/// `NIOSingletons.singletonsEnabledSuggestion = false`. All singleton-creating facilities should check
/// this setting and if `false` restrain from creating any global singleton resources. Please note that disabling the
/// global singletons will lead to a crash if _any_ code attempts to use any of the singletons.
public enum NIOGlobalSingletons {
public enum NIOSingletons {
}

extension NIOGlobalSingletons {
extension NIOSingletons {
/// A suggestion of how many ``EventLoop``s the global singleton ``EventLoopGroup``s are supposed to consist of.
///
/// The thread count is ``System/coreCount`` unless the environment variable `NIO_SINGLETON_GROUP_LOOP_COUNT`
/// is set or this value was set manually by the user.
///
/// - note: This value must be set _before_ any singletons are used and must only be set once.
public static var suggestedGlobalSingletonGroupLoopCount: Int {
public static var groupLoopCountSuggestion: Int {
set {
Self.userSetSingletonThreadCount(rawStorage: globalRawSuggestedLoopCount, userValue: newValue)
}
Expand All @@ -61,7 +61,7 @@ extension NIOGlobalSingletons {
/// `NIO_SINGLETON_BLOCKING_POOL_THREAD_COUNT` is set or this value was set manually by the user.
///
/// - note: This value must be set _before_ any singletons are used and must only be set once.
public static var suggestedBlockingPoolThreadCount: Int {
public static var blockingPoolThreadCountSuggestion: Int {
set {
Self.userSetSingletonThreadCount(rawStorage: globalRawSuggestedBlockingThreadCount, userValue: newValue)
}
Expand All @@ -77,7 +77,7 @@ extension NIOGlobalSingletons {
/// This value cannot be changed using an environment variable.
///
/// - note: This value must be set _before_ any singletons are used and must only be set once.
public static var globalSingletonsEnabledSuggestion: Bool {
public static var singletonsEnabledSuggestion: Bool {
get {
let (exchanged, original) = globalRawSingletonsEnabled.compareExchange(expected: 0,
desired: 1,
Expand All @@ -96,9 +96,9 @@ extension NIOGlobalSingletons {

set {
let intRepresentation = newValue ? 1 : -1
let (exchanged, original) = globalRawSingletonsEnabled.compareExchange(expected: 0,
desired: intRepresentation,
ordering: .relaxed)
let (exchanged, _) = globalRawSingletonsEnabled.compareExchange(expected: 0,
desired: intRepresentation,
ordering: .relaxed)
guard exchanged else {
fatalError("""
Bug in user code: Global singleton enabled suggestion has been changed after \
Expand All @@ -115,7 +115,7 @@ private let globalRawSuggestedLoopCount = ManagedAtomic(0)
private let globalRawSuggestedBlockingThreadCount = ManagedAtomic(0)
private let globalRawSingletonsEnabled = ManagedAtomic(0)

extension NIOGlobalSingletons {
extension NIOSingletons {
private static func userSetSingletonThreadCount(rawStorage: ManagedAtomic<Int>, userValue: Int) {
precondition(userValue > 0, "illegal value: needs to be strictly positive")

Expand Down
44 changes: 22 additions & 22 deletions Sources/NIOCrashTester/CrashTests+EventLoop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ struct EventLoopCrashTests {
let testUsingTheSingletonGroupWhenDisabled = CrashTest(
regex: #"Fatal error: Cannot create global singleton MultiThreadedEventLoopGroup because the global singletons"#
) {
NIOGlobalSingletons.globalSingletonsEnabledSuggestion = false
try? NIOGlobalSingletons.posixEventLoopGroup.next().submit {}.wait()
NIOSingletons.singletonsEnabledSuggestion = false
try? NIOSingletons.posixEventLoopGroup.next().submit {}.wait()
}

let testUsingTheSingletonBlockingPoolWhenDisabled = CrashTest(
Expand All @@ -120,62 +120,62 @@ struct EventLoopCrashTests {
defer {
try? group.syncShutdownGracefully()
}
NIOGlobalSingletons.globalSingletonsEnabledSuggestion = false
try? NIOGlobalSingletons.posixBlockingThreadPool.runIfActive(eventLoop: group.next(), {}).wait()
NIOSingletons.singletonsEnabledSuggestion = false
try? NIOSingletons.posixBlockingThreadPool.runIfActive(eventLoop: group.next(), {}).wait()
}

let testDisablingGlobalSingletonsEnabledValueTwice = CrashTest(
let testDisablingSingletonsEnabledValueTwice = CrashTest(
regex: #"Fatal error: Bug in user code: Global singleton enabled suggestion has been changed after"#
) {
NIOGlobalSingletons.globalSingletonsEnabledSuggestion = false
NIOGlobalSingletons.globalSingletonsEnabledSuggestion = false
NIOSingletons.singletonsEnabledSuggestion = false
NIOSingletons.singletonsEnabledSuggestion = false
}

let testEnablingGlobalSingletonsEnabledValueTwice = CrashTest(
let testEnablingSingletonsEnabledValueTwice = CrashTest(
regex: #"Fatal error: Bug in user code: Global singleton enabled suggestion has been changed after"#
) {
NIOGlobalSingletons.globalSingletonsEnabledSuggestion = true
NIOGlobalSingletons.globalSingletonsEnabledSuggestion = true
NIOSingletons.singletonsEnabledSuggestion = true
NIOSingletons.singletonsEnabledSuggestion = true
}

let testEnablingThenDisablingGlobalSingletonsEnabledValue = CrashTest(
let testEnablingThenDisablingSingletonsEnabledValue = CrashTest(
regex: #"Fatal error: Bug in user code: Global singleton enabled suggestion has been changed after"#
) {
NIOGlobalSingletons.globalSingletonsEnabledSuggestion = true
NIOGlobalSingletons.globalSingletonsEnabledSuggestion = false
NIOSingletons.singletonsEnabledSuggestion = true
NIOSingletons.singletonsEnabledSuggestion = false
}

let testSettingTheGlobalSingletonEnabledValueAfterUse = CrashTest(
let testSettingTheSingletonEnabledValueAfterUse = CrashTest(
regex: #"Fatal error: Bug in user code: Global singleton enabled suggestion has been changed after"#
) {
try? MultiThreadedEventLoopGroup.globalSingleton.next().submit({}).wait()
NIOGlobalSingletons.globalSingletonsEnabledSuggestion = true
try? MultiThreadedEventLoopGroup.singleton.next().submit({}).wait()
NIOSingletons.singletonsEnabledSuggestion = true
}

let testSettingTheSuggestedSingletonGroupCountTwice = CrashTest(
regex: #"Fatal error: Bug in user code: Global singleton suggested loop/thread count has been changed after"#
) {
NIOGlobalSingletons.suggestedGlobalSingletonGroupLoopCount = 17
NIOGlobalSingletons.suggestedGlobalSingletonGroupLoopCount = 17
NIOSingletons.groupLoopCountSuggestion = 17
NIOSingletons.groupLoopCountSuggestion = 17
}

let testSettingTheSuggestedSingletonGroupChangeAfterUse = CrashTest(
regex: #"Fatal error: Bug in user code: Global singleton suggested loop/thread count has been changed after"#
) {
try? MultiThreadedEventLoopGroup.globalSingleton.next().submit({}).wait()
NIOGlobalSingletons.suggestedGlobalSingletonGroupLoopCount = 17
try? MultiThreadedEventLoopGroup.singleton.next().submit({}).wait()
NIOSingletons.groupLoopCountSuggestion = 17
}

let testSettingTheSuggestedSingletonGroupLoopCountToZero = CrashTest(
regex: #"Precondition failed: illegal value: needs to be strictly positive"#
) {
NIOGlobalSingletons.suggestedGlobalSingletonGroupLoopCount = 0
NIOSingletons.groupLoopCountSuggestion = 0
}

let testSettingTheSuggestedSingletonGroupLoopCountToANegativeValue = CrashTest(
regex: #"Precondition failed: illegal value: needs to be strictly positive"#
) {
NIOGlobalSingletons.suggestedGlobalSingletonGroupLoopCount = -1
NIOSingletons.groupLoopCountSuggestion = -1
}
}
#endif
6 changes: 3 additions & 3 deletions Sources/NIOHTTP1Server/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,8 @@ func childChannelInitializer(channel: Channel) -> EventLoopFuture<Void> {
}
}

let fileIO = NonBlockingFileIO(threadPool: .globalSingleton)
let socketBootstrap = ServerBootstrap(group: MultiThreadedEventLoopGroup.globalSingleton)
let fileIO = NonBlockingFileIO(threadPool: .singleton)
let socketBootstrap = ServerBootstrap(group: MultiThreadedEventLoopGroup.singleton)
// Specify backlog and enable SO_REUSEADDR for the server itself
.serverChannelOption(ChannelOptions.backlog, value: 256)
.serverChannelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1)
Expand All @@ -533,7 +533,7 @@ let socketBootstrap = ServerBootstrap(group: MultiThreadedEventLoopGroup.globalS
.childChannelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1)
.childChannelOption(ChannelOptions.maxMessagesPerRead, value: 1)
.childChannelOption(ChannelOptions.allowRemoteHalfClosure, value: allowHalfClosure)
let pipeBootstrap = NIOPipeBootstrap(group: MultiThreadedEventLoopGroup.globalSingleton)
let pipeBootstrap = NIOPipeBootstrap(group: MultiThreadedEventLoopGroup.singleton)
// Set the handlers that are applied to the accepted Channels
.channelInitializer(childChannelInitializer(channel:))

Expand Down
44 changes: 22 additions & 22 deletions Sources/NIOPosix/PosixSingletons.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@

import NIOCore

extension NIOGlobalSingletons {
extension NIOSingletons {
/// A globally shared, lazily initialized ``MultiThreadedEventLoopGroup`` that uses `epoll`/`kqueue` as the
/// selector mechanism.
///
/// The number of threads is determined by ``NIOGlobalSingletons/suggestedGlobalSingletonGroupLoopCount``.
/// The number of threads is determined by ``NIOSingletons/groupLoopCountSuggestion``.
public static var posixEventLoopGroup: MultiThreadedEventLoopGroup {
return globalSingletonMTELG
return singletonMTELG
}

/// A globally shared, lazily initialized ``NIOThreadPool`` that can be used for blocking I/O and other blocking operations.
///
/// The number of threads is determined by ``NIOGlobalSingletons/suggestedBlockingPoolThreadCount``.
/// The number of threads is determined by ``NIOSingletons/blockingPoolThreadCountSuggestion``.
public static var posixBlockingThreadPool: NIOThreadPool {
return globalPosixBlockingPool
}
Expand All @@ -43,23 +43,23 @@ extension MultiThreadedEventLoopGroup {
/// Programs and libraries that do not use these singletons will not incur extra resource usage, these resources are lazily initialized on
/// first use.
///
/// The loop count of this group is determined by ``NIOGlobalSingletons/suggestedGlobalSingletonGroupLoopCount``.
/// The loop count of this group is determined by ``NIOSingletons/groupLoopCountSuggestion``.
///
/// - note: Users who do not want any code to spawn global singleton resources may set
/// ``NIOGlobalSingletons/globalSingletonsEnabledSuggestion`` to `false` which will lead to a forced crash
/// ``NIOSingletons/singletonsEnabledSuggestion`` to `false` which will lead to a forced crash
/// if any code attempts to use the global singletons.
///
public static var globalSingleton: MultiThreadedEventLoopGroup {
return NIOGlobalSingletons.posixEventLoopGroup
public static var singleton: MultiThreadedEventLoopGroup {
return NIOSingletons.posixEventLoopGroup
}
}

extension EventLoopGroup where Self == MultiThreadedEventLoopGroup {
/// A globally shared, singleton ``MultiThreadedEventLoopGroup``.
///
/// This provides the same object as ``MultiThreadedEventLoopGroup/globalSingletonMultiThreadedEventLoopGroup``.
public static var globalSingletonMultiThreadedEventLoopGroup: Self {
return MultiThreadedEventLoopGroup.globalSingleton
/// This provides the same object as ``MultiThreadedEventLoopGroup/singleton``.
public static var singletonMultiThreadedEventLoopGroup: Self {
return MultiThreadedEventLoopGroup.singleton
}
}

Expand All @@ -75,39 +75,39 @@ extension NIOThreadPool {
/// Programs and libraries that do not use these singletons will not incur extra resource usage, these resources are lazily initialized on
/// first use.
///
/// The thread count of this pool is determined by ``NIOGlobalSingletons/suggestedBlockingPoolThreadCount``.
/// The thread count of this pool is determined by ``NIOSingletons/suggestedBlockingPoolThreadCount``.
///
/// - note: Users who do not want any code to spawn global singleton resources may set
/// ``NIOGlobalSingletons/globalSingletonsEnabledSuggestion`` to `false` which will lead to a forced crash
/// ``NIOSingletons/singletonsEnabledSuggestion`` to `false` which will lead to a forced crash
/// if any code attempts to use the global singletons.
public static var globalSingleton: NIOThreadPool {
return NIOGlobalSingletons.posixBlockingThreadPool
public static var singleton: NIOThreadPool {
return NIOSingletons.posixBlockingThreadPool
}
}

private let globalSingletonMTELG: MultiThreadedEventLoopGroup = {
guard NIOGlobalSingletons.globalSingletonsEnabledSuggestion else {
private let singletonMTELG: MultiThreadedEventLoopGroup = {
guard NIOSingletons.singletonsEnabledSuggestion else {
fatalError("""
Cannot create global singleton MultiThreadedEventLoopGroup because the global singletons have been \
disabled by setting `NIOGlobalSingletons.globalSingletonsEnabledSuggestion = false`
disabled by setting `NIOSingletons.singletonsEnabledSuggestion = false`
""")
}
let threadCount = NIOGlobalSingletons.suggestedGlobalSingletonGroupLoopCount
let threadCount = NIOSingletons.groupLoopCountSuggestion
let group = MultiThreadedEventLoopGroup._makePerpetualGroup(threadNamePrefix: "NIO-SGLTN-",
numberOfThreads: threadCount)
_ = Unmanaged.passUnretained(group).retain() // Never gonna give you up,
return group
}()

private let globalPosixBlockingPool: NIOThreadPool = {
guard NIOGlobalSingletons.globalSingletonsEnabledSuggestion else {
guard NIOSingletons.singletonsEnabledSuggestion else {
fatalError("""
Cannot create global singleton NIOThreadPool because the global singletons have been \
disabled by setting `NIOGlobalSingletons.globalSingletonsEnabledSuggestion = false`
disabled by setting `NIOSingletons.singletonsEnabledSuggestion = false`
""")
}
let pool = NIOThreadPool._makePerpetualStartedPool(
numberOfThreads: NIOGlobalSingletons.suggestedBlockingPoolThreadCount,
numberOfThreads: NIOSingletons.blockingPoolThreadCountSuggestion,
threadNamePrefix: "SGLTN-TP-#"
)
_ = Unmanaged.passUnretained(pool).retain() // never gonna let you down.
Expand Down
26 changes: 13 additions & 13 deletions Tests/NIOSingletonsTests/GlobalSingletonsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,50 +17,50 @@ import NIOCore
import NIOPosix
import Foundation

final class NIOGlobalSingletonsTests: XCTestCase {
final class NIOSingletonsTests: XCTestCase {
func testSingletonMultiThreadedEventLoopWorks() async throws {
let works = try await MultiThreadedEventLoopGroup.globalSingleton.any().submit { "yes" }.get()
let works = try await MultiThreadedEventLoopGroup.singleton.any().submit { "yes" }.get()
XCTAssertEqual(works, "yes")
}

func testSingletonBlockingPoolWorks() async throws {
let works = try await NIOThreadPool.globalSingleton.runIfActive(
eventLoop: MultiThreadedEventLoopGroup.globalSingleton.any()
let works = try await NIOThreadPool.singleton.runIfActive(
eventLoop: MultiThreadedEventLoopGroup.singleton.any()
) {
"yes"
}.get()
XCTAssertEqual(works, "yes")
}

func testCannotShutdownMultiGroup() {
XCTAssertThrowsError(try MultiThreadedEventLoopGroup.globalSingleton.syncShutdownGracefully()) { error in
XCTAssertThrowsError(try MultiThreadedEventLoopGroup.singleton.syncShutdownGracefully()) { error in
XCTAssertEqual(.unsupportedOperation, error as? EventLoopError)
}
}

func testCannotShutdownBlockingPool() {
XCTAssertThrowsError(try NIOThreadPool.globalSingleton.syncShutdownGracefully()) { error in
XCTAssertThrowsError(try NIOThreadPool.singleton.syncShutdownGracefully()) { error in
XCTAssert(error is NIOThreadPoolError.UnsupportedOperation)
}
}

func testMultiGroupThreadPrefix() {
XCTAssert(MultiThreadedEventLoopGroup.globalSingleton.description.contains("NIO-SGLTN-"),
"\(MultiThreadedEventLoopGroup.globalSingleton.description)")
XCTAssert(MultiThreadedEventLoopGroup.singleton.description.contains("NIO-SGLTN-"),
"\(MultiThreadedEventLoopGroup.singleton.description)")

for _ in 0..<100 {
let someEL = MultiThreadedEventLoopGroup.globalSingleton.next()
let someEL = MultiThreadedEventLoopGroup.singleton.next()
XCTAssert(someEL.description.contains("NIO-SGLTN-"), "\(someEL.description)")
}
}

func testSingletonsAreEnabledAndCanBeReadMoreThanOnce() {
XCTAssertTrue(NIOGlobalSingletons.globalSingletonsEnabledSuggestion)
XCTAssertTrue(NIOGlobalSingletons.globalSingletonsEnabledSuggestion)
XCTAssertTrue(NIOGlobalSingletons.globalSingletonsEnabledSuggestion)
XCTAssertTrue(NIOSingletons.singletonsEnabledSuggestion)
XCTAssertTrue(NIOSingletons.singletonsEnabledSuggestion)
XCTAssertTrue(NIOSingletons.singletonsEnabledSuggestion)
}

func testCanCreateClientBootstrapWithoutSpecifyingTypeName() {
_ = ClientBootstrap(group: .globalSingletonMultiThreadedEventLoopGroup)
_ = ClientBootstrap(group: .singletonMultiThreadedEventLoopGroup)
}
}

0 comments on commit 1cd1d4e

Please sign in to comment.