Skip to content

Commit

Permalink
Merge pull request #51 from puzza007/max-total-connections-opt
Browse files Browse the repository at this point in the history
Add config item for CURLMOPT_MAX_TOTAL_CONNECTIONS
  • Loading branch information
puzza007 committed Apr 2, 2017
2 parents 80b4ca2 + 4fc96ff commit 7fa1aeb
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,11 @@ katipo:Method(Pool :: atom(), URL :: binary(), ReqOptions :: map()).

#### Pool Options

| Option | Type | Default | Note |
|:----------------------|:---------------------|:----------------- |----------------------------------------|
| `pipelining` | `boolean()` | `false` | HTTP pipelining |
| `max_pipeline_length` | `non_neg_integer()` | 100 | |
| Option | Type | Default | Note |
|:------------------------|:---------------------|:----------------- |----------------------------------------|
| `pipelining` | `boolean()` | `false` | HTTP pipelining |
| `max_pipeline_length` | `non_neg_integer()` | 100 | |
| `max_total_connections` | `non_neg_integer()` | 0 (no limit) | |

#### Metrics

Expand Down
7 changes: 6 additions & 1 deletion c_src/katipo.c
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,7 @@ int main(int argc, char **argv) {
struct option long_options[] = {
{ "pipelining", no_argument, &pipelining_flag, 1 },
{ "max-pipeline-length", required_argument, 0, 'a' },
{ "max-total-connections", required_argument, 0, 'c' },
{ 0, 0, 0, 0 }
};

Expand All @@ -1090,7 +1091,7 @@ int main(int argc, char **argv) {
curl_multi_setopt(global.multi, CURLMOPT_TIMERDATA, &global);

while (1) {
c = getopt_long(argc, argv, "a:", long_options, &option_index);
c = getopt_long(argc, argv, "ac:", long_options, &option_index);
if (c == -1)
break;
switch (c) {
Expand All @@ -1100,6 +1101,10 @@ int main(int argc, char **argv) {
curl_multi_setopt(global.multi, CURLMOPT_MAX_PIPELINE_LENGTH,
atoi(optarg));
break;
case 'c':
curl_multi_setopt(global.multi, CURLMOPT_MAX_TOTAL_CONNECTIONS,
atoi(optarg));
break;
default:
errx(2, "Unknown option '%c'\n", c);
}
Expand Down
9 changes: 7 additions & 2 deletions src/katipo.erl
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@
%% curlmopt_content_length_penalty_size |
%% curlmopt_max_host_connections |
max_pipeline_length |
%% curlmopt_max_total_connections |
curlmopt_max_total_connections |
%% curlmopt_maxconnects |
pipelining.
%% curlmopt_pipelining_site_bl |
Expand Down Expand Up @@ -205,7 +205,9 @@
-type response() :: {ok, map()} | {error, map()}.
-type http_auth() :: basic | digest.
-type http_auth_int() :: ?CURLAUTH_BASIC | ?CURLAUTH_DIGEST.
-type curlmopts() :: [{max_pipeline_length, non_neg_integer()} | {pipelining, boolean()}].
-type curlmopts() :: [{max_pipeline_length, non_neg_integer()} |
{pipelining, boolean()} |
{max_total_connections, non_neg_integer()}].

-export_type([method/0]).
-export_type([url/0]).
Expand Down Expand Up @@ -463,6 +465,9 @@ mopt_supported({max_pipeline_length, Val})
{true, "--max-pipeline-length " ++ integer_to_list(Val)};
mopt_supported({pipelining, true}) ->
{true, "--pipelining"};
mopt_supported({max_total_connections, Val})
when is_integer(Val) andalso Val >= 0 ->
{true, "--max-total-connections " ++ integer_to_list(Val)};
mopt_supported({_, _}) ->
false.

Expand Down
23 changes: 21 additions & 2 deletions test/katipo_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,17 @@ groups() ->
session_new_cookies,
session_new_headers,
session_update,
session_update_bad_opts]}].
session_update_bad_opts]},
{port, [],
[max_total_connections]}].

all() ->
[{group, http},
{group, pool},
{group, https},
{group, proxy},
{group, session}].
{group, session},
{group, port}].

get(_) ->
{ok, #{status := 200, body := Body}} =
Expand Down Expand Up @@ -591,6 +594,22 @@ session_update_bad_opts(_) ->
{error, #{code := bad_opts}} =
katipo_session:update(#{timeout_ms => <<"wrong">>, what => not_even_close}, Session).

max_total_connections(_) ->
PoolName = max_total_connections,
{ok, _} = katipo_pool:start(PoolName, 1, [{max_total_connections, 1}]),
Self = self(),
Fun = fun() ->
{ok, #{status := 200}} =
katipo:get(PoolName, <<"http://httpbin.org/delay/5">>),
Self ! ok
end,
spawn(Fun),
spawn(Fun),
Start = erlang:system_time(seconds),
[receive ok -> ok end || _ <- [1, 2]],
Diff = erlang:system_time(seconds) - Start,
true = Diff >= 10.

repeat_until_true(Fun) ->
try
case Fun() of
Expand Down

0 comments on commit 7fa1aeb

Please sign in to comment.