Created
July 2, 2014 11:03
-
-
Save darach/83ca6d1dd347dd215082 to your computer and use it in GitHub Desktop.
ZMQ lazy pirate pattern - server - erlzmq2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env escript | |
-module(lps). | |
-mode(compile). | |
-define(request_timeout,2500). %% msecs, (> 1000!) | |
-define(request_retries,3). %% before we abandon | |
-define(halt_strategy,die). | |
main(_Args) -> | |
%% Append project ebin to path | |
true = code:add_pathz(filename:dirname(escript:script_name()) ++ "/../ebin"), | |
%% Create ZMQ client with REQ client socket pattern and connect | |
{ok, Ctx} = erlzmq:context(), | |
{ok, Sock} = erlzmq:socket(Ctx, rep), | |
ok = erlzmq:bind(Sock, "tcp://*:4321"), | |
Cycles = 0, | |
loop(Cycles,{Ctx,Sock}). | |
loop(Cycles,{Ctx,Sock}) -> | |
{ok,Msg} = erlzmq:recv(Sock), | |
io:format("RECV: ~p~n", [Msg]), | |
case trunc(random:uniform() * 9) == 0 of | |
true -> | |
io:format("Simulate down [exiting]~n",[]), maybehalt(); | |
%% dont send response as 'down' (ie: simulates loss) | |
false -> | |
case trunc(random:uniform() * 5) == 0 of | |
true -> | |
io:format("Simulate saturation [delay]~n", []), | |
%% sleep an inordinate amount of time (simulate delay) | |
timer:sleep(1000), | |
erlzmq:send(Sock,Msg); | |
false -> | |
erlzmq:send(Sock,Msg) | |
end | |
end, | |
loop(Cycles+1,{Ctx,Sock}). | |
maybehalt() -> | |
case ?halt_strategy of | |
die -> halt(1); | |
_ -> ok | |
%% side-effect: 0MQ send blocks forever, but msg is effectively "lost by N/W" | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment