Created
July 7, 2020 09:22
-
-
Save elbrujohalcon/1610d8bbeda9c8a0eea926708fc1ed29 to your computer and use it in GitHub Desktop.
A very busy server that blocks its clients
This file contains hidden or 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
-module busy. | |
-export [loop/0]. | |
-export [test/0]. | |
test() -> | |
start_server(), | |
{error, server_down} = call(server, die), % Your code will catch this | |
start_server(), | |
pong = call(server, ping_and_sleep), | |
pong = call(server, ping), % Your code will block here forever | |
passed. | |
start_server() -> | |
Server = spawn(busy, loop, []), | |
register(server, Server). | |
call(RegName, Request) -> | |
Ref = monitor(process, whereis(RegName)), | |
RegName ! {request, self(), Ref, Request}, | |
receive | |
{reply, Ref, Reply} -> | |
demonitor(Ref, [flush]), | |
Reply; | |
{'DOWN', Ref, process, Pid, Info} -> | |
io:format("Pid ~p Info ~p~n", [Pid, Info]), | |
{error, server_down} | |
end. | |
loop() -> | |
receive | |
{request, From, Ref, ping} -> | |
From ! {reply, Ref, pong}, | |
loop(); | |
{request, _, _, die} -> | |
io:format("Server ~p dying~n", [self()]), | |
dying; | |
{request, From, Ref, ping_and_sleep} -> | |
From ! {reply, Ref, pong}, | |
receive | |
[something, that, will, never, come] -> | |
loop() | |
end | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment