Top Description Inners Fields Constructors Methods
org.graalvm.polyglot

public final Class ResourceLimits

extends Object
Class Inheritance
Imports
java.util.function.Consumer, .Predicate

Represents resource limits configuration that is used to configure contexts. Resource limit instances are created using the builder and activated by enabling them for a context. All configured limits are applied per context instance. Resource limits may be reset. If a resource limit is triggered the context that triggered the limit will automatically be closed and can no longer be used to evaluate code.

The following resource limits are supported:

Statement Limit Example

ResourceLimits limits = ResourceLimits.newBuilder()
                      .statementLimit(500, null)
                  .build();
try (Context context = Context.newBuilder("js")
                           .resourceLimits(limits)
                       .build();) {
    try {
        context.eval("js", "while(true);");
        assert false;
    } catch (PolyglotException e) {
        // triggered after 500 iterations of while(true);
        // context is closed and can no longer be used
        assert e.isCancelled();
    }
}
Since
19.3
See Also
newBuilder()

Nested and Inner Type Summary

Modifier and TypeClass and Description
public class
ResourceLimits.Builder

A builder used to construct resource limits.

Field Summary

Modifier and TypeField and Description
private static final ResourceLimits
pack-priv final Object

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public static ResourceLimits.Builder
newBuilder()

Creates a new builder to construct ResourceLimits instances.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

EMPTYback to summary
private static final ResourceLimits EMPTY
receiverback to summary
pack-priv final Object receiver

Constructor Detail

ResourceLimitsback to summary
pack-priv ResourceLimits(Object receiver)

Method Detail

newBuilderback to summary
public static ResourceLimits.Builder newBuilder()

Creates a new builder to construct ResourceLimits instances.

Since
19.3
org.graalvm.polyglot back to summary

public final Class ResourceLimits.Builder

extends Object
Class Inheritance

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

Field Summary

Modifier and TypeField and Description
pack-priv Consumer<?>
pack-priv long
pack-priv Predicate<?>

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public ResourceLimits
build()

Builds the limit configuration object.

public ResourceLimits.Builder
onLimit(Consumer<ResourceLimitEvent> onLimit)

Notified when a resource limit is reached.

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.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

onLimitback to summary
pack-priv Consumer<?> onLimit
statementLimitback to summary
pack-priv long statementLimit
statementLimitSourceFilterback to summary
pack-priv Predicate<?> statementLimitSourceFilter

Constructor Detail

Builderback to summary
pack-priv Builder()

Method Detail

buildback to summary
public ResourceLimits build()

Builds the limit configuration object.

Annotations
@SuppressWarnings:unchecked
Since
19.3
See Also
Example Usage
onLimitback to summary
public ResourceLimits.Builder onLimit(Consumer<ResourceLimitEvent> onLimit)

Notified when a resource limit is reached. Default is null. May be set to null to disable events.

Since
19.3
statementLimitback to summary
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.

Annotations
@SuppressWarnings:hiding
Since
19.3
See Also
Example Usage