[erlang-questions] Regarding clearing gen_server state
Bernard Duggan
bernie@REDACTED
Sun Feb 20 00:25:00 CET 2011
Depending on exactly what sort of housekeeping you're talking about,
it's often possible to approach the problem from the other direction.
Say you've got some limited resource being handed out by process P to
your gen_server Q. In this case I always have P monitor Q and reclaim
the resource itself if Q exits (presuming Q hasn't already released the
resource itself). This doesn't cover every possible case of
"housekeeping", but, at least in my case, I've found it does drastically
shrink the amount of self-cleanup any process has to worry about (to
nothing at all, in most cases).
For other cases, the 'heir' option on ETS that Steve suggested is a
reasonable option.
Perhaps some more specific examples might help people suggest the most
appropriate solution, though.
Cheers,
Bernard
On 20/02/2011 7:40 AM, Edward Wang wrote:
> Hi,
>
> In Erlang, it is quite ubiquitous to use state of dynamically spawned
> gen_server to represent entities in problem domain. These gen_servers can
> trap exit signal and do housekeeping when gracefully terminating. If some of
> them crash, a monitoring process can take care of housekeeping for them.
> But, the problem is most likely the monitoring process doesn't have enough
> data to do so. No shared state, remember?
>
> So, I end up with two copy of the same data, one in gen_server's state, one
> in ets table. Not only I need to keep them in sync, but also it *is* shared
> data to some extent.
>
> How can I do it differently?
>
> Regards,
> Edward
More information about the erlang-questions
mailing list