Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import Foundation

public enum GoogleAccessTokenWait {
// A cancelled caller stops waiting; the fetch, which other callers may share, runs on to its end.
public static func token(from provider: any GoogleAccessTokenProviding) async throws -> String {
let wait = TokenWait(provider: provider)
return try await withTaskCancellationHandler {
try await withCheckedThrowingContinuation { caller in
wait.start(caller: caller)
}
} onCancel: {
wait.cancel()
}
}
}

private final class TokenWait: @unchecked Sendable {
typealias Caller = CheckedContinuation<String, any Error>

private enum Stage {
case notStarted
case cancelledBeforeStart
case waiting(Caller)
case finished
}

private let provider: any GoogleAccessTokenProviding
private let lock = NSLock()
private var stage = Stage.notStarted

init(provider: any GoogleAccessTokenProviding) {
self.provider = provider
}

func start(caller waiting: Caller) {
let launches: Bool = lock.withLock {
switch stage {
case .notStarted:
stage = .waiting(waiting)
return true
case .cancelledBeforeStart:
stage = .finished
return false
case .waiting, .finished:
return false
}
}
guard launches else {
waiting.resume(throwing: CancellationError())
return
}
Task { [provider] in
let result: Result<String, any Error>
do {
result = .success(try await provider.accessToken())
} catch {
result = .failure(error)
}
self.finish(with: result)
}
}

func cancel() {
let waiting: Caller? = lock.withLock {
switch stage {
case .notStarted:
stage = .cancelledBeforeStart
return nil
case .waiting(let caller):
stage = .finished
return caller
case .cancelledBeforeStart, .finished:
return nil
}
}
waiting?.resume(throwing: CancellationError())
}

private func finish(with result: Result<String, any Error>) {
let waiting: Caller? = lock.withLock {
guard case .waiting(let caller) = stage else { return nil }
stage = .finished
return caller
}
waiting?.resume(with: result)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import Foundation

public enum BigQueryRequestLoop {
public struct Answer: Sendable {
public let status: Int
public let data: Data

public init(status: Int, data: Data) {
self.status = status
self.data = data
}
}

public static let rateLimitRetries = 3
private static let unauthorizedStatus = 401
private static let rateLimitedStatus = 429

// Nil when Stop came before a send. Stop is read again after the token fetch, which can take a round trip.
public static func run<Token>(
signals: BigQueryRequestSignals,
authorize: () async throws -> Token,
send: (Token) async throws -> Answer,
refreshToken: () async -> Void,
backoff: (Int) async throws -> Void
) async throws -> Answer? {
var rateLimitAttempt = 0
var refreshedToken = false
while true {
guard !signals.isStopped() else { return nil }
let token = try await authorize()
guard signals.beginAttempt() else { return nil }
let answer = try await send(token)
if answer.status == unauthorizedStatus, !refreshedToken {
refreshedToken = true
await refreshToken()
} else if answer.status == rateLimitedStatus, rateLimitAttempt < rateLimitRetries {
rateLimitAttempt += 1
try await backoff(rateLimitAttempt)
} else {
return answer
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import Foundation

public enum BigQueryStatementPolicy {
public static let jobCreationMode = "JOB_CREATION_OPTIONAL"

// A stopped statement's job reference arrives within this wait, so its cancel can follow.
public static let serverWaitMilliseconds = 2_000

public static let stopGrace: Duration = .seconds(1)

public static let stoppedRequestLimit: Duration = .seconds(10)

// The server caps an answer at 10 MB.
public static let answerByteLimit = 32 * 1_024 * 1_024

// jobs.query answers a statement the server refuses with these, where jobs.insert reported them on the job.
public static func isStatementRefusal(status: Int) -> Bool {
status == 400 || status == 403 || status == 404 || status == 409
}
}

public struct BigQueryStatementRequest<Parameter: Encodable & Sendable>: Encodable, Sendable {
public struct Dataset: Encodable, Sendable {
public let projectId: String
public let datasetId: String

public init(projectId: String, datasetId: String) {
self.projectId = projectId
self.datasetId = datasetId
}
}

public let query: String
public let useLegacySql: Bool
public let jobCreationMode: String
// A retry with the same id returns the first request's statement instead of running it again.
public let requestId: String
public let maxResults: Int
public let timeoutMs: Int
public let location: String?
public let defaultDataset: Dataset?
public let maximumBytesBilled: String?
public let parameterMode: String?
public let queryParameters: [Parameter]?
public let jobTimeoutMs: String?

public init(
query: String,
requestId: String,
maxResults: Int,
location: String?,
defaultDataset: Dataset?,
maximumBytesBilled: String?,
parameterMode: String?,
queryParameters: [Parameter]?,
jobTimeoutMs: String?
) {
self.query = query
self.useLegacySql = false
self.jobCreationMode = BigQueryStatementPolicy.jobCreationMode
self.requestId = requestId
self.maxResults = maxResults
self.timeoutMs = BigQueryStatementPolicy.serverWaitMilliseconds
self.location = location
self.defaultDataset = defaultDataset
self.maximumBytesBilled = maximumBytesBilled
self.parameterMode = parameterMode
self.queryParameters = queryParameters
self.jobTimeoutMs = jobTimeoutMs
}
}

public enum BigQueryStatementProgress: Equatable, Sendable {
case finished(jobId: String?)
case running(jobId: String)
case unreadable

// A statement that finished without a job cannot page, so a page token with no job cannot be followed.
public init(jobComplete: Bool?, jobId: String?, pageToken: String?) {
if jobComplete == false {
self = jobId.map { .running(jobId: $0) } ?? .unreadable
} else if jobId == nil, pageToken != nil {
self = .unreadable
} else {
self = .finished(jobId: jobId)
}
}

public static func jobToCancel(jobComplete: Bool?, jobId: String?) -> String? {
guard case .running(let jobId) = BigQueryStatementProgress(jobComplete: jobComplete, jobId: jobId, pageToken: nil)
else { return nil }
return jobId
}
}
Loading
Loading