-
Notifications
You must be signed in to change notification settings - Fork 263
feat(sandbox): add idempotent retry for sandbox creation and connection #472
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b33fc94
feat(sandbox): add idempotent retry for sandbox creation and connection
DROWNING2003 8706773
fix(sandbox): add backoff between retries and fail on non-retryable e…
DROWNING2003 dc8bd76
fix(sandbox): retry network errors wrapped as SandboxError
DROWNING2003 0da1669
fix(sandbox): add 'failed to resolve' to retryable patterns, tighten …
DROWNING2003 1454409
chore: restore compatibility comment for endpoint env vars in .env.ex…
DROWNING2003 5ae06a8
fix(sandbox): validate max_retries, propagate cause in SandboxError, …
DROWNING2003 bbee453
fix(sandbox): forward max_retries in Sandbox.create and improve idemp…
DROWNING2003 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
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,41 @@ | ||
| #!/usr/bin/env python | ||
| # -*- coding: utf-8 -*- | ||
| """幂等重试示例:同一幂等键连调两次 Create,验证返回同一沙箱。""" | ||
| import os | ||
| import sys | ||
| import time | ||
|
|
||
| from qiniu.services.sandbox import Sandbox | ||
|
|
||
| API_KEY = os.getenv('QINIU_SANDBOX_API_KEY') or os.getenv('QINIU_API_KEY') or os.getenv('E2B_API_KEY') | ||
| if not API_KEY: | ||
| print('请设置 QINIU_SANDBOX_API_KEY 环境变量') | ||
| sys.exit(1) | ||
|
|
||
| ENDPOINT = os.getenv('QINIU_SANDBOX_ENDPOINT') or os.getenv('QINIU_SANDBOX_API_URL') | ||
|
|
||
| idempotency_key = 'sdk-example-{}'.format(int(time.time())) | ||
| print('幂等键: {}'.format(idempotency_key)) | ||
|
|
||
| first_sandbox = Sandbox.create( | ||
| template='base', timeout=300, endpoint=ENDPOINT, api_key=API_KEY, | ||
| idempotency_key=idempotency_key, | ||
| ) | ||
| second_sandbox = None | ||
| try: | ||
| print('第一次创建: {}'.format(first_sandbox.sandbox_id)) | ||
| second_sandbox = Sandbox.create( | ||
| template='base', timeout=300, endpoint=ENDPOINT, api_key=API_KEY, | ||
| idempotency_key=idempotency_key, | ||
| ) | ||
| print('第二次创建: {}'.format(second_sandbox.sandbox_id)) | ||
| if first_sandbox.sandbox_id != second_sandbox.sandbox_id: | ||
| raise RuntimeError( | ||
| '幂等重试验证失败:两次创建返回不同沙箱: {} vs {}'.format( | ||
| first_sandbox.sandbox_id, second_sandbox.sandbox_id)) | ||
| print('幂等重试验证通过:两次创建返回同一沙箱') | ||
| finally: | ||
| if second_sandbox is not None and second_sandbox.sandbox_id != first_sandbox.sandbox_id: | ||
| second_sandbox.kill() | ||
| first_sandbox.kill() | ||
| print('沙箱已清理') |
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
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
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 |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| # -*- coding: utf-8 -*- | ||
| import os | ||
| import time | ||
|
|
||
| import pytest | ||
|
|
||
| from qiniu.services.sandbox import ( | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.