Module em_filter_server

This module implements a generic filter server based on the gen_server behaviour.

Behaviours: gen_server.

Description

This module implements a generic filter server based on the gen_server behaviour. It manages starting an HTTP server (using Wade) that listens on a configurable port and provides a /query endpoint for incoming requests.

The filter server synchronizes access using an ETS-based lock system to prevent multiple instances of the same filter starting concurrently.

Incoming HTTP requests are delegated to a pluggable handler module that must export a handle/1 function. This separation allows different filter logic to be plugged without modifying the server infrastructure.

The module includes robust error handling, detailed logging, and graceful shutdown procedures to ensure reliability in production. It also integrates with a discovery service for runtime registration of filter URLs.

State stored in the process includes: - filter_name: Atom identifying the filter - handler_module: Module handling query processing - port: TCP port number for HTTP server - wade_pid: PID of the Wade HTTP server process

Function Index

code_change/3 Handles code upgrades; currently simply preserves state without changes.
handle_call/3 Handles gen_server call requests; here simply replies :ok as no calls are handled.
handle_cast/2 Handles asynchronous gen_server cast messages; no specific processing done.
handle_info/2 Handles info messages, notably traps exit signals from Wade to cleanup.
init/1 Initializes the server by: 1.
start_link/3 Starts the filter server process with given name, handler module, and HTTP port.
terminate/2 Gracefully terminates the filter server by stopping Wade server, cleaning persistent state, and releasing the synchronization lock.
wait_for_lock/1 Waits recursively for a lock on the filter to be released before proceeding.

Function Details

code_change/3

code_change(OldVsn, State, Extra) -> any()

Handles code upgrades; currently simply preserves state without changes.

handle_call/3

handle_call(Request, From, State) -> any()

Handles gen_server call requests; here simply replies :ok as no calls are handled

handle_cast/2

handle_cast(Msg, State) -> any()

Handles asynchronous gen_server cast messages; no specific processing done

handle_info/2

handle_info(Info, Filter_state) -> any()

Handles info messages, notably traps exit signals from Wade to cleanup

init/1

init(X1) -> any()

Initializes the server by: 1. Waiting for any existing lock on the filter to be released (prevents concurrent starts) 2. Starting the Wade HTTP server on the configured port 3. Registering an HTTP route /query that delegates to local handle_query/2 4. Registering filter service URL to a discovery mechanism 5. Setting up internal process state

Returns {ok, State} if successful, otherwise stops the server.

start_link/3

start_link(FilterName, HandlerModule, Port) -> any()

Starts the filter server process with given name, handler module, and HTTP port. Registers the process locally using a derived name to avoid conflicts.

terminate/2

terminate(Reason, State) -> any()

Gracefully terminates the filter server by stopping Wade server, cleaning persistent state, and releasing the synchronization lock.

wait_for_lock/1

wait_for_lock(FilterName) -> any()

Waits recursively for a lock on the filter to be released before proceeding.


Generated by EDoc