microsoft / RulesEngine

A Json based Rules Engine with extensive Dynamic expression support
https://microsoft.github.io/RulesEngine/
MIT License
3.47k stars 528 forks source link

RulesEngine lifetime and thread-safety #580

Open Makciek opened 4 months ago

Makciek commented 4 months ago

Hello I can't find it in docs nor in wiki, what's the advised lifetime of the RulesEngine object? Should I create it every time I need it and it will cache the compiled evaluator itself or should I create just one instance and cache it? If so is it thread-safe and I can reuse the same instance at the same time?

abbasc52 commented 4 months ago

@Makciek , it is recommended to use it as single instance to benefit from caching of compilation.

Also, it is thread safe, so you can use same instance at same time

vanwx commented 4 months ago

Hello, I'm curious about the thread safety of the localParam. If we aim to record the result of rule evaluation and assign it to a local parameter, could it be overwritten by other rule evaluations with different inputs?

For instance, let's say we construct a rule containing blacklist words. If the text contains any of these blacklist words, we wish to record the offensive word in the localParam and utilize it to generate the FailureMessage.

abbasc52 commented 4 months ago

@vanwx localParam is like a local variable in code, they do not share reference with any other rule or execution. So it should be completely thread safe

vanwx commented 4 months ago

@vanwx localParam is like a local variable in code, they do not share reference with any other rule or execution. So it should be completely thread safe

Yeah thanks @abbasc52 . I did write some tests and it's been safe. Also I found some examples on this repo use the localParam to store result state so it's good.