Files
ai_academy/internal/sandbox/e2b_rpc_timeout.go
T
runny 7ec21f35b0 chore: 初始化仓库,导入项目基线代码
Academy v0.8.3 完整代码基线:Go 后端(internal/cmd/cli/mcp-server/docreader)、
Vue 前端(frontend)、数据库迁移(migrations)、部署编排(docker/helm/deploy)与文档。
今日开发的培训推送管理改动随后单独提交。
2026-11-16 18:12:23 +08:00

36 lines
1.1 KiB
Go

package sandbox
import (
"context"
"net/http"
"time"
)
// Keep the HTTP budget on ordinary requests (including their response bodies).
// Start and Connect stream for the command's lifetime and are bounded by the
// SDK's execution context instead. Parent cancellation is preserved throughout.
type e2bRPCTimeoutTransport struct {
next http.RoundTripper
timeout time.Duration
}
func (t *e2bRPCTimeoutTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if t.timeout <= 0 || req.URL.Path == "/process.Process/Start" || req.URL.Path == "/process.Process/Connect" || isFilesystemContentRPC(req.URL.Path) {
return t.next.RoundTrip(req)
}
ctx, cancel := context.WithTimeout(req.Context(), t.timeout)
response, err := t.next.RoundTrip(req.Clone(ctx))
if err != nil {
cancel()
return nil, err
}
response.Body = &cancelOnCloseBody{ReadCloser: response.Body, cancel: cancel}
return response, nil
}
func (t *e2bRPCTimeoutTransport) CloseIdleConnections() {
if closer, ok := t.next.(interface{ CloseIdleConnections() }); ok {
closer.CloseIdleConnections()
}
}