Skip to content

Commit 1c955c8

Browse files
Replace token iterator with independent refreshOnce calls (#87)
The iteration is a relic of the previous implementation.
2 parents 6bce0b5 + bd0a699 commit 1c955c8

1 file changed

Lines changed: 20 additions & 25 deletions

File tree

pushiko-fcm/src/main/kotlin/com/bloomberg/pushiko/fcm/oauth/CredentialsRefreshManager.kt

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,33 +42,10 @@ internal class CredentialsRefreshManager(
4242
private val backOff: BackOff = OAuthRefreshBackOff(credentials)
4343
) {
4444
private val logger = Logger()
45-
private val iterator = iterator {
46-
while (true) {
47-
try {
48-
credentials.run {
49-
refresh()
50-
logger.info(
51-
"Google OAuth token was refreshed for {}, expires in at most {}s",
52-
credentials.projectId,
53-
accessToken.expiresInSeconds
54-
)
55-
yield(RefreshResult.Success(accessToken))
56-
}
57-
} catch (e: IOException) {
58-
if (e.isPermanentFailure()) {
59-
logger.error("OAuth refresh failed permanently for {}: {}", credentials.projectId, e.message)
60-
yield(RefreshResult.PermanentFailure(e))
61-
} else {
62-
logger.warn("No new Google OAuth token for ${credentials.projectId} is available", e)
63-
yield(RefreshResult.RetryableFailure(e))
64-
}
65-
}
66-
}
67-
}
6845
private val scope = CoroutineScope(SupervisorJob() + dispatcher)
6946
private val startup = scope.async(start = CoroutineStart.LAZY) {
7047
while (currentCoroutineContext().isActive) {
71-
when (val result = iterator.next()) {
48+
when (val result = refreshOnce()) {
7249
is RefreshResult.Success -> {
7350
logger.info(
7451
"Startup OAuth refresh succeeded for {}, token expires in at most {}s",
@@ -109,6 +86,24 @@ internal class CredentialsRefreshManager(
10986
scope.cancel()
11087
}
11188

89+
private fun refreshOnce(): RefreshResult = try {
90+
credentials.refresh()
91+
logger.info(
92+
"Google OAuth token was refreshed for {}, expires in at most {}s",
93+
credentials.projectId,
94+
credentials.accessToken.expiresInSeconds
95+
)
96+
RefreshResult.Success(credentials.accessToken)
97+
} catch (e: IOException) {
98+
if (e.isPermanentFailure()) {
99+
logger.error("OAuth refresh failed permanently for {}: {}", credentials.projectId, e.message)
100+
RefreshResult.PermanentFailure(e)
101+
} else {
102+
logger.warn("No new Google OAuth token for ${credentials.projectId} is available", e)
103+
RefreshResult.RetryableFailure(e)
104+
}
105+
}
106+
112107
private suspend fun keepAlive() {
113108
while (currentCoroutineContext().isActive) {
114109
val interval = backOff.nextBackOffMillis()
@@ -126,7 +121,7 @@ internal class CredentialsRefreshManager(
126121
TimeUnit.MILLISECONDS.toSeconds(interval)
127122
)
128123
delay(interval)
129-
when (val result = iterator.next()) {
124+
when (val result = refreshOnce()) {
130125
is RefreshResult.Success -> {
131126
logger.info(
132127
"Keep-alive OAuth refresh succeeded for {}, token expires in at most {}s",

0 commit comments

Comments
 (0)