MCPcopy Create free account
hub / github.com/cel-expr/cel-go / launch

Method launch

interpreter/async.go:331–383  ·  view source on GitHub ↗

launch returns a call's cached result, or starts the call (subject to the launch limiter) and returns an Unknown referencing its callID while the result is pending. Admission control: when a concurrency semaphore is configured, a launch slot is reserved with a non-blocking send. If no slot is free

(ctx context.Context, acs *asyncCallState, observer AsyncObserver)

Source from the content-addressed store, hash-verified

329// The slot is held by the launched goroutine and released when it exits, so the number of live
330// async goroutines is bounded by the semaphore capacity.
331func (t *asyncCallStateTracker) launch(ctx context.Context, acs *asyncCallState, observer AsyncObserver) ref.Val {
332 if res := acs.ResultOrUnknown(); res != nil {
333 return res
334 }
335 gate := acs.gate
336 if !gate.TryAcquire() {
337 return types.NewUnknown(acs.callID, nil)
338 }
339 acs.mu.Lock()
340 if acs.started || acs.result != nil {
341 // Defensive: the evaluator is single-threaded so this should not happen, but if it does,
342 // return the reserved slot rather than leak it.
343 acs.mu.Unlock()
344 gate.Release()
345 return types.NewUnknown(acs.callID, nil)
346 }
347 acs.started = true
348 acs.mu.Unlock()
349
350 if observer != nil {
351 observer.OnCallStarted(acs.callID, acs.function, acs.overload, acs.argVals)
352 }
353 go func() {
354 defer func() {
355 if observer != nil {
356 observer.OnCallFinished(acs.callID, acs.function, acs.overload, acs.ResultOrUnknown())
357 }
358 gate.Complete(ctx, acs.callID)
359 }()
360
361 ch := acs.impl(ctx, acs.argVals...)
362 // Early terminate with a CEL error when an implementation returns an empty channel.
363 if ch == nil {
364 acs.SetResult(types.NewErrFromString(
365 fmt.Sprintf("function %s returned an empty channel", acs.function)))
366 return
367 }
368 // Wait for the async computation to finish or for the context to be cancelled.
369 select {
370 case r, ok := <-ch:
371 if !ok {
372 acs.SetResult(types.NewErrFromString(
373 fmt.Sprintf("function %s returned an empty channel", acs.function)))
374 return
375 }
376 acs.SetResult(r)
377 case <-ctx.Done():
378 // Evaluation context cancelled before the async operation completed.
379 acs.SetResult(types.WrapErr(context.Cause(ctx)))
380 }
381 }()
382 return types.NewUnknown(acs.callID, nil)
383}
384
385// matches reports whether two call states refer to the same function, overload, and arguments.
386func (acs *asyncCallState) matches(id int64, function, overload string, args []ref.Val) bool {

Callers 1

Calls 10

NewUnknownFunction · 0.92
NewErrFromStringFunction · 0.92
WrapErrFunction · 0.92
ResultOrUnknownMethod · 0.80
TryAcquireMethod · 0.80
CompleteMethod · 0.80
SetResultMethod · 0.80
OnCallStartedMethod · 0.65
OnCallFinishedMethod · 0.65
ReleaseMethod · 0.45

Tested by

no test coverage detected