Skip to content

Commit

Permalink
Provider sync shutdown (#319)
Browse files Browse the repository at this point in the history
- When syncShutdown is called on RotatingCredentialProvider/DeferredCredentialProvider, it calls the same on the CredentialProvider it is managing.
- Fixed hang, where the syncShutdown wouldn't finish because it was waiting on a EventLoopFuture to complete but the EventLoopFuture couldnt finish because it couldnt get access to the lock that syncShutdown had locked.
  • Loading branch information
adam-fowler authored Jul 4, 2020
1 parent 5826235 commit 42e0f0e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class DeferredCredentialProvider: CredentialProvider {
}
}

private var provider: CredentialProvider
private var startupPromise: EventLoopPromise<Credential>
private var internalCredential: Credential? = nil

Expand All @@ -37,6 +38,7 @@ public class DeferredCredentialProvider: CredentialProvider {
/// - provider: Credential provider to wrap
public init(eventLoop: EventLoop, provider: CredentialProvider) {
self.startupPromise = eventLoop.makePromise(of: Credential.self)
self.provider = provider
provider.getCredential(on: eventLoop)
.flatMapErrorThrowing { _ in throw CredentialProviderError.noProvider }
.map { credential in
Expand All @@ -48,6 +50,7 @@ public class DeferredCredentialProvider: CredentialProvider {

public func syncShutdown() throws {
_ = try startupPromise.futureResult.wait()
try provider.syncShutdown()
}

/// Return credentials. If still in process of the getting credentials then return future result of `startupPromise`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public final class RotatingCredentialProvider: CredentialProvider {
}

public func syncShutdown() throws {
self.lock.lock()
defer { self.lock.unlock() }
if let future = credentialFuture {
let future: EventLoopFuture<Credential>? = self.lock.withLock { credentialFuture }
if let future = future {
_ = try future.wait()
}
try provider.syncShutdown()
}

public func getCredential(on eventLoop: EventLoop) -> EventLoopFuture<Credential> {
Expand Down

0 comments on commit 42e0f0e

Please sign in to comment.