ConnectorIO / connectorio-addons

Dedicated repository for openHAB software extensions maintained by ConnectorIO.
Apache License 2.0
18 stars 11 forks source link

Eliminate use o ThreadLocal in chained profiles #85

Closed splatch closed 1 month ago

splatch commented 1 month ago

The chained profile concept outlined in opensmarthouse/opensmarthouse-core#35 which was supplied in this repository have been implemented in 2021 and quickly redesigned in 2022. First and redesigned implementation embedded ThreadLocal to pass invocation which initially worked well. However, this design have a large drawback of retaining invocation state within a shared object. The main point of it was really to separate creation and invocation time, which could not be shared.

Looking closer at the issue of sharing state, it actually can be bridged between creation and invocation time without ThreadLocal and major multi threading risks. When invocation chain is initialized, chain elements are initialized sequentially. It means that they know own position and can reference chain. In order to simplify invocation, we can also swap use of iterator and rely on chain element index manipulation (+1 from handler to item, -1 from item to handler), in order to avoid direct references.

While I know it sounds very complex, actual change (despite of its size) will result in much simpler code providing the same functionality.