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

Method ConcurrentEval

cel/program.go:623–782  ·  view source on GitHub ↗

ConcurrentEval implements the Program interface.

(ctx context.Context, input any)

Source from the content-addressed store, hash-verified

621
622// ConcurrentEval implements the Program interface.
623func (p *prog) ConcurrentEval(ctx context.Context, input any) <-chan EvalResult {
624 resCh := make(chan EvalResult, 1)
625 if p == nil {
626 resCh <- EvalResult{Err: errors.New("program is nil")}
627 close(resCh)
628 return resCh
629 }
630 if ctx == nil {
631 resCh <- EvalResult{Err: errors.New("context can not be nil")}
632 close(resCh)
633 return resCh
634 }
635
636 go func() {
637 defer close(resCh)
638 frame, err := p.newAsyncFrame(ctx, input)
639 if err != nil {
640 resCh <- EvalResult{Err: err}
641 return
642 }
643 defer frame.Close()
644
645 var det *EvalDetails
646 // Ensure concurrent eval handles panic / recovery properly.
647 //
648 // The details are only captured and published here for the panic case. Every normal
649 // return path populates the details and sends them on the result channel itself, and
650 // the receiver may read them as soon as the send completes. Mutating det from this
651 // defer, which runs after the send, would race with those reads.
652 defer func() {
653 r := recover()
654 if r == nil {
655 return
656 }
657 det = captureCostTracker(det, frame)
658 switch t := r.(type) {
659 case interpreter.EvalCancelledError:
660 resCh <- EvalResult{EvalDetails: det, Err: t}
661 case cost.LimitExceededError:
662 resCh <- EvalResult{
663 EvalDetails: det,
664 Err: interpreter.EvalCancelledError{
665 Cause: interpreter.CostLimitExceeded,
666 Message: t.Message,
667 },
668 }
669 default:
670 resCh <- EvalResult{EvalDetails: det, Err: fmt.Errorf("internal error: %v", r)}
671 }
672 }()
673
674 // Completions are signaled to this channel as async calls finish. The asyncCallState
675 // fan-in also selects on ctx.Done(), so the sender will not leak if this loop returns early.
676 completions := make(chan int64, p.resolveCompletionBufferSize())
677 frame.SetCompletions(completions)
678
679 for {
680 var out ref.Val

Callers

nothing calls this directly

Calls 15

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

Tested by

no test coverage detected