[erlang-questions] Question about behaviours in Erlang/OTP
Mon Jul 14 03:58:21 CEST 2014
# Yves S. Garret 2014-07-13:
> I'm looking at some code that was written where a user uses a behaviour of
> gen_server and then uses a process_flag(trap_exit, true).
Nothing wrong with that, they probably just need detailed control over
child process' faults. But you don't say what the code is really up to.
> To me, this seems like a supervisor behaviour would work better.
Supervisors work well at controlling the relatively small & long-lived
"spine" of a complex control system. The lower you go the more subleties
emerge, thus the more low-level tools you need to tame them; generally
speaking. There's only so far pre-baked patterns will go, and that's
fine as they were never meant to go much further.
> What would be a better approach in this case?
To understand the functional requirements at hand and write the simplest
possible code that complies with them. You're allowed to cheat on both
accounts as long as the customer couldn't possibly tell in live operation.
> To declare this module a supervisor?
What good would a supervisor do in a case where you have one process
spawning jobs supplied on request by a subordinate worker process,
whilst limiting job issue rate against configured maximum, whilst
engaging in exponential backoff of said activity when job fault
rate breaches other configured threshold, whilst merely temporarily
slowing down job issue rate if the faults are of certain kinds; all
the time making sure the same logical job isn't active more than
once at a time?
That was a real use case BTW, a simplified form thereof. And that is
why you have all those low-level tools available (like trapping exits,
various ways of spawning processes, monitors etc etc). Use them. ;-)
> Is it possible (sensible? logical?) to give the same module gen_server
> and supervisor behaviours?
One module can adhere to multiple behaviors in principle, though it is
only very rarely sensible, and this particular combination certainly
doesn't look like one of those cases (it probably wouldn't even work,
considering 'supervisor' already is a 'gen_server' if memory serves).
PS: Oh no, now hipster kids will be all over me. Whoopsy daisy. :-)
More information about the erlang-questions