Skip to content

Commit c2dede3

Browse files
authored
fix: support custom timeout when bundling functions (#4120)
1 parent be5fde2 commit c2dede3

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

pkg/function/bundle.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os/exec"
1111
"path/filepath"
1212
"strings"
13+
"time"
1314

1415
"github.com/andybalholm/brotli"
1516
"github.com/go-errors/errors"
@@ -19,13 +20,24 @@ import (
1920
type nativeBundler struct {
2021
tempDir string
2122
fsys fs.FS
23+
timeout time.Duration
2224
}
2325

24-
func NewNativeBundler(tempDir string, fsys fs.FS) EszipBundler {
25-
return &nativeBundler{
26+
func NewNativeBundler(tempDir string, fsys fs.FS, opts ...func(*nativeBundler)) EszipBundler {
27+
b := &nativeBundler{
2628
tempDir: tempDir,
2729
fsys: fsys,
2830
}
31+
for _, apply := range opts {
32+
apply(b)
33+
}
34+
return b
35+
}
36+
37+
func WithTimeout(timeout time.Duration) func(*nativeBundler) {
38+
return func(b *nativeBundler) {
39+
b.timeout = timeout
40+
}
2941
}
3042

3143
var (
@@ -48,6 +60,11 @@ func (b *nativeBundler) Bundle(ctx context.Context, slug, entrypoint, importMap
4860
args = append(args, "--static", staticFile)
4961
}
5062
args = append(args, BundleFlags...)
63+
if b.timeout > 0 {
64+
timeoutCtx, cancel := context.WithTimeout(ctx, b.timeout)
65+
defer cancel() // release resources if command exits before timeout
66+
ctx = timeoutCtx
67+
}
5168
cmd := exec.CommandContext(ctx, edgeRuntimeBin, args...)
5269
cmd.Stderr = os.Stderr
5370
cmd.Stdout = os.Stdout

0 commit comments

Comments
 (0)