Skip to content

Commit

Permalink
fix: jsonnet pool timeouts
Browse files Browse the repository at this point in the history
The fixed 1 second timeout now only applies to the Jsonnet evaluation step,
but not the the spawning of the process which can occasionally take longer.

Additionally, we now warm the pool asynchronously on construction.
  • Loading branch information
alnr committed Jul 30, 2024
1 parent a9ee431 commit 56aace8
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions jsonnetsecure/jsonnet_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ func NewProcessPool(size int) Pool {
if err != nil {
panic(err) // this should never happen, see implementation of puddle.NewPool
}
for range size {

Check failure on line 68 in jsonnetsecure/jsonnet_pool.go

View workflow job for this annotation

GitHub Actions / Run Tests and Lint Code

cannot range over size (variable of type int) (typecheck)
// warm pool
go pud.CreateResource(context.Background())
}
go func() {
for {
time.Sleep(10 * time.Second)
Expand Down Expand Up @@ -141,12 +145,19 @@ func newWorker(ctx context.Context) (_ worker, err error) {
errs := make(chan string)
go scan(errs, stderr)

return worker{
w := worker{
cmd: cmd,
stdin: in,
stdout: out,
stderr: errs,
}, nil
}
_, err = w.eval(ctx, []byte("{}")) // warm up
if err != nil {
w.destroy()
return worker{}, errors.Wrap(err, "newWorker: warm up failed")
}

return w, nil
}

func (w worker) destroy() {
Expand Down Expand Up @@ -182,9 +193,6 @@ func (vm *processPoolVM) EvaluateAnonymousSnippet(filename string, snippet strin
ctx, span := tracer.Start(vm.ctx, "jsonnetsecure.processPoolVM.EvaluateAnonymousSnippet", trace.WithAttributes(attribute.String("filename", filename)))
defer otelx.End(span, &err)

// TODO: maybe leave the timeout to the caller?
ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
defer cancel()

params := vm.params
params.Filename = filename
Expand All @@ -201,6 +209,8 @@ func (vm *processPoolVM) EvaluateAnonymousSnippet(filename string, snippet strin
return "", errors.Wrap(err, "jsonnetsecure: acquire")
}

ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
defer cancel()
result, err := worker.Value().eval(ctx, pp)
if err != nil {
worker.Destroy()
Expand Down

0 comments on commit 56aace8

Please sign in to comment.