Skip to content

Commit 56743de

Browse files
author
Guilherme Souza
committed
refactor AutoRefreshToken constants
1 parent a20129e commit 56743de

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

Sources/Auth/Internal/AutoRefreshToken.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ import ConcurrencyExtras
99
import Foundation
1010

1111
actor AutoRefreshToken {
12-
private var task: Task<Void, Never>?
12+
/// Current session will be checked for refresh at this interval.
13+
static let tickDuration: TimeInterval = 30
14+
/// A token refresh will be attempted this many ticks before the current session expires.
15+
static let tickThreshold = 3
1316

14-
static let autoRefreshTickDuration: TimeInterval = 30
15-
private let autoRefreshTickThreshold = 3
17+
private var task: Task<Void, Never>?
1618

1719
@Dependency(\.sessionManager) var sessionManager
1820
@Dependency(\.logger) var logger
@@ -24,7 +26,7 @@ actor AutoRefreshToken {
2426
task = Task {
2527
while !Task.isCancelled {
2628
await autoRefreshTokenTick()
27-
try? await Task.sleep(nanoseconds: UInt64(Self.autoRefreshTickDuration) * NSEC_PER_SEC)
29+
try? await Task.sleep(nanoseconds: UInt64(AutoRefreshToken.tickDuration) * NSEC_PER_SEC)
2830
}
2931
}
3032
}
@@ -52,14 +54,14 @@ actor AutoRefreshToken {
5254
let expiresAt = session.expiresAt
5355

5456
// session will expire in this many ticks (or has already expired if <= 0)
55-
let expiresInTicks = Int((expiresAt - now.timeIntervalSince1970) / Self.autoRefreshTickDuration)
57+
let expiresInTicks = Int((expiresAt - now.timeIntervalSince1970) / AutoRefreshToken.tickDuration)
5658

5759
logger?
5860
.debug(
59-
"access token expires in \(expiresInTicks) ticks, a tick last \(Self.autoRefreshTickDuration)s, refresh threshold is \(autoRefreshTickThreshold) ticks"
61+
"access token expires in \(expiresInTicks) ticks, a tick last \(AutoRefreshToken.tickDuration)s, refresh threshold is \(AutoRefreshToken.tickThreshold) ticks"
6062
)
6163

62-
if expiresInTicks <= autoRefreshTickThreshold {
64+
if expiresInTicks <= AutoRefreshToken.tickThreshold {
6365
_ = try await sessionManager.refreshSession(session.refreshToken)
6466
}
6567

Sources/Auth/Internal/SessionManager.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ actor LiveSessionRefresher {
3333
private var eventEmitter
3434

3535
func refreshSession(_ refreshToken: String) async throws -> Session {
36-
logger?.debug("being")
36+
logger?.debug("begin")
3737
defer { logger?.debug("end") }
3838

3939
if let inFlightRefreshTask {
@@ -53,7 +53,7 @@ actor LiveSessionRefresher {
5353
}
5454

5555
private func _refreshAccessTokenWithRetry(_ refreshToken: String) async throws -> Session {
56-
logger?.debug("being")
56+
logger?.debug("begin")
5757
defer { logger?.debug("end") }
5858

5959
let startedAt = Date()
@@ -80,7 +80,7 @@ actor LiveSessionRefresher {
8080
return
8181
isRetryableError(error) &&
8282
// retryable only if the request can be sent before the backoff overflows the tick duration
83-
Date().timeIntervalSince1970 + nextBackoffInterval - startedAt.timeIntervalSince1970 < AutoRefreshToken.autoRefreshTickDuration
83+
Date().timeIntervalSince1970 + nextBackoffInterval - startedAt.timeIntervalSince1970 < AutoRefreshToken.tickDuration
8484
}
8585
}
8686
}

0 commit comments

Comments
 (0)