-
Notifications
You must be signed in to change notification settings - Fork 46
[feat]: introduce runtime configuration SPI #354
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
+238
−7
Merged
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 hidden or 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,102 @@ | ||
/* | ||
* Copyright Square Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/// System for managing configuration options for Workflow runtime behaviors. | ||
/// - important: These interfaces are subject to breaking changes without corresponding semantic | ||
/// versioning changes. | ||
@_spi(WorkflowRuntimeConfig) | ||
public enum Runtime { | ||
@TaskLocal | ||
static var _currentConfiguration: Configuration? | ||
|
||
static var _bootstrapConfiguration = BootstrappableConfiguration() | ||
|
||
/// Bootstrap the workflow runtime with the given configuration. | ||
/// This can only be called once per process and must be called from the main thread. | ||
/// | ||
/// - Parameter configuration: The runtime configuration to use. | ||
@MainActor | ||
public static func bootstrap( | ||
_ configureBlock: (inout Configuration) -> Void | ||
) { | ||
MainActor.preconditionIsolated( | ||
"The Workflow runtime must be bootstrapped from the main actor." | ||
) | ||
guard !_isBootstrapped else { | ||
fatalError("The Workflow runtime can only be bootstrapped once.") | ||
} | ||
|
||
var config = _bootstrapConfiguration.currentConfiguration | ||
configureBlock(&config) | ||
_bootstrapConfiguration._bootstrapConfig = config | ||
} | ||
|
||
static var configuration: Configuration { | ||
_currentConfiguration ?? _bootstrapConfiguration.currentConfiguration | ||
} | ||
|
||
/// Allows temporary customization of the runtime configuration during the execution of the `operation`. | ||
/// | ||
/// - Parameters: | ||
/// - override: An option block to reconfigure the current configuration value. | ||
/// - operation: The operation to perform with the customized configuration. | ||
public static func withConfiguration<T>( | ||
override: ((inout Configuration) -> Void)? = nil, | ||
operation: () -> T | ||
) -> T { | ||
var configSnapshot = configuration | ||
override?(&configSnapshot) | ||
|
||
return Runtime | ||
.$_currentConfiguration | ||
.withValue( | ||
configSnapshot, | ||
operation: operation | ||
) | ||
} | ||
|
||
// MARK: - | ||
|
||
private static var _isBootstrapped: Bool { | ||
_bootstrapConfiguration._bootstrapConfig != nil | ||
} | ||
|
||
/// The current runtime configuration that may have been set via `bootstrap()`. | ||
private static var _currentBootstrapConfiguration: Configuration { | ||
_bootstrapConfiguration.currentConfiguration | ||
} | ||
} | ||
|
||
extension Runtime { | ||
/// Configuration options for the Workflow runtime. | ||
public struct Configuration: Equatable { | ||
/// The default runtime configuration. | ||
static let `default` = Configuration() | ||
|
||
/// Note: this doesn't control anything yet, but is here as a placeholder | ||
public var renderOnlyIfStateChanged: Bool = false | ||
} | ||
|
||
struct BootstrappableConfiguration { | ||
var _bootstrapConfig: Configuration? | ||
let _defaultConfig: Configuration = .default | ||
|
||
/// The current runtime configuration that may have been set via `Runtime.bootstrap()`. | ||
var currentConfiguration: Configuration { | ||
_bootstrapConfig ?? _defaultConfig | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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,89 @@ | ||
/* | ||
* Copyright Square Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import Testing | ||
|
||
@_spi(WorkflowRuntimeConfig) @testable import Workflow | ||
|
||
@MainActor | ||
struct RuntimeConfigTests { | ||
@Test | ||
private func runtime_config_inits_to_default() { | ||
let cfg = Runtime.Configuration() | ||
#expect(cfg == Runtime.Configuration.default) | ||
} | ||
|
||
@Test | ||
private func test_global_config_defaults_to_default() { | ||
#expect(Runtime.configuration == .default) | ||
} | ||
|
||
@Test | ||
private func runtime_config_prefers_bootstrap_value() { | ||
#expect(Runtime.configuration.renderOnlyIfStateChanged == false) | ||
|
||
defer { | ||
// reset global state... | ||
Runtime.resetConfig() | ||
} | ||
Runtime.bootstrap { cfg in | ||
cfg.renderOnlyIfStateChanged = true | ||
} | ||
|
||
#expect(Runtime.configuration.renderOnlyIfStateChanged == true) | ||
} | ||
|
||
@Test | ||
private func test_config_respects_task_local_overrides() { | ||
var customConfig = Runtime.configuration | ||
customConfig.renderOnlyIfStateChanged = true | ||
|
||
Runtime.$_currentConfiguration.withValue(customConfig) { | ||
#expect(Runtime.configuration.renderOnlyIfStateChanged == true) | ||
} | ||
} | ||
|
||
@Test | ||
private func test_withConfiguration() { | ||
#expect(Runtime.configuration.renderOnlyIfStateChanged == false) | ||
|
||
var override = Runtime.configuration | ||
override.renderOnlyIfStateChanged = true | ||
|
||
let newValue = Runtime.$_currentConfiguration.withValue(override) { | ||
Runtime.withConfiguration { | ||
Runtime.configuration.renderOnlyIfStateChanged | ||
} | ||
} | ||
#expect(newValue == true) | ||
} | ||
|
||
@Test | ||
private func test_withConfigurationOverride() { | ||
let newValue = Runtime.withConfiguration( | ||
override: { cfg in | ||
#expect(Runtime.configuration.renderOnlyIfStateChanged == false) | ||
#expect(cfg.renderOnlyIfStateChanged == false) | ||
cfg.renderOnlyIfStateChanged = true | ||
}, | ||
operation: { | ||
Runtime.configuration.renderOnlyIfStateChanged | ||
} | ||
) | ||
|
||
#expect(newValue == true) | ||
} | ||
} |
This file contains hidden or 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 hidden or 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.
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.
not at all a blocker, but IMO the one-time bootstrapping requirement is overkill vs having a static var that can be changed at any time