Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multithreading usage of template rendering #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/main/java/de/neuland/pug4j/template/PugTemplate.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package de.neuland.pug4j.template;

import java.io.Writer;

import de.neuland.pug4j.Pug4J.Mode;
import de.neuland.pug4j.compiler.Compiler;
import de.neuland.pug4j.exceptions.PugCompilerException;
import de.neuland.pug4j.expression.ExpressionHandler;
import de.neuland.pug4j.lexer.token.Doctypes;
import de.neuland.pug4j.model.PugModel;
import de.neuland.pug4j.parser.node.Node;
import java.io.Writer;
import org.apache.commons.lang3.StringUtils;

public class PugTemplate {
Expand All @@ -22,9 +21,27 @@ public class PugTemplate {
private String doctypeLine;

public void process(PugModel model, Writer writer) throws PugCompilerException {
//
// Create a new `template` using data from this instance.
// We need it to workaround the multithreading corruption of `doctypeLine`
// as it's modified during `compiler.compile()` call and it can happen simultaneously
// for mutltiple threads rendering the same template.
//
// The fix looks somewhat ugly but it fixes the issue and shouldn't impact performance.
// The proper fix requires bigger changes to the whole template-compiler interaction,
// preferable using a contructor for members initialization and making all members final.

PugTemplate template = new PugTemplate();
template.setTemplateLoader(templateLoader);
template.setExpressionHandler(expressionHandler);
template.setPrettyPrint(prettyPrint);
template.setRootNode(rootNode);
template.terse = terse;
template.xml = xml;

Compiler compiler = new Compiler(rootNode);
compiler.setPrettyPrint(prettyPrint);
compiler.setTemplate(this);
compiler.setTemplate(template);
compiler.compile(model, writer);
}

Expand Down