What is the recommended way to use Graal.js in a multi-threaded application (such as per servlet request)? We are using Graal.js like this
jsContext = Context.newBuilder("js").allowAllAccess(true).build();
bindings = jsContext.getBindings("js");
jsContext.eval("js", jsCodeString);
Should we have a unique Context/binding for each executing thread. This can be accomplished through a pool of Context/Binding pairs or by using threadlocal. Is this the proper way to do this?
You should not access one Context
from several threads at the same time. So, the solutions that you propose (a pool of Context
s or thread-local Context
s) are valid ways to do that.
graalvm/graaljs
repository contains some threading-related examples. You can see the usage of a thread-local Context
there (in ExecutorsTest).
FYI: There is a blog-post about multi-threading with Graal JavaScript that may clarify further questions that you may have.