Parse options to thrift_socket_transport:new_protocol_factory/3 "manually" - O(n) instead of O(n^2)
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@666462 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/alterl/src/thrift_socket_transport.erl b/lib/alterl/src/thrift_socket_transport.erl
index 0c65a22..323afa3 100644
--- a/lib/alterl/src/thrift_socket_transport.erl
+++ b/lib/alterl/src/thrift_socket_transport.erl
@@ -49,35 +49,57 @@
%%%% FACTORY GENERATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+%% The following "local" record is filled in by parse_factory_options/2
+%% below. These options can be passed to new_protocol_factory/3 in a
+%% proplists-style option list. They're parsed like this so it is an O(n)
+%% operation instead of O(n^2)
+-record(factory_opts, {connect_timeout = infinity,
+ sockopts = [],
+ framed = false,
+ strict_read = true,
+ strict_write = true}).
+
+parse_factory_options([], Opts) ->
+ Opts;
+parse_factory_options([{strict_read, Bool} | Rest], Opts) when is_boolean(Bool) ->
+ parse_factory_options(Rest, Opts#factory_opts{strict_read=Bool});
+parse_factory_options([{strict_write, Bool} | Rest], Opts) when is_boolean(Bool) ->
+ parse_factory_options(Rest, Opts#factory_opts{strict_write=Bool});
+parse_factory_options([{framed, Bool} | Rest], Opts) when is_boolean(Bool) ->
+ parse_factory_options(Rest, Opts#factory_opts{framed=Bool});
+parse_factory_options([{sockopts, OptList} | Rest], Opts) when is_list(OptList) ->
+ parse_factory_options(Rest, Opts#factory_opts{sockopts=OptList});
+parse_factory_options([{connect_timeout, TO} | Rest], Opts) when TO =:= infinity; is_integer(TO) ->
+ parse_factory_options(Rest, Opts#factory_opts{connect_timeout=TO}).
+
%%
%% Generates a "protocol factory" function - a fun which returns a Protocol instance.
%% This can be passed to thrift_client:start_link in order to connect to a
%% server over a socket.
%%
new_protocol_factory(Host, Port, Options) ->
- ConnectTimeout = proplists:get_value(connect_timeout, Options, infinity),
- InSockOpts = proplists:get_value(sockopts, Options, []),
- Framed = proplists:get_value(framed, Options, false),
- StrictRead = proplists:get_value(strict_read, Options, true),
- StrictWrite = proplists:get_value(strict_write, Options, true),
+ ParsedOpts = parse_factory_options(Options, #factory_opts{}),
F = fun() ->
SockOpts = [binary,
{packet, 0},
{active, false},
{nodelay, true} |
- InSockOpts],
- case catch gen_tcp:connect(Host, Port, SockOpts, ConnectTimeout) of
+ ParsedOpts#factory_opts.sockopts],
+ case catch gen_tcp:connect(Host, Port, SockOpts,
+ ParsedOpts#factory_opts.connect_timeout) of
{ok, Sock} ->
{ok, Transport} = thrift_socket_transport:new(Sock),
{ok, BufTransport} =
- case Framed of
+ case ParsedOpts#factory_opts.framed of
true -> thrift_framed_transport:new(Transport);
false -> thrift_buffered_transport:new(Transport)
end,
- thrift_binary_protocol:new(BufTransport,
- [{strict_read, StrictRead},
- {strict_write, StrictWrite}]);
+ thrift_binary_protocol:new(
+ BufTransport,
+ [{strict_read, ParsedOpts#factory_opts.strict_read},
+ {strict_write, ParsedOpts#factory_opts.strict_write}]);
Error ->
Error
end