View Source WithTimeout (WithTimeout v0.1.0)

Both total and time limited evaluation of expressions

Link to this section Summary

Types

Evaluation result payload

Evaluation result payload in case of exception during the evaluation

Evaluation result payload in case of backing task termination

Evaluation result payload in case of evaluation timeout

Evaluation time limit in milliseconds

Any anonymous function without arguments

Functions

Evaluates "lazy expression" (anonymous function without arguments) within the passed time interval.

Link to this section Types

@type evaluation_error() ::
  evaluation_exception() | evaluation_termination(term()) | evaluation_timeout()

Evaluation result payload

Link to this type

evaluation_exception()

View Source
@type evaluation_exception() :: {:exception, Exception.t(), Exception.stacktrace()}

Evaluation result payload in case of exception during the evaluation

@type evaluation_options() :: [
  within_milliseconds: evaluation_timeout_in_milliseconds(),
  with_evaluation_shutdown_timeout_in_milliseconds:
    evaluation_shutdown_timeout_in_milliseconds()
]
Link to this type

evaluation_shutdown_timeout_in_milliseconds()

View Source
@type evaluation_shutdown_timeout_in_milliseconds() :: :brutal_kill | timeout()

Task.shutdown/2 shutdown timeout backing evaluation

Link to this type

evaluation_termination(a)

View Source
@type evaluation_termination(a) :: {:exit, reason :: a}

Evaluation result payload in case of backing task termination

@type evaluation_timeout() :: :timeout

Evaluation result payload in case of evaluation timeout

Link to this type

evaluation_timeout_in_milliseconds()

View Source
@type evaluation_timeout_in_milliseconds() :: pos_integer()

Evaluation time limit in milliseconds

@type lazy_expression(a) :: (() -> a)

Any anonymous function without arguments

Link to this section Functions

Link to this function

evaluate(lazy_expression, evaluation_options)

View Source
@spec evaluate(lazy_expression(any()), evaluation_options()) ::
  {:error, evaluation_error()} | {:ok, evaluated_expression :: any()}

Evaluates "lazy expression" (anonymous function without arguments) within the passed time interval.

The main difference from basic Task facility is that the evaluation is total, so you get the result even if the expression raises. The expression evaluation task instead of being linked to the caller is supervised by Task.Supervisor spawned under the hood each time you call the function and linked to the caller process.

Thus, you get "supervised" expression evaluation:

  • caller process shutdown causes expression evaluation task to shut down (same as Task.async/1, but what's missing in Task.Supervisor.async_nolink/3 in general);
  • expression evaluation task shutdown does not cause the caller process to shut down.

If the expression does not terminate within the passed time interval, its evaluation will be terminated, so keep this in mind, if it has side effects (effectful expression evaluation is not reproducible in general).

Optional evaluation shutdown timeout backs underlying Task.shutdown/2, so all acquired by lazy_expression resources (e.g. linked processes) can be gracefully terminated or released. Its default value is 5000ms.