is OTP needed if my state does not change?

2019-07-17 03:48发布

问题:

I have an erlang project whose state is entirely read only, and composed of ets tables and a compiled module. It takes a few seconds to build the tables when the code starts.

What is the best way to package this so it can be used by other applications?

Some other things to consider:

  • There are no data contention issues or changing state, so it seems as if there is no need for a gen_server.
  • I like being able to call application:start/1 and have things "just work".
  • Some process should own the ets tables. It seems like I should not leave that up to the client code.

The above leads me to think I should create an application, and call the setup code from the supervisor's init/1 function, but I'm unsure if this is a silly way to approach it.

回答1:

If the module is entirely static you won't need any processes in such an application. If you implement the application behavior in OTP you should be able to connect the ETS tables directly to the main application process (initiate the tables in the application start/2 callback). That way you'll have a minimal process model, allowing you to skip both the supervisor and any gen_server.

You should certainly use OTP, but that doesn't mean that you absolutely must have a supervisor or a gen_server.

Use appmon to view the process hierarchy of your application.



回答2:

Wrap it up as a standard OTP application. gen_server isn't about maintaining state, it's about having a server which can handle requests (it's not called gen_state for a reason imho ;)). Create an OTP app and let people use it in the same way they would any other.



标签: erlang