-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix(kotlin): Honor Retry-After for pool warmups #1512
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
Open
Gujiassh
wants to merge
1
commit into
opensandbox-group:main
Choose a base branch
from
Gujiassh:fix/kotlin-pool-retry-after-1500
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
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 hidden or 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
64 changes: 64 additions & 0 deletions
64
...src/main/kotlin/com/alibaba/opensandbox/sandbox/infrastructure/pool/PoolRateLimitState.kt
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| * Copyright 2026 Alibaba Group Holding Ltd. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.alibaba.opensandbox.sandbox.infrastructure.pool | ||
|
|
||
| import com.alibaba.opensandbox.sandbox.transport.RETRY_AFTER_CAP | ||
| import java.time.Duration | ||
| import java.time.Instant | ||
|
|
||
| /** Per-run warmup throttle established by rate-limited sandbox creates. */ | ||
| internal class PoolRateLimitState( | ||
| private val defaultDelay: Duration = DEFAULT_RATE_LIMIT_DELAY, | ||
| private val maxDelay: Duration = RETRY_AFTER_CAP, | ||
| ) { | ||
| init { | ||
| require(!defaultDelay.isNegative) { "defaultDelay must not be negative" } | ||
| require(!maxDelay.isNegative) { "maxDelay must not be negative" } | ||
| } | ||
|
|
||
| @Volatile | ||
| private var throttleUntil: Instant? = null | ||
|
|
||
| /** Extends, but never shortens, the current throttle deadline. */ | ||
| @Synchronized | ||
| fun recordRateLimit( | ||
| retryAfter: Duration?, | ||
| now: Instant = Instant.now(), | ||
| ) { | ||
| val requestedDelay = retryAfter?.takeUnless { it.isNegative } ?: defaultDelay | ||
| val candidate = now.plus(minOf(requestedDelay, maxDelay)) | ||
| val current = throttleUntil | ||
| if (current == null || candidate.isAfter(current)) { | ||
| throttleUntil = candidate | ||
| } | ||
| } | ||
|
|
||
| fun isActive(now: Instant = Instant.now()): Boolean { | ||
| val until = throttleUntil ?: return false | ||
| return now.isBefore(until) | ||
| } | ||
|
|
||
| fun remainingDelay(now: Instant = Instant.now()): Duration { | ||
| val until = throttleUntil ?: return Duration.ZERO | ||
| val remaining = Duration.between(now, until) | ||
| return if (remaining.isNegative || remaining.isZero) Duration.ZERO else remaining | ||
| } | ||
|
|
||
| companion object { | ||
| internal val DEFAULT_RATE_LIMIT_DELAY: Duration = Duration.ofSeconds(10) | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
101 changes: 101 additions & 0 deletions
101
...test/kotlin/com/alibaba/opensandbox/sandbox/infrastructure/pool/PoolRateLimitStateTest.kt
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| /* | ||
| * Copyright 2026 Alibaba Group Holding Ltd. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.alibaba.opensandbox.sandbox.infrastructure.pool | ||
|
|
||
| import com.alibaba.opensandbox.sandbox.config.ConnectionConfig | ||
| import com.alibaba.opensandbox.sandbox.domain.pool.PoolConfig | ||
| import com.alibaba.opensandbox.sandbox.domain.pool.PoolCreationSpec | ||
| import org.junit.jupiter.api.Assertions.assertEquals | ||
| import org.junit.jupiter.api.Assertions.assertFalse | ||
| import org.junit.jupiter.api.Assertions.assertTrue | ||
| import org.junit.jupiter.api.Test | ||
| import java.time.Duration | ||
| import java.time.Instant | ||
| import java.util.concurrent.atomic.AtomicInteger | ||
|
|
||
| class PoolRateLimitStateTest { | ||
| private val now: Instant = Instant.parse("2026-08-14T00:00:00Z") | ||
|
|
||
| @Test | ||
| fun `missing retry after uses bounded default delay`() { | ||
| val state = PoolRateLimitState() | ||
|
|
||
| state.recordRateLimit(retryAfter = null, now = now) | ||
|
|
||
| assertTrue(state.isActive(now.plusSeconds(9))) | ||
| assertFalse(state.isActive(now.plusSeconds(10))) | ||
| } | ||
|
|
||
| @Test | ||
| fun `retry after is capped at transport ceiling`() { | ||
| val state = PoolRateLimitState() | ||
|
|
||
| state.recordRateLimit(retryAfter = Duration.ofMinutes(5), now = now) | ||
|
|
||
| assertTrue(state.isActive(now.plusSeconds(59))) | ||
| assertFalse(state.isActive(now.plusSeconds(60))) | ||
| } | ||
|
|
||
| @Test | ||
| fun `concurrent rate limits only extend throttle deadline`() { | ||
| val state = PoolRateLimitState() | ||
|
|
||
| state.recordRateLimit(retryAfter = Duration.ofSeconds(30), now = now) | ||
| state.recordRateLimit(retryAfter = Duration.ofSeconds(5), now = now.plusSeconds(1)) | ||
|
|
||
| assertEquals(Duration.ofSeconds(1), state.remainingDelay(now.plusSeconds(29))) | ||
| state.recordRateLimit(retryAfter = Duration.ofSeconds(60), now = now.plusSeconds(1)) | ||
| assertTrue(state.isActive(now.plusSeconds(60))) | ||
| assertFalse(state.isActive(now.plusSeconds(61))) | ||
| } | ||
|
|
||
| @Test | ||
| fun `rate limit suppresses warmups without blocking excess idle shrink`() { | ||
| val stateStore = InMemoryPoolStateStore() | ||
| val poolName = "rate-limited-shrink" | ||
| stateStore.putIdle(poolName, "idle-1") | ||
| stateStore.putIdle(poolName, "idle-2") | ||
| val config = | ||
| PoolConfig.builder() | ||
| .poolName(poolName) | ||
| .ownerId("owner-1") | ||
| .maxIdle(1) | ||
| .warmupConcurrency(1) | ||
| .stateStore(stateStore) | ||
| .connectionConfig(ConnectionConfig.builder().build()) | ||
| .creationSpec(PoolCreationSpec.builder().image("ubuntu:22.04").build()) | ||
| .build() | ||
| val rateLimitState = PoolRateLimitState() | ||
| rateLimitState.recordRateLimit(Duration.ofSeconds(30)) | ||
| val discarded = mutableListOf<String>() | ||
| val submitted = AtomicInteger(0) | ||
|
|
||
| PoolReconciler.runReconcileTick( | ||
| config = config, | ||
| stateStore = stateStore, | ||
| onDiscardSandbox = { discarded += it }, | ||
| reconcileState = ReconcileState(degradedThreshold = 3), | ||
| warmingCount = 0, | ||
| rateLimitState = rateLimitState, | ||
| submitWarmups = { submitted.addAndGet(it) }, | ||
| ) | ||
|
|
||
| assertEquals(1, discarded.size) | ||
| assertEquals(0, submitted.get()) | ||
| assertEquals(1, stateStore.snapshotCounters(poolName).idleCount) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
This branch makes HTTP 429 warmup failures stop contributing to degraded backoff only in Kotlin, but the same client-pool warmup exists in Python and Go and their reconcilers still count every thrown warmup error into
record_failures/recordFailures(checkedsdks/sandbox/python/src/opensandbox/_pool_reconciler.py:161-164andsdks/sandbox/go/pool_reconciler.go:271-282). Without the same handling or a documented platform constraint, server 429/Retry-After produces different quota/back-pressure semantics across SDKs.AGENTS.md reference: sdks/AGENTS.md:L122-L122
Useful? React with 👍 / 👎.