Swapping ETS tables whole-sale without race conditions

To solve the issue of having to do whole-sale replacement of the contents of ETS tables without creating ad-hoc logic for each table/process owning the table I wrote a small module called ETSManager that can be started as part of your supervision trees.

Then, using its methods, you can access the tables you create through it as if they were named tables and refresh their contents in a non-blocking way, always without race-conditions.

You can see the module here https://gist.github.com/mnussbaumer/270e10a89733b860489bce230cb615da with a simple example - this is not a library because it’s not finished, as in, it doesn’t wrap all functionality of ETS, only encapsulates what I needed and probably there’s better ways to design it in terms of defining actual real callbacks and not simple function captures - but it should be quite self-explanatory and might be useful if you’re looking into ways of doing something similar.

A create/refresh action is done by doing for e.g. GenServer.cast(ETSManager, {:refresh, %ETSManager.Entity{name: :blog_posts, tasker: @tasker, populate_callback: &populate_top_blog_posts/0}})

Any comments/tips wtv welcomed

3 Likes