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