Created
March 17, 2012 13:05
-
-
Save brendanhay/2058721 to your computer and use it in GitHub Desktop.
AMQP single send - error monad vs nested case
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
-spec send(method(), #amqp_params_direct{}) -> error_m(ok, term()). | |
%% @private | |
send(Method, Params) -> | |
do([error_m || | |
Conn <- amqp_connection:start(Params), | |
Chan <- amqp_connection:open_channel(Conn), | |
return(amqp_channel:call(Chan, Method)), | |
return(amqp_channel:close(Chan)), | |
amqp_connection:close(Conn)]). | |
-spec send(method(), #amqp_params_direct{}) -> ok. | |
%% @private | |
send(Method, Params) -> | |
{Conn, Chan} = | |
case amqp_connection:start(Params) of | |
{ok, Conn} -> | |
case amqp_connection:open_channel(Conn) of | |
{ok, Chan} -> | |
amqp_channel:call(Chan, Method), | |
{Conn, Chan}; | |
Error -> | |
{Conn, none} | |
end; | |
Error -> | |
{none, none} | |
end, | |
case Chan of | |
none -> ok; | |
_ -> amqp_channel:close(Chan) | |
end, | |
case Conn of | |
none -> ok; | |
_ -> amqp_connection:close(Conn) | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment