-
Notifications
You must be signed in to change notification settings - Fork 652
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
thread configuration (names & QoS on Darwin) #2943
Open
weissi
wants to merge
2
commits into
apple:main
Choose a base branch
from
weissi:jw-thread-config
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,7 +70,7 @@ public final class MultiThreadedEventLoopGroup: EventLoopGroup { | |
private let index = ManagedAtomic<Int>(0) | ||
private var eventLoops: [SelectableEventLoop] | ||
private let shutdownLock: NIOLock = NIOLock() | ||
private let threadNamePrefix: String | ||
private let threadNamePrefix: String? | ||
private var runState: RunState = .running | ||
private let canBeShutDown: Bool | ||
|
||
|
@@ -108,7 +108,8 @@ public final class MultiThreadedEventLoopGroup: EventLoopGroup { | |
} | ||
|
||
private static func setupThreadAndEventLoop( | ||
name: String, | ||
name: String?, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How come name is optional now? |
||
threadConfiguration: NIOThreadConfiguration, | ||
parentGroup: MultiThreadedEventLoopGroup, | ||
selectorFactory: @escaping () throws -> NIOPosix.Selector<NIORegistration>, | ||
initializer: @escaping ThreadInitializer, | ||
|
@@ -119,7 +120,7 @@ public final class MultiThreadedEventLoopGroup: EventLoopGroup { | |
// synchronised by `lock` | ||
var _loop: SelectableEventLoop! = nil | ||
|
||
NIOThread.spawnAndRun(name: name, detachThread: false) { t in | ||
NIOThread.spawnAndRun(name: name, configuration: threadConfiguration, detachThread: false) { t in | ||
MultiThreadedEventLoopGroup.runTheLoop( | ||
thread: t, | ||
parentGroup: parentGroup, | ||
|
@@ -150,6 +151,7 @@ public final class MultiThreadedEventLoopGroup: EventLoopGroup { | |
public convenience init(numberOfThreads: Int) { | ||
self.init( | ||
numberOfThreads: numberOfThreads, | ||
threadConfiguration: .defaultForEventLoopGroups, | ||
canBeShutDown: true, | ||
metricsDelegate: nil, | ||
selectorFactory: NIOPosix.Selector<NIORegistration>.init | ||
|
@@ -169,6 +171,32 @@ public final class MultiThreadedEventLoopGroup: EventLoopGroup { | |
public convenience init(numberOfThreads: Int, metricsDelegate: NIOEventLoopMetricsDelegate) { | ||
self.init( | ||
numberOfThreads: numberOfThreads, | ||
threadConfiguration: .defaultForEventLoopGroups, | ||
canBeShutDown: true, | ||
metricsDelegate: metricsDelegate, | ||
selectorFactory: NIOPosix.Selector<NIORegistration>.init | ||
) | ||
} | ||
|
||
/// Creates a `MultiThreadedEventLoopGroup` instance which uses `numberOfThreads`. | ||
/// | ||
/// - note: Don't forget to call `shutdownGracefully` or `syncShutdownGracefully` when you no longer need this | ||
/// `EventLoopGroup`. If you forget to shut the `EventLoopGroup` down you will leak `numberOfThreads` | ||
/// (kernel) threads which are costly resources. This is especially important in unit tests where one | ||
/// `MultiThreadedEventLoopGroup` is started per test case. | ||
/// | ||
/// - Parameters: | ||
/// - numberOfThreads: The number of `Threads` to use. | ||
/// - threadConfiguration: Configuration for the threads to spawn. | ||
/// - metricsDelegate: Delegate for collecting information from this eventloop | ||
public convenience init( | ||
numberOfThreads: Int, | ||
threadConfiguration: NIOThreadConfiguration, | ||
metricsDelegate: NIOEventLoopMetricsDelegate? = nil | ||
) { | ||
self.init( | ||
numberOfThreads: numberOfThreads, | ||
threadConfiguration: threadConfiguration, | ||
canBeShutDown: true, | ||
metricsDelegate: metricsDelegate, | ||
selectorFactory: NIOPosix.Selector<NIORegistration>.init | ||
|
@@ -179,20 +207,21 @@ public final class MultiThreadedEventLoopGroup: EventLoopGroup { | |
/// | ||
/// This is only useful for global singletons. | ||
public static func _makePerpetualGroup( | ||
threadNamePrefix: String, | ||
numberOfThreads: Int | ||
numberOfThreads: Int, | ||
threadConfiguration: NIOThreadConfiguration | ||
) -> MultiThreadedEventLoopGroup { | ||
self.init( | ||
numberOfThreads: numberOfThreads, | ||
threadConfiguration: threadConfiguration, | ||
canBeShutDown: false, | ||
threadNamePrefix: threadNamePrefix, | ||
metricsDelegate: nil, | ||
selectorFactory: NIOPosix.Selector<NIORegistration>.init | ||
) | ||
} | ||
|
||
internal convenience init( | ||
numberOfThreads: Int, | ||
threadConfiguration: NIOThreadConfiguration, | ||
metricsDelegate: NIOEventLoopMetricsDelegate?, | ||
selectorFactory: @escaping () throws -> NIOPosix.Selector<NIORegistration> | ||
) { | ||
|
@@ -201,31 +230,15 @@ public final class MultiThreadedEventLoopGroup: EventLoopGroup { | |
self.init( | ||
threadInitializers: initializers, | ||
canBeShutDown: true, | ||
threadConfiguration: threadConfiguration, | ||
metricsDelegate: metricsDelegate, | ||
selectorFactory: selectorFactory | ||
) | ||
} | ||
|
||
internal convenience init( | ||
numberOfThreads: Int, | ||
canBeShutDown: Bool, | ||
threadNamePrefix: String, | ||
metricsDelegate: NIOEventLoopMetricsDelegate?, | ||
selectorFactory: @escaping () throws -> NIOPosix.Selector<NIORegistration> | ||
) { | ||
precondition(numberOfThreads > 0, "numberOfThreads must be positive") | ||
let initializers: [ThreadInitializer] = Array(repeating: { _ in }, count: numberOfThreads) | ||
self.init( | ||
threadInitializers: initializers, | ||
canBeShutDown: canBeShutDown, | ||
threadNamePrefix: threadNamePrefix, | ||
metricsDelegate: metricsDelegate, | ||
selectorFactory: selectorFactory | ||
) | ||
} | ||
|
||
internal convenience init( | ||
numberOfThreads: Int, | ||
threadConfiguration: NIOThreadConfiguration, | ||
canBeShutDown: Bool, | ||
metricsDelegate: NIOEventLoopMetricsDelegate?, | ||
selectorFactory: @escaping () throws -> NIOPosix.Selector<NIORegistration> | ||
|
@@ -235,20 +248,23 @@ public final class MultiThreadedEventLoopGroup: EventLoopGroup { | |
self.init( | ||
threadInitializers: initializers, | ||
canBeShutDown: canBeShutDown, | ||
threadConfiguration: threadConfiguration, | ||
metricsDelegate: metricsDelegate, | ||
selectorFactory: selectorFactory | ||
) | ||
} | ||
|
||
internal convenience init( | ||
threadInitializers: [ThreadInitializer], | ||
threadConfiguration: NIOThreadConfiguration, | ||
metricsDelegate: NIOEventLoopMetricsDelegate?, | ||
selectorFactory: @escaping () throws -> NIOPosix.Selector<NIORegistration> = NIOPosix.Selector<NIORegistration> | ||
.init | ||
) { | ||
self.init( | ||
threadInitializers: threadInitializers, | ||
canBeShutDown: true, | ||
threadConfiguration: threadConfiguration, | ||
metricsDelegate: metricsDelegate, | ||
selectorFactory: selectorFactory | ||
) | ||
|
@@ -261,12 +277,12 @@ public final class MultiThreadedEventLoopGroup: EventLoopGroup { | |
internal init( | ||
threadInitializers: [ThreadInitializer], | ||
canBeShutDown: Bool, | ||
threadNamePrefix: String = "NIO-ELT-", | ||
threadConfiguration: NIOThreadConfiguration, | ||
metricsDelegate: NIOEventLoopMetricsDelegate?, | ||
selectorFactory: @escaping () throws -> NIOPosix.Selector<NIORegistration> = NIOPosix.Selector<NIORegistration> | ||
.init | ||
) { | ||
self.threadNamePrefix = threadNamePrefix | ||
self.threadNamePrefix = threadConfiguration.threadNamePrefix | ||
let myGroupID = nextEventLoopGroupID.loadThenWrappingIncrement(ordering: .relaxed) | ||
self.myGroupID = myGroupID | ||
var idx = 0 | ||
|
@@ -275,7 +291,8 @@ public final class MultiThreadedEventLoopGroup: EventLoopGroup { | |
self.eventLoops = threadInitializers.map { initializer in | ||
// Maximum name length on linux is 16 by default. | ||
let ev = MultiThreadedEventLoopGroup.setupThreadAndEventLoop( | ||
name: "\(threadNamePrefix)\(myGroupID)-#\(idx)", | ||
name: self.threadNamePrefix.map { "\($0)\(myGroupID)-#\(idx)" }, | ||
threadConfiguration: threadConfiguration, | ||
parentGroup: self, | ||
selectorFactory: selectorFactory, | ||
initializer: initializer, | ||
|
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this still used?