Is there a way to unregister/1 a name from one process and register/2 it to another atomically?

:wave:

I wonder if there is a way to unregister/1 a name from one process and register/2 it to another atomically?

My use case is I want to swap processes transparently to the caller without introducing an additional process registry.

2 Likes

Hello!

No there is not, but you could implement it using a via callback module for gen_server et al.

5 Likes

You could also use 2 different names and store the one that is β€œactive” in persistent_term. When a process wants to send a message to it they fetch the active atom from persistent_term first.

5 Likes

A follow-up note regarding using via is that if the global module for global name registration is suitable for you, there is no need to implement your own registration module - it already has an atomic global:re_register_name/2,3 function.

4 Likes