I've been thinking about custom process attributes

Sometimes when working with specific concepts in mind, I tend to put certain ‘attributes’ into the Process Dictionary - sometimes these are things like ‘process_type’ or ‘process_name’ where these are then used elsewhere for monitoring/reporting or to use as an indicator of how a process differs from the others under the same supervisor

Take for instance an Erlang node operating in a clustered environment;

Each of these nodes has their own cluster controller gen_server to handle interop between nodes - what would be the best way to quickly indicate which is the current ‘master’ node?

arbitrarily creating an atom to register in the global dict seems overly complex and doesn’t fully handle the node going down unless the registered process is monitored by the other controllers.

I find that these situations can usually be handled by storing something like {is_master, true}' in the process dictionary and writing generic code that will query when necessary.

The issue with this approach however is that, if the Process Dictionary becomes ‘large’ (maybe it contains some large binaries), these will need to be copied over to the other processes whenever process_info(Pid, [dictionary]) is called.

I’ve been wondering for a while what the general thought is towards this type of thing; is there a better way of approaching the issue where you’re not left with a single point of failure / maintaining some type of registry or are you left to swallow the cost of looking up the dictionary?

In these instances, I’m left wishing that I could arbitrarily set some type of attribute against the current process that could be queried using something like get_attribute(Pid, Key) which would only return the value of that key, without needing to copy any other associated values into the current process

Could be a dumb idea but I wanted to see what other people thought :slight_smile:

1 Like

Hello!

In Erlang/OTP 27 erlang:process_info(Pid,{dictionary,Key}) has been added to do exactly what you are describing.

There are also new proc_lib functions that can be used to set labels on process, which sound very similar to what you describe: proc_lib — stdlib v6.0.

5 Likes