Skip to content

Commit bb57bf2

Browse files
author
Ryan Dingman
committed
Update PlatformOptions to have preferredStreamBufferSizeRange and use RangeExpression to express the various ways to configure the low and high watermark
1 parent aa903ad commit bb57bf2

File tree

4 files changed

+45
-37
lines changed

4 files changed

+45
-37
lines changed

Sources/Subprocess/Platforms/Subprocess+Darwin.swift

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ public struct PlatformOptions: Sendable {
5757
/// Creates a session and sets the process group ID
5858
/// i.e. Detach from the terminal.
5959
public var createSession: Bool = false
60-
public var outputOptions: StreamOptions = .init()
61-
public var errorOptions: StreamOptions = .init()
60+
61+
private(set) var preferredStreamBufferSizeRange: (any RangeExpression & Sendable)? = nil
62+
6263
/// An ordered list of steps in order to tear down the child
6364
/// process in case the parent task is cancelled before
6465
/// the child proces terminates.
@@ -84,7 +85,17 @@ public struct PlatformOptions: Sendable {
8485
) throws -> Void
8586
)? = nil
8687

87-
public init() {}
88+
public init() {
89+
self.preferredStreamBufferSizeRange = nil
90+
}
91+
92+
public init<R>(preferredStreamBufferSizeRange: R?) where R: RangeExpression & Sendable, R.Bound == Int {
93+
self.preferredStreamBufferSizeRange = preferredStreamBufferSizeRange
94+
}
95+
96+
mutating func setPreferredStreamBufferSizeRange<R>(_ range: R?) where R: RangeExpression & Sendable, R.Bound == Int {
97+
self.preferredStreamBufferSizeRange = range
98+
}
8899
}
89100

90101
extension PlatformOptions {
@@ -128,18 +139,6 @@ extension PlatformOptions {
128139
#endif
129140
}
130141

131-
extension PlatformOptions {
132-
public struct StreamOptions: Sendable {
133-
let lowWater: Int?
134-
let highWater: Int?
135-
136-
init(lowWater: Int? = nil, highWater: Int? = nil) {
137-
self.lowWater = lowWater
138-
self.highWater = highWater
139-
}
140-
}
141-
}
142-
143142
extension PlatformOptions: CustomStringConvertible, CustomDebugStringConvertible {
144143
internal func description(withIndent indent: Int) -> String {
145144
let indent = String(repeating: " ", count: indent * 4)
@@ -369,8 +368,8 @@ extension Configuration {
369368
output: output,
370369
error: error,
371370
inputPipe: inputPipe.createInputPipe(),
372-
outputPipe: outputPipe.createOutputPipe(with: platformOptions.outputOptions),
373-
errorPipe: errorPipe.createOutputPipe(with: platformOptions.errorOptions)
371+
outputPipe: outputPipe.createOutputPipe(with: platformOptions),
372+
errorPipe: errorPipe.createOutputPipe(with: platformOptions)
374373
)
375374
}
376375

Sources/Subprocess/Platforms/Subprocess+Linux.swift

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,9 @@ public struct PlatformOptions: Sendable {
176176
// Creates a session and sets the process group ID
177177
// i.e. Detach from the terminal.
178178
public var createSession: Bool = false
179-
public var outputOptions: StreamOptions = .init()
180-
public var errorOptions: StreamOptions = .init()
179+
180+
private(set) var preferredStreamBufferSizeRange: (any RangeExpression & Sendable)? = nil
181+
181182
/// An ordered list of steps in order to tear down the child
182183
/// process in case the parent task is cancelled before
183184
/// the child proces terminates.
@@ -196,18 +197,16 @@ public struct PlatformOptions: Sendable {
196197
/// call any necessary process setup functions.
197198
public var preSpawnProcessConfigurator: (@convention(c) @Sendable () -> Void)? = nil
198199

199-
public init() {}
200-
}
200+
public init() {
201+
self.preferredStreamBufferSizeRange = nil
202+
}
201203

202-
extension PlatformOptions {
203-
public struct StreamOptions: Sendable {
204-
let lowWater: Int?
205-
let highWater: Int?
204+
public init<R>(preferredStreamBufferSizeRange: R?) where R: RangeExpression & Sendable, R.Bound == Int {
205+
self.preferredStreamBufferSizeRange = preferredStreamBufferSizeRange
206+
}
206207

207-
init(lowWater: Int? = nil, highWater: Int? = nil) {
208-
self.lowWater = lowWater
209-
self.highWater = highWater
210-
}
208+
mutating func setPreferredStreamBufferSizeRange<R>(_ range: R?) where R: RangeExpression & Sendable, R.Bound == Int {
209+
self.preferredStreamBufferSizeRange = range
211210
}
212211
}
213212

Sources/Subprocess/Platforms/Subprocess+Unix.swift

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ extension CreatedPipe {
424424
)
425425
}
426426

427-
internal func createOutputPipe(with options: PlatformOptions.StreamOptions) -> OutputPipe {
427+
internal func createOutputPipe(with options: PlatformOptions) -> OutputPipe {
428428
var readEnd: TrackedPlatformDiskIO? = nil
429429
if let readFileDescriptor = self.readFileDescriptor {
430430
let dispatchIO: DispatchIO = DispatchIO(
@@ -439,12 +439,22 @@ extension CreatedPipe {
439439
}
440440
)
441441

442-
if let lowWater = options.lowWater {
443-
dispatchIO.setLimit(lowWater: lowWater)
444-
}
445-
446-
if let highWater = options.highWater {
447-
dispatchIO.setLimit(highWater: highWater)
442+
if let preferredStreamBufferSizeRange = options.preferredStreamBufferSizeRange {
443+
if let range = preferredStreamBufferSizeRange as? Range<Int> {
444+
dispatchIO.setLimit(lowWater: range.lowerBound)
445+
dispatchIO.setLimit(highWater: range.upperBound)
446+
} else if let range = preferredStreamBufferSizeRange as? ClosedRange<Int> {
447+
dispatchIO.setLimit(lowWater: range.lowerBound)
448+
dispatchIO.setLimit(highWater: range.upperBound)
449+
} else if let range = preferredStreamBufferSizeRange as? PartialRangeFrom<Int> {
450+
dispatchIO.setLimit(lowWater: range.lowerBound)
451+
} else if let range = preferredStreamBufferSizeRange as? PartialRangeUpTo<Int> {
452+
dispatchIO.setLimit(highWater: range.upperBound - 1)
453+
} else if let range = preferredStreamBufferSizeRange as? PartialRangeThrough<Int> {
454+
dispatchIO.setLimit(highWater: range.upperBound)
455+
} else {
456+
fatalError("Unsupported preferredStreamBufferSizeRange: \(preferredStreamBufferSizeRange)")
457+
}
448458
}
449459

450460
readEnd = .init(

Tests/SubprocessTests/SubprocessTests+Unix.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ extension SubprocessUnixTests {
675675
"""
676676

677677
var platformOptions = PlatformOptions()
678-
platformOptions.outputOptions = .init(lowWater: 0)
678+
platformOptions.setPreferredStreamBufferSizeRange(0...)
679679

680680
let start = ContinuousClock().now
681681

0 commit comments

Comments
 (0)