How jeffutter/rediscovery works
Main Components
• Rediscovery Core Module: This is the heart of the project (see files such as lib/rediscovery.ex, lib/rediscovery/application.ex, and several internal modules like poller, pub_sub, listener, logger, etc.). It provides the service discovery functionality and interacts with Redis.
• Listener Modules: These are user-supplied modules (as demonstrated in the README with MyApp.Listener) that implement specific callbacks (list/0, add/1, remove/1) to react to node changes.
• External Redis Service: Since the project relies on a Redis backend, include a component representing the Redis server; remember to note configuration options (host, port, and prefix).
Relationships and Interactions
• The Rediscovery component communicates with the Redis backend (using a connection probably managed internally) to detect node information.
• When changes are observed (via polling or pub/sub mechanisms, as indicated by the presence of modules like poller and pub_sub), Rediscovery notifies the Listener modules.
• The Listener modules use the provided callbacks to update node connectivity (e.g., connecting or disconnecting nodes), and this communication is triggered by the Rediscovery core service.
• All these components are wired together and supervised as part of the OTP supervision tree to ensure fault tolerance.
Architectural Patterns and Design Principles
• OTP Supervision Tree: The proper use of supervisors is critical, ensuring that Rediscovery and the listeners are restarted if they crash.
• Callback/Listener Pattern: The design allows the integration of custom listener implementations to handle node addition and removal events.
• Decoupling: The architecture decouples the discovery logic (Redis discovery) from the application's reaction logic (listener callbacks), increasing flexibility.
• External Service Integration: The system is designed to work with an external Redis server for data storage and event propagation.