Is it okay to cast messages to a process that may or may not be running?

I have a monitoring solution that uses genservers that we can turn on and off when needed (We only want to monitor for debugging purposes). However, I have integrated code that will always cast messages to those processes regardless of if they are running. While this seems to work just fine and appears to be a no-op, something does not feel right about it. Is there a better way to do this, or is this an acceptable solution?

The elixir logger lets you wrap logging expressions in anonymous functions, which are only executed if the logging level does actually allow for the logging to happen. You might be able to adopt that pattern.

1 Like

Depending on the use-case it is entirely fine to do in my opinion. :slight_smile:

Again, that depends on the use-case. ^.^

I think the main downside is the overhead of sending a message that will not be read. But the upside is increased decoupling between the two systems.

That sounds a lot like good old gen_event:

It’s made for that kind of use case where you have handlers (e.g. for logging) that can be dynamically added or removed. The main advantage would be less coupling, the processes that send monitoring data don’t need to know about the monitoring module’s interface, they only talk to the gen_event. This can help for automated tests and break the dependency if you want to have the two modules in separate apps in an umbrella for instance. gen_event has some warts but they shouldn’t matter much for your use case.

If what you’re doing is simple and does the job though - no need to overthink it. It’s fine to cast messages into a blackhole :slight_smile:

1 Like