I want to extend gen_server (create a gen_server_extra) with some additional functionality. The requirements are:
- The
gen_server_extraprocesses should behave like a regulargen_server‘s. E.g, they should accept calls viagen_server:call, integrate with SASL, fit OTC supervision tree, etc. gen_server_extraprocesses should have an additional functionality, provided bygen_server_extra. That basically means some of the messages will be handled bygen_server_extracode, without passing them to the callback module. The rest of the messages are passed to callback module as is.gen_server_extrafunctionality requires its own state which should be hidden from the callback module.
What is the simplest approach to do that?
The best, most modular approach would be to implement a new behavior in a module (e.g.
gen_ext_server) and wrap thegen_serverbehavior from there.First, make sure your behavior is identical to
gen_server:Implement all callbacks needed for
gen_server, keep the name of the callback module that implements your behavior in your state:Then, in each
gen_servercallback, implement your behavior and then call the callback module if needed:Implement similar functionality for
handle_cast/2,handle_info/2,terminate/1etc.