Event driven architecture ... endless loop

I have an event driven architecture where A is waiting for change from B and B is waiting for change from C and C is waiting for change from A, forming a loop.

Now, if B changes, then A fires an event in C that fires in B, which fires before A, which fires before C ... ad infinitum.

I can change my program right now so as not to contain this cycle, but I am worried that I may later be in the corner when I cannot. How to avoid such events when developing event-based systems?

+7
design events architecture event-driven-design
source share
5 answers

The cyclic dependencies are really bad. I had to write your message in terms of A, B and C before it even made sense to me. I think you should get rid of this. If you put yourself in a corner, this is probably much better than the problems you may encounter with circular dependencies.

You too can avoid this. A, B and C are really closely related. I think you need to reconsider your responsibilities. Perhaps there is a common element D that takes away a lot of your stress.

Something else that comes to mind is architectural layering . If you can add A over B and require anyone talking to B to go through A and down through the layers, you can give yourself an easier time. Again, I know little about your problem, so these are just broad offers.

One last option and my least favorite is to pass a message between each of the three components. Each visit requires each component to add a message to the message that it saw. Then the next component for receiving the message contains information about who saw it. View as a registration sheet. But then again, the least beloved. Try something else first.

Good luck

-3
source share

Mark your addictions. There should be no cycles. Looping dependencies is a good reason to reorganize your code.

They can also cause deadlocks if you need another reason to try to avoid them.

+3
source share

Everyone here seems to say that cyclic dependencies are bad. This is correct in a way, and I try to avoid static cyclic dependencies in almost all costs. You can do this with inverse control, as described in this blog: http://blog.schauderhaft.de/2011/07/17/breaking-dependency-cylces/

But what you are describing is not required for static cyclic dependency, but at runtime. I'm not quite sure, but I think it is more or less impossible to avoid circular dependencies at runtime. But, of course, this should not lead to endless cycles. To fix those, I see two and a half options

Hack first

Make sure that each event triggered by another event has a link to the original event (or significant information about it, as an identifier). When you process an event, make sure that it does not come from you.

Pro: easy to implement; completely prevents recursion

The other half of the hack

If you work synchronously, you can set the firingEvent flag before and reset it after. Ignore events that occur during the installation of firingEvent .

Pro: even easier to implement; completely prevents recursion when working in a single thread

Semantic rich decision

I am convinced that the event that A fires on some external trigger, and the event that fires A, because the fire C are really two different events, or all three events are actually only one that can arise from another unknown source D. Or something like that. It is impossible to speak without information about what A, B and C are and what events they shoot. If you find the right events, the loop will disappear.

Pro: The design will be cleaner and contain more information.

+2
source share

How to avoid such events when developing event-based systems?

  • Raise an event only when the state of the object really changes.

  • Prevent the state of an object from changing when an event is created.

+2
source share

I think this is a good question. Unfortunately, I do not have a complete answer, but this post has a couple of good points:

how to avoid endless loop in observer pattern?

I don't think the answer is to avoid circular dependencies, as others have suggested. (Well, it depends on your definition of "circular dependency.") A language like Java will use interfaces to minimize circular type dependencies at compile time, which is often a good idea. For example, the presentation class in the MVC template is independent of your application, it only knows about interfaces with names like ValueChangedListener , ClickListener , etc. But this does not eliminate cyclic connections between objects at runtime, which can lead to event loops.

As mentioned in another related post, some loops stop in the user interface tools because the view will not trigger a “modified” event if the controller or model “sets” the view value to something equal to its current value. But in other cases, for example, when creating a custom representation of a more complex part of the data, it is impossible to calculate the equality of current and new data.

0
source share

All Articles