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

Thread Safety Improvements #36

Open
groue opened this issue Oct 25, 2016 · 2 comments
Open

Thread Safety Improvements #36

groue opened this issue Oct 25, 2016 · 2 comments

Comments

@groue
Copy link
Owner

groue commented Oct 25, 2016

One does not generally trigger threading issues with GRMustache:

// Thread-safe
let template = try Template(path: "/path/to/templates/document.mustache")
let rendering = try template.render(data)

But should one go down one level and expose Template Repositories to make good use of their configurability, template caching, or support for absolute paths to partials, threading issues may arise:

let templatesURL = URL(fileURLWithPath: "/path/to/templates")
let repository = TemplateRepository(baseURL: templatesURL)

// Not thread-safe (1)
repository.configuration.register(StandardLibrary.eachFilter, forKey: "each") 

// Not thread-safe (2)
let template = try repository.template(named: "document")

// Thread-safe
let rendering = try template.render(data)
  1. The configuration of a template repository is not protected against concurrent accesses: should two threads modify or read the configuration of a single repository at the same time, the program may behave in an unexpected way, or crash.

    This issue is not a big deal: template repository configuration is supposed to happen once, before any template is loaded from the repository. We can just update the documentation and add a warning.

  2. Template loading is not thread-safe: should two threads load a template at the same time, the program may behave in an unexpected way, or crash.

    Now this is a real issue, which needs to be addressed. Basically the problem lies in the fact that a template repository's cache and locked configuration are not protected against concurrent uses.

    The cache contains Mustache template ASTs, and prevents GRMustache from parsing again and again the same templates and partials. It also gives support for recursive templates. The locked configuration is a copy of the repository's configuration made as soon as the repository loads its first template. Once the configuration locked, further updates to the repository's configuration have no effect.

@groue
Copy link
Owner Author

groue commented Oct 25, 2016

CC @vadimeisenbergibm since this topic may be of some interest for Kitura

@vadimeisenbergibm
Copy link

@groue Please note that for Kitura only the following functionality is exposed in Kitura-MustacheTemplateEngine:

let template = try Template(path: filePath)
return try template.render(with: Box(context))

Since these two lines do not change GRMoustache configuration, they cannot cause concurrency issues, can they?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants