Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions internal/cmd/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,20 @@ func UploadZipToService(ctx context.Context, zipBytes []byte) (string, error) {
}

// Step 3: Upload file to S3
uploadReq, err := http.NewRequestWithContext(ctx, uploadSession.PresignMethod, uploadSession.PresignURL, bytes.NewReader(zipBytes))
// The S3 PUT carries the whole zip, so the shared 30s client would cap
// uploads at ~14 Mbps. Give it its own deadline that scales with size.
uploadCtx, cancelUpload := context.WithTimeout(ctx, pkgutil.UploadTimeout(int64(len(zipBytes))))
defer cancelUpload()

uploadReq, err := http.NewRequestWithContext(uploadCtx, uploadSession.PresignMethod, uploadSession.PresignURL, bytes.NewReader(zipBytes))
if err != nil {
return "", fmt.Errorf("failed to create S3 upload request: %w", err)
}

uploadReq.Header.Set("Content-Type", uploadSession.PresignHeader.ContentType)
uploadReq.Header.Set("Content-Length", strconv.FormatInt(int64(len(zipBytes)), 10))

uploadResp, err := client.Do(uploadReq)
uploadResp, err := (&http.Client{}).Do(uploadReq)
if err != nil {
return "", fmt.Errorf("failed to upload to S3: %w", err)
}
Expand Down
9 changes: 7 additions & 2 deletions pkg/api/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,15 +410,20 @@ func (c *client) UploadZipToService(ctx context.Context, projectID string, servi
}

// Step 3: Upload file to S3
uploadReq, err := http.NewRequestWithContext(ctx, uploadSession.PresignMethod, uploadSession.PresignURL, bytes.NewReader(zipBytes))
// The S3 PUT carries the whole zip, so the shared 30s client would cap
// uploads at ~14 Mbps. Give it its own deadline that scales with size.
uploadCtx, cancelUpload := context.WithTimeout(ctx, util.UploadTimeout(int64(len(zipBytes))))
defer cancelUpload()

uploadReq, err := http.NewRequestWithContext(uploadCtx, uploadSession.PresignMethod, uploadSession.PresignURL, bytes.NewReader(zipBytes))
if err != nil {
return nil, fmt.Errorf("failed to create S3 upload request: %w", err)
}

uploadReq.Header.Set("Content-Type", uploadSession.PresignHeader.ContentType)
uploadReq.Header.Set("Content-Length", strconv.FormatInt(int64(len(zipBytes)), 10))

uploadResp, err := client.Do(uploadReq)
uploadResp, err := (&http.Client{}).Do(uploadReq)
if err != nil {
return nil, fmt.Errorf("failed to upload to S3: %w", err)
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/util/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,20 @@ import (
"fmt"
"io"
"net/http"
"time"
)

// UploadTimeout returns how long an upload of contentLength bytes is allowed
// to take: a 2-minute floor plus one second per 256 KiB of payload.
//
// http.Client.Timeout covers the entire request including body transfer, so a
// fixed timeout makes large uploads fail on slow links no matter how patient
// the user is. The 256 KiB/s floor is deliberately conservative (~2 Mbps);
// a 50 MiB zip gets ~5.3 minutes.
func UploadTimeout(contentLength int64) time.Duration {
return 2*time.Minute + time.Duration(contentLength/(256*1024))*time.Second
}
Comment on lines +18 to +20

// FormatHTTPError reads a non-2xx HTTP response and returns an error that
// preserves whatever the server actually said.
//
Expand Down
24 changes: 24 additions & 0 deletions pkg/util/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/zeabur/cli/pkg/util"
Expand Down Expand Up @@ -74,3 +75,26 @@ func TestFormatHTTPError(t *testing.T) {
})
}
}

func TestUploadTimeout(t *testing.T) {
t.Parallel()

tests := []struct {
name string
size int64
want time.Duration
}{
{"zero size gets the 2min floor", 0, 2 * time.Minute},
{"tiny payload still gets the floor", 1024, 2 * time.Minute},
{"1 MiB adds 4s over the floor", 1 << 20, 2*time.Minute + 4*time.Second},
{"50 MiB scales to ~5.3 min", 50 << 20, 2*time.Minute + 200*time.Second},
{"1 GiB scales to ~70 min", 1 << 30, 2*time.Minute + 4096*time.Second},
Comment on lines +87 to +91
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tc.want, util.UploadTimeout(tc.size))
})
}
}