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)
| 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. |
| 331 | func (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. |
| 386 | func (acs *asyncCallState) matches(id int64, function, overload string, args []ref.Val) bool { |
no test coverage detected