Class ResourceLimits.Builder

java.lang.Object
org.graalvm.polyglot.ResourceLimits.Builder
Enclosing class:
ResourceLimits

public final class ResourceLimits.Builder extends Object
A builder used to construct resource limits. Builder instances are not thread-safe and may not be used from multiple threads at the same time.
Since:
19.3
  • Method Details

    • statementLimit

      public ResourceLimits.Builder statementLimit(long limit, Predicate<Source> sourceFilter)
      Specifies the maximum number of statements a context may execute until the onLimit event is notified and the context will be closed. After the statement limit was triggered for a context, it is no longer usable and every use of the context will throw a PolyglotException that returns true for PolyglotException.isCancelled(). The statement limit is independent of the number of threads executing and is applied per context. Invoking this method multiple times overwrites previous statement limit configurations. If the statement limit is exceeded then the onLimit listener is notified.

      By default there is no statement limit applied. The limit may be set to 0 to disable it. In addition to the limit a source filter may be set to indicate for which sources the limit should be applied. If the source filter is null then it will be applied to all internal and public sources. The provided limit must not be negative otherwise an IllegalArgumentException is thrown. If a shared engine is used then the same source filter instance must be used for all contexts of an engine. Otherwise an IllegalArgumentException is thrown when the context is built. The limit itself may vary between contexts.

      The statement limit is applied to the context and all inner contexts it spawns. Therefore, new inner contexts cannot be used to exceed the statement limit.

      Note that attaching a statement limit to a context reduces the throughput of all guest applications with the same engine. The statement counter needs to be updated with every statement that is executed. It is recommended to benchmark the use of the statement limit before it is used in production.

      Note that the complexity of a single statement may not be constant time depending on the guest language. For example, statements that execute JavaScript builtins, like Array.sort, may account for a single statement, but its execution time is dependent on the size of the array. The statement count limit is therefore not suitable to perform time boxing and must be combined with other more reliable measures.

      Since:
      19.3
      See Also:
    • onLimit

      Notified when a resource limit is reached. Default is null. May be set to null to disable events.
      Since:
      19.3
    • build

      public ResourceLimits build()
      Builds the limit configuration object.
      Since:
      19.3
      See Also: