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

Method ConcurrentEval

cel/program.go:487–622  ·  view source on GitHub ↗

ConcurrentEval implements the Program interface.

(ctx context.Context, input any)

Source from the content-addressed store, hash-verified

485
486// ConcurrentEval implements the Program interface.
487func (p *prog) ConcurrentEval(ctx context.Context, input any) <-chan EvalResult {
488 resCh := make(chan EvalResult, 1)
489 if ctx == nil {
490 resCh <- EvalResult{Err: errors.New("context can not be nil")}
491 close(resCh)
492 return resCh
493 }
494
495 go func() {
496 defer close(resCh)
497 // Ensure concurrent eval handles panic / recovery properly
498 defer func() {
499 if r := recover(); r != nil {
500 switch t := r.(type) {
501 case interpreter.EvalCancelledError:
502 resCh <- EvalResult{Err: t}
503 default:
504 resCh <- EvalResult{Err: fmt.Errorf("internal error: %v", r)}
505 }
506 }
507 }()
508
509 frame, err := p.newAsyncFrame(ctx, input)
510 if err != nil {
511 resCh <- EvalResult{Err: err}
512 return
513 }
514 defer frame.Close()
515
516 // Completions are signaled to this channel as async calls finish. The asyncCallState
517 // fan-in also selects on ctx.Done(), so the sender will not leak if this loop returns early.
518 completions := make(chan int64, p.resolveCompletionBufferSize())
519 frame.SetCompletions(completions)
520
521 for {
522 var out ref.Val
523 var det *EvalDetails
524
525 if p.observable != nil {
526 det = &EvalDetails{}
527 out = p.observable.ObserveExec(frame, func(observed any) {
528 switch o := observed.(type) {
529 case interpreter.EvalState:
530 det.state = o
531 case *interpreter.CostTracker:
532 det.costTracker = o
533 }
534 })
535 } else {
536 out = p.interpretable.Exec(frame)
537 }
538
539 // Communicate errors quickly.
540 if types.IsError(out) {
541 var err error = out.(*types.Err)
542 if errors.Is(err, interpreter.InterruptError{}) {
543 err = fmt.Errorf("%w: %w", err, context.Cause(ctx))
544 }

Callers

nothing calls this directly

Calls 15

newAsyncFrameMethod · 0.95
IsErrorFunction · 0.92
NewMethod · 0.80
CloseMethod · 0.80
SetCompletionsMethod · 0.80
ObserveExecMethod · 0.80
HasUnknownFunctionMethod · 0.80
AsyncCallMethod · 0.80
ErrMethod · 0.80
ActiveAsyncCallsMethod · 0.80

Tested by

no test coverage detected