Skip to content

Commit bccc076

Browse files
author
Pablo Lopez
committed
Starting one pool per region
1 parent 08c9c96 commit bccc076

File tree

2 files changed

+33
-22
lines changed

2 files changed

+33
-22
lines changed

src/kinetic.erl

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121

2222
-include("kinetic.hrl").
2323

24-
-define(EHTTPC_POOL, kinetic_pool).
25-
2624
% application behaviour
2725

2826
-spec start() -> ok | {error, any()}.
@@ -34,17 +32,21 @@ stop() ->
3432
application:stop(kinetic).
3533

3634
start(Opts) when is_list(Opts) ->
37-
start_pool(Opts),
35+
start_pool(),
3836
kinetic_sup:start_link(Opts).
3937

4038
-spec start(normal | {takeover, node()} | {failover, node()}, any()) -> {ok, pid()}.
4139
start(_, Opts) ->
42-
start_pool(Opts),
40+
start_pool(),
4341
kinetic_sup:start_link(Opts).
4442

4543
-spec stop(any()) -> ok.
4644
stop(_) ->
47-
ehttpc_sup:stop_pool(?EHTTPC_POOL),
45+
lists:foreach(fun(Region) ->
46+
PoolName = kinetic_utils:pool_name(Region),
47+
ok = ehttpc_sup:stop_pool(PoolName)
48+
end,
49+
kinetic_utils:regions()),
4850
ok.
4951

5052
% Public API
@@ -281,7 +283,8 @@ execute(Operation, Payload, Opts) ->
281283
signed_headers => SignedHeaders,
282284
aws_date => Date},
283285
iolist_to_binary(Body))],
284-
Worker = ehttpc_pool:pick_worker(?EHTTPC_POOL),
286+
PoolName = kinetic_utils:pool_name(Region),
287+
Worker = ehttpc_pool:pick_worker(PoolName),
285288
case ehttpc:request(Worker, post, {"/", Headers, Body}, Timeout) of
286289
{ok, 200, _, ResponseBody} ->
287290
{ok, kinetic_utils:decode(ResponseBody)};
@@ -305,19 +308,15 @@ get_value(Key, TupleList) ->
305308
{Key, Value} = lists:keyfind(Key, 1, TupleList),
306309
Value.
307310

308-
start_pool(Opts) ->
309-
Region =
310-
case proplists:get_value(region, Opts, undefined) of
311-
undefined ->
312-
{ok, Zone} = imds:zone(),
313-
kinetic_utils:region(Zone);
314-
R ->
315-
R
316-
end,
317-
Endpoint = kinetic_utils:endpoint(Region),
311+
start_pool() ->
318312
PoolSize = application:get_env(?MODULE, pool_size, 100),
319-
ehttpc_sup:start_pool(?EHTTPC_POOL,
320-
[{host, Endpoint},
321-
{port, 443},
322-
{pool_size, PoolSize},
323-
{gun_opts, [{transport, tls}]}]).
313+
lists:foreach(fun(Region) ->
314+
PoolName = kinetic_utils:pool_name(Region),
315+
Endpoint = kinetic_utils:endpoint(Region),
316+
ehttpc_sup:start_pool(PoolName,
317+
[{host, Endpoint},
318+
{port, 443},
319+
{pool_size, PoolSize},
320+
{gun_opts, [{transport, tls}]}])
321+
end,
322+
kinetic_utils:regions()).

src/kinetic_utils.erl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
-module(kinetic_utils).
22

3-
-export([region/1, endpoint/1, decode/1, encode/1]).
3+
-export([pool_name/1, regions/0, region/1, endpoint/1, decode/1, encode/1]).
4+
5+
pool_name("us-east-1" ++ _R) ->
6+
'dinerl_pool_us-east-1';
7+
pool_name("us-west-2" ++ _R) ->
8+
'dinerl_pool_us-west-2';
9+
pool_name("ap-southeast-1" ++ _R) ->
10+
'dinerl_pool_ap-southeast-1';
11+
pool_name("eu-west-1" ++ _R) ->
12+
'dinerl_pool_eu-west-1'.
13+
14+
regions() ->
15+
["us-east-1", "us-west-2", "ap-southeast-1", "eu-west-1"].
416

517
region("us-east-1" ++ _R) ->
618
"us-east-1";

0 commit comments

Comments
 (0)