-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.erl
40 lines (28 loc) · 995 Bytes
/
router.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
-module(router).
-behaviour(gen_server).
-author("Dodi Cristian-Dumitru").
-export([start_link/0]).
%% Callbacks
-export([init/1,handle_cast/2, handle_call/3, handle_info/2]).
start_link() ->
gen_server:start_link({local, router}, ?MODULE, [], [{debug, [statistics]}]).
init(_Args) ->
io:format("Router started ~p~n",[self()]),
daynamic_supervisor:add_worker(2),
autoscaler ! count,
{ok, 1}.
handle_cast({msg, Msg}, State) ->
Idx = round_robin(Msg, State),
{noreply, Idx}.
handle_info(_Info, State) ->
{noreply, State}.
handle_call(stop, _From, State) ->
{stop, normal, stopped, State};
handle_call(_Request, _From, State) ->
{reply, ok, State}.
%%%%%%%%%%%%%%%%%%%%%%
round_robin(Msg, Idx) ->
Workers = global:registered_names(),
NewIdx = if Idx < length(Workers) -> gen_server:cast(lists:nth(Idx, Workers), {init_msg,Msg}), Idx + 1;
true -> gen_server:cast(lists:nth(1, Workers), {init_msg,Msg}), 1 end,
NewIdx.