ConcurrentEval implements the Program interface.
(ctx context.Context, input any)
| 485 | |
| 486 | // ConcurrentEval implements the Program interface. |
| 487 | func (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 | } |
nothing calls this directly
no test coverage detected