aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.textile5
-rw-r--r--erlbal.erl12
-rw-r--r--erlbal_tests.erl36
3 files changed, 44 insertions, 9 deletions
diff --git a/README.textile b/README.textile
index 42f05f3..8af6e41 100644
--- a/README.textile
+++ b/README.textile
@@ -18,6 +18,11 @@ h2. Request calls:
* make_request/1 takes a balancer and returns the response from the server.
* make_request/2 takes a balancer and an argument and returns the response from the server.
+h2. The function:
+
+p. The function must take two lists, some arguments and some state.
+p. It must return a tuple of {Return value, new state}.
+
h2. Info
p. The balancer round robins requests between erlang processes.
diff --git a/erlbal.erl b/erlbal.erl
index 84830a7..e03d5e1 100644
--- a/erlbal.erl
+++ b/erlbal.erl
@@ -1,8 +1,8 @@
-module(erlbal).
--export([make_request/2, start_bal/0, stop_bal/1, start_server/3, stop_server/2, list_servers/1]).
+-export([make_request/2, start_bal/0, stop_bal/1, start_server/4, stop_server/2, list_servers/1]).
-start_server(Balancer, Node, Fun) when is_function(Fun, 1) ->
- PID = spawn(Node, fun() -> server_loop(Fun) end),
+start_server(Balancer, Node, Fun, INITSTATE) when is_function(Fun, 2) andalso is_list(INITSTATE) ->
+ PID = spawn(Node, fun() -> server_loop(Fun, INITSTATE) end),
Balancer ! {add_node, PID}.
stop_server(Balancer, PID) ->
@@ -13,12 +13,12 @@ list_servers(Balancer) ->
Balancer ! {list_nodes, self()},
receive Nodes -> Nodes end.
-server_loop(Fun) ->
+server_loop(Fun, STATE) ->
receive
{request, From, ARGS} ->
- Ret = Fun(ARGS),
+ {Ret, NEWSTATE} = Fun(ARGS, STATE),
From ! Ret,
- server_loop(Fun);
+ server_loop(Fun, NEWSTATE);
die ->
ok
end.
diff --git a/erlbal_tests.erl b/erlbal_tests.erl
index df68706..cee35fc 100644
--- a/erlbal_tests.erl
+++ b/erlbal_tests.erl
@@ -3,8 +3,38 @@
start(simple) ->
Bal = erlbal:start_bal(),
- erlbal:start_server(Bal, node(), fun(_) -> 7 end),
- erlbal:start_server(Bal, node(), fun(_) -> 8 end),
+ erlbal:start_server(Bal, node(), fun(_, _) -> {7, []} end, []),
+ erlbal:start_server(Bal, node(), fun(_, _) -> {8, []} end, []),
7 = erlbal:make_request(Bal, []),
8 = erlbal:make_request(Bal, []),
- erlbal:stop_bal(Bal).
+ erlbal:stop_bal(Bal),
+ ok;
+
+start(args) ->
+ Bal = erlbal:start_bal(),
+ erlbal:start_server(Bal, node(), fun([X], _) -> {X+1, []} end, []),
+ erlbal:start_server(Bal, node(), fun([X], _) -> {X+2, []} end, []),
+ 7 = erlbal:make_request(Bal, [6]),
+ 8 = erlbal:make_request(Bal, [6]),
+ erlbal:stop_bal(Bal),
+ ok;
+
+start(state) ->
+ Bal = erlbal:start_bal(),
+ erlbal:start_server(Bal, node(), fun([X], [S]) -> Y = X + S, {Y, [S]} end, [1]),
+ erlbal:start_server(Bal, node(), fun([X], [S]) -> Y = X + S, {Y, [S]} end, [10]),
+ 7 = erlbal:make_request(Bal, [6]),
+ 16 = erlbal:make_request(Bal, [6]),
+ erlbal:stop_bal(Bal),
+ ok;
+
+start(complex_state) ->
+ Bal = erlbal:start_bal(),
+ erlbal:start_server(Bal, node(), fun([X], [S]) -> Y = X + S, {Y, [S+1]} end, [1]),
+ erlbal:start_server(Bal, node(), fun([X], [S]) -> Y = X + S, {Y, [S+1]} end, [10]),
+ 7 = erlbal:make_request(Bal, [6]),
+ 16 = erlbal:make_request(Bal, [6]),
+ 8 = erlbal:make_request(Bal, [6]),
+ 17 = erlbal:make_request(Bal, [6]),
+ erlbal:stop_bal(Bal),
+ ok.