Skip to content

Add unified e2b.Client and sandbox List API#3

Merged
eric642 merged 1 commit into
mainfrom
feat/unified-client-and-list
Jun 4, 2026
Merged

Add unified e2b.Client and sandbox List API#3
eric642 merged 1 commit into
mainfrom
feat/unified-client-and-list

Conversation

@eric642

@eric642 eric642 commented Jun 4, 2026

Copy link
Copy Markdown
Owner

背景

当前 sandbox 生命周期入口 Create/Connect/Kill 每次调用都会重新 resolve() 配置并重建控制面 REST 客户端 *apiclient.ClientConfig.HTTPClient 为 nil 时还会新建 *http.Client)。这不利于资源复用,也缺少一个"持有一次、统一管理所有功能"的客户端对象。此外 SDK 缺少列出 sandbox 的 List 接口。

精确说明:旧代码里 http.Client{Timeout}Transport 为 nil 时会回退到全局 http.DefaultTransport,所以底层连接池本就共享。本次真正的收益是:REST 客户端只构造一次跨调用复用、一个统一的持有对象、一个显式的传输层注入点,以及同一 Client 创建的所有 sandbox 共享该传输层。

改动

统一客户端 e2b.Clientclient.go

  • NewClient(cfg) 一次性解析 Config,构造一份共享 *http.Client 与控制面 REST 客户端,供其创建的所有 sandbox 复用。
  • 方法:Create / Connect / Kill / List / ListAllConfig() 返回深拷贝(map 字段用 maps.Clone,避免别名 Client 的活跃请求头、规避并发竞争)。

重构 sandbox.go

  • 自由函数 newSandbox(*Client).newSandbox,复用 Client 共享传输(envd 客户端仍按 sandbox 维度构造,但共享 httpCli)。
  • 包级 Create/Connect/Kill 保留为弱化包装器// Deprecated:),委托临时 Client,行为不变,向后兼容。

List 分页接口(list.go

  • SandboxPaginatorHasNext() / NextItems(ctx) / NextToken())+ 便捷 ListAll,对齐上游 Python/JS 的游标分页(读响应头 x-next-token)。
  • SandboxListOptions(Metadata / State / Limit / NextToken),metadata 双层编码与上游一致。
  • types.go 新增 sandboxInfoFromListed 转换器。

示例与文档

  • 7 个 example 迁移到 NewClient(...).Create(消除 SA1019 弃用 lint,示范推荐用法)。
  • README.md / doc.go 新增"统一客户端"段落和 scope 中的 List

测试

新增 client_test.go(10 个用例):客户端复用同一 apiCli/httpCli 指针、opts.Config 被忽略、Config() 深拷贝、List 两页分页、ListAll、metadata/state 参数、错误映射(401/429/500)、空响应、转换器。

go build ✅ · go vet ✅ · go test ./... ✅ · go test -race ✅ · golangci-lint run(v2.11.4)0 issues

经一轮 Codex review,已修复其指出的 Config() map 别名问题(P2)。

🤖 Generated with Claude Code

Introduce a unified Client that resolves Config once and shares a single
HTTP client and control-plane REST client across all lifecycle calls and
every Sandbox it creates, instead of rebuilding the REST client per call.

- client.go: Client{cfg, apiCli, httpCli}, NewClient, Config (deep-copies
  Headers/ExtraSandboxHeaders to avoid aliasing the live request maps), and
  Create/Connect/Kill methods carrying the lifecycle logic.
- sandbox.go: newSandbox becomes (*Client).newSandbox sharing the Client's
  transports; package-level Create/Connect/Kill are kept as deprecated thin
  wrappers that delegate to a one-shot Client (back-compat).
- list.go: SandboxListOptions, SandboxPaginator (HasNext/NextItems/NextToken)
  and ListAll, following upstream cursor pagination via the x-next-token
  response header; package-level List/ListAll convenience wrappers.
- types.go: sandboxInfoFromListed converter for ListedSandbox.
- client_test.go: client reuse, opts.Config ignored, Config deep-copy, List
  pagination, ListAll, metadata/state params, error mapping, empty body.
- examples/*: migrate to NewClient(...).Create (clears SA1019, shows new API).
- README.md, doc.go: document the unified client and List.

Build, vet, race tests, and golangci-lint v2 all pass.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@eric642
eric642 merged commit f9ea4b5 into main Jun 4, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant