I am trying to use GraphViz 2.40.1 under Linux in a REST service that lays out graphs. The service is a Java Spring Boot application. My current approach is to load shared libraries into my JVM, and call native code via JNI, using functions from libraries cgraph and gvc. I'd like to do everything in-memory and avoid file I/O. But I have read the following caveat in the GraphViz lib guide, printed in bold face at the end of section 1: "N.B. Using Graphviz as a library is not thread-safe."
I am seeking help about the consequences I should draw from that statement. No details are given. For example, I can imagine that functions that keep mutable state about errors that have occurred in graph parsing are not thread-safe, but I'm not using these. I only use the following functions: agmemread
and agclose
from cgraph, and gvContext
, gvParseArgs
, gvLayout
, gvRenderData
, gvFreeRenderData
, gvFreeLayout
, gvFreeContext
from gvc. I am caching nothing in Java, local vars and method params only. Would such a use of the library be thread-safe?
If not, does the non-thread-safety only affect uses of single functions, not across functions? So would it be enough to make my Java native methods static synchronized
? Or would I have to synchronize on every REST request?
Alternatively, I could fork a new OS process for each request and do file operations with GraphViz's dot program with Runtime.exec().
What approach would scale best?