Should a mapping of Id to Pid be stored in an ets table, or the gen_server's state?

StackOverflow https://stackoverflow.com/questions/16951098

  •  31-05-2022
  •  | 
  •  

Question

I'm building an OTP application which follows a pattern similar to one described on trapexit, where I implement a non-blocking gen_server using gen_server:call/3 to initiate a transaction with a backend and store a mapping of transaction id to the From pid. When the gen_server receives a message from the backend, it extracts the transaction id and uses this mapping to look up the correct pid, which it forwards the message to.

In the trapexit example, this mapping is implemented using ets, however I found that having the gen_server's state contain a dict with these mappings to be a very natural solution.

For my particular use case the mapping will contain, at most, 200 entries.

Which implementation is recommended?

Thanks in advance!

Était-ce utile?

La solution

200 is enough to have some impact on performance compared to ets (probably one order of magnitude or less). The real question you must ask yourself is "Do I need this extra performance or will this be sufficient?".

If performance isn't an issue use the dict.

Autres conseils

The functional approach is to keep your private data in state. One practical consideration against having very large state data (which yours does not appear to be) however is that it will get dumped in a crash log.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top