-
Notifications
You must be signed in to change notification settings - Fork 23
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
Use LockedValueBoxes for state protection #35
Open
PeterAdams-A
wants to merge
2
commits into
apple:main
Choose a base branch
from
PeterAdams-A:niolock
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
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 |
---|---|---|
|
@@ -190,15 +190,14 @@ extension CassandraSession { | |
|
||
extension CassandraClient { | ||
internal final class Session: CassandraSession { | ||
private let eventLoopGroupContainer: EventLoopGroupConainer | ||
private let eventLoopGroupContainer: EventLoopGroupContainer | ||
public var eventLoopGroup: EventLoopGroup { | ||
self.eventLoopGroupContainer.value | ||
} | ||
|
||
private let configuration: Configuration | ||
private let logger: Logger | ||
private var state = State.idle | ||
private let lock = Lock() | ||
private let stateStore = NIOLockedValueBox(State.idle) | ||
|
||
private let rawPointer: OpaquePointer | ||
|
||
|
@@ -212,58 +211,68 @@ extension CassandraClient { | |
case disconnected | ||
} | ||
|
||
internal init(configuration: Configuration, logger: Logger, eventLoopGroupContainer: EventLoopGroupConainer) { | ||
internal init(configuration: Configuration, logger: Logger, eventLoopGroupContainer: EventLoopGroupContainer) { | ||
self.configuration = configuration | ||
self.logger = logger | ||
self.eventLoopGroupContainer = eventLoopGroupContainer | ||
self.rawPointer = cass_session_new() | ||
} | ||
|
||
deinit { | ||
guard case .disconnected = (self.lock.withLock { self.state }) else { | ||
let isDisconnected = self.stateStore.withLockedValue { state in | ||
if case .disconnected = state { | ||
return true | ||
} | ||
return false | ||
} | ||
guard isDisconnected else { | ||
preconditionFailure("Session not shut down before the deinit. Please call session.shutdown() when no longer needed.") | ||
} | ||
cass_session_free(self.rawPointer) | ||
} | ||
|
||
func shutdown() throws { | ||
self.lock.lock() | ||
defer { | ||
self.state = .disconnected | ||
self.lock.unlock() | ||
} | ||
switch self.state { | ||
case .connected: | ||
try self.disconect() | ||
default: | ||
break | ||
try self.stateStore.withLockedValue { (state: inout State) in | ||
defer { | ||
state = .disconnected | ||
} | ||
switch state { | ||
case .connected: | ||
try self.disconect() | ||
default: | ||
break | ||
} | ||
} | ||
} | ||
|
||
func execute(statement: Statement, on eventLoop: EventLoop?, logger: Logger? = .none) -> EventLoopFuture<Rows> { | ||
let eventLoop = eventLoop ?? self.eventLoopGroup.next() | ||
let logger = logger ?? self.logger | ||
|
||
self.lock.lock() | ||
switch self.state { | ||
let (startingState, future) = self.stateStore.withLockedValue { (state: inout State) -> (State, EventLoopFuture<Void>?) in | ||
if case .idle = state { | ||
let future = self.connect(on: eventLoop, logger: logger) | ||
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. Same notes here. |
||
state = .connectingFuture(future) | ||
return (.idle, future) | ||
} else { | ||
return (state, nil) | ||
} | ||
} | ||
|
||
switch startingState { | ||
case .idle: | ||
let future = self.connect(on: eventLoop, logger: logger) | ||
self.state = .connectingFuture(future) | ||
self.lock.unlock() | ||
return future.flatMap { _ in | ||
self.lock.withLock { | ||
self.state = .connected | ||
return future!.flatMap { _ in | ||
self.stateStore.withLockedValue { (state: inout State) in | ||
state = .connected | ||
} | ||
return self.execute(statement: statement, on: eventLoop, logger: logger) | ||
} | ||
case .connectingFuture(let future): | ||
self.lock.unlock() | ||
return future.flatMap { _ in | ||
self.execute(statement: statement, on: eventLoop, logger: logger) | ||
} | ||
#if compiler(>=5.5) && canImport(_Concurrency) | ||
case .connecting(let task): | ||
self.lock.unlock() | ||
let promise = eventLoop.makePromise(of: Rows.self) | ||
if #available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) { | ||
promise.completeWithTask { | ||
|
@@ -274,7 +283,6 @@ extension CassandraClient { | |
return promise.futureResult | ||
#endif | ||
case .connected: | ||
self.lock.unlock() | ||
logger.debug("executing: \(statement.query)") | ||
logger.trace("\(statement.parameters)") | ||
let promise = eventLoop.makePromise(of: Rows.self) | ||
|
@@ -284,7 +292,6 @@ extension CassandraClient { | |
} | ||
return promise.futureResult | ||
case .disconnected: | ||
self.lock.unlock() | ||
if self.eventLoopGroupContainer.managed { | ||
// eventloop *is* shutdown now | ||
preconditionFailure("client is disconnected") | ||
|
@@ -444,28 +451,30 @@ extension CassandraClient.Session { | |
func execute(statement: CassandraClient.Statement, logger: Logger? = .none) async throws -> CassandraClient.Rows { | ||
let logger = logger ?? self.logger | ||
|
||
lock.lock() | ||
switch state { | ||
case .idle: | ||
let task = self.connect(logger: logger) | ||
state = .connecting(ConnectionTask(task)) | ||
lock.unlock() | ||
let (startingState, task) = self.stateStore.withLockedValue { (state: inout State) -> (State, Task<Void, Swift.Error>?) in | ||
if case .idle = state { | ||
let task = self.connect(logger: logger) | ||
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. And here. |
||
state = .connecting(ConnectionTask(task)) | ||
return (.idle, task) | ||
} else { | ||
return (state, nil) | ||
} | ||
} | ||
|
||
try await task.value | ||
lock.withLock { | ||
self.state = .connected | ||
switch startingState { | ||
case .idle: | ||
try await task!.value | ||
self.stateStore.withLockedValue { (state: inout State) in | ||
state = .connected | ||
} | ||
return try await self.execute(statement: statement, logger: logger) | ||
case .connectingFuture(let future): | ||
lock.unlock() | ||
try await future.get() | ||
return try await self.execute(statement: statement, logger: logger) | ||
case .connecting(let task): | ||
lock.unlock() | ||
try await task.task.value | ||
return try await self.execute(statement: statement, logger: logger) | ||
case .connected: | ||
lock.unlock() | ||
logger.debug("executing: \(statement.query)") | ||
logger.trace("\(statement.parameters)") | ||
let future = cass_session_execute(rawPointer, statement.rawPointer) | ||
|
@@ -475,7 +484,6 @@ extension CassandraClient.Session { | |
} | ||
} | ||
case .disconnected: | ||
lock.unlock() | ||
if eventLoopGroupContainer.managed { | ||
// eventloop *is* shutdown now | ||
preconditionFailure("client is disconnected") | ||
|
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.
I'm pretty nervous about this function being called with the lock held.