ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Erlang Message Queue Consumer with Basic Acknowledgement

Erlang

Goal -- WPM

Ready
Exercise Algorithm Area
1-module(mq_consumer).
2
3-export([start_link/1, stop/0]).
4
5-behavior(gen_server).
6
7%% API
8start_link(Queue) ->
9gen_server:start_link({local, ?MODULE}, ?MODULE, [Queue], []).
10
11stop() ->
12gen_server:stop(?MODULE).
13
14%% GenServer Callbacks
15init(Queue) ->
16io:format("Consumer started with queue: ~p~n", [Queue]),
17{ok, Queue}.
18
19handle_call(_Msg, _From, State) ->
20%% No synchronous calls expected.
21{reply, {error, not_supported}, State}.
22
23handle_cast(_Msg, State) ->
24%% No cast messages expected.
25{noreply, State}.
26
27handle_info(message, State = [Head | Tail]) ->
28%% Process the message
29io:format("Processing message: ~p~n", [Head]),
30%% Acknowledge message (remove from queue)
31{noreply, Tail};
32
33handle_info(message, State = []) ->
34%% Queue is empty, do nothing.
35{noreply, State};
36
37handle_info(Other, State) ->
38%% Handle unexpected messages.
39io:format("Received unexpected info: ~p~n", [Other]),
40{noreply, State}.
41
42terminate(_Reason, _State) ->
43ok.
44
45code_change(_OldVsn, State, _Extra) ->
46{ok, State}.
Algorithm description viewbox

Erlang Message Queue Consumer with Basic Acknowledgement

Algorithm description:

This Erlang code implements a basic message queue consumer. It uses a GenServer to hold the state of the message queue (represented as a list). The consumer continuously checks for new messages. When a message is received, it's processed (in this case, printed), and then acknowledged by removing it from the queue. This pattern is fundamental for building systems that process asynchronous tasks from a queue, ensuring that messages are handled reliably.

Algorithm explanation:

The `mq_consumer` module defines a GenServer that acts as a message queue consumer. The `start_link/1` function initializes the GenServer with a list representing the message queue. The `init/1` callback simply stores the initial queue. The `handle_info/2` callback is the core of the consumer logic. It pattern matches on the `message` atom. If the queue is not empty (`[Head | Tail]`), it processes the `Head` message (prints it) and then updates the state to `Tail`, effectively acknowledging and removing the message. If the queue is empty (`[]`), it does nothing. Any other `handle_info` messages are logged as unexpected. The `handle_call` and `handle_cast` callbacks are included for completeness but are not used in this simple consumer. The time complexity for processing a message is O(1) assuming list head/tail operations are O(1). The space complexity is O(Q) where Q is the number of messages in the queue.

Pseudocode:

Consumer GenServer:
  State: List representing the message queue.
  init(Queue): Store Queue as state.
  handle_info('message'):
    If Queue is not empty (Head | Tail):
      Process Head (e.g., print).
      Update state to Tail (acknowledge).
    Else (Queue is empty):
      Do nothing.
  Handle other info messages: Log and ignore.
  Ignore call and cast messages.