Skip to content

Commit 2c665dd

Browse files
committed
Replace thread worker/sup with poolboy worker pool
1 parent 334c31c commit 2c665dd

File tree

13 files changed

+136
-191
lines changed

13 files changed

+136
-191
lines changed

config/dev.exs.sample

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ config :gmail, :oauth2, [
88
token_type: "Bearer",
99
expires_at: 1428085947
1010
]
11+
12+
config :gmail, :thread,
13+
pool: 100

lib/gmail/base.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ defmodule Gmail.Base do
5454
@spec handle_error(map) :: atom | {atom, String.t} | {atom, map} | map
5555
def handle_error(response) do
5656
case response do
57-
{:ok, %{"error" => %{"code" => 404}} } ->
57+
{:ok, %{"error" => %{"code" => 404}}} ->
5858
{:error, :not_found}
59-
{:ok, %{"error" => %{"code" => 400, "errors" => errors}} } ->
59+
{:ok, %{"error" => %{"code" => 400, "errors" => errors}}} ->
6060
[%{"message" => error_message}|_rest] = errors
6161
{:error, error_message}
6262
{:ok, %{"error" => details}} ->

lib/gmail/history.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ defmodule Gmail.History do
2424
"""
2525
def handle_history_response(response) do
2626
case response do
27-
{:ok, %{"error" => %{"code" => 404}} } ->
27+
{:ok, %{"error" => %{"code" => 404}}} ->
2828
:not_found
29-
{:ok, %{"error" => %{"code" => 400, "errors" => errors}} } ->
29+
{:ok, %{"error" => %{"code" => 400, "errors" => errors}}} ->
3030
{:error, errors}
3131
{:ok, %{"history" => history}} ->
3232
{:ok, Helper.atomise_keys(history)}

lib/gmail/http.ex

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,40 @@ defmodule Gmail.HTTP do
1717

1818
def execute({:get, url, path}, %{access_token: access_token}) do
1919
(url <> path)
20-
|> HTTPoison.get(get_headers(access_token))
20+
|> HTTPoison.get(get_headers(access_token), timeout: :infinity, recv_timeout: :infinity)
2121
|> do_parse_response
2222
end
2323

2424
def execute({:post, url, path, data}, %{access_token: access_token}) do
2525
{:ok, json} = encode(data)
2626
(url <> path)
27-
|> HTTPoison.post(json, get_headers(access_token))
27+
|> HTTPoison.post(json, get_headers(access_token), timeout: :infinity, recv_timeout: :infinity)
2828
|> do_parse_response
2929
end
3030

3131
def execute({:post, url, path}, %{access_token: access_token}) do
3232
(url <> path)
33-
|> HTTPoison.post("", get_headers(access_token))
33+
|> HTTPoison.post("", get_headers(access_token), timeout: :infinity, recv_timeout: :infinity)
3434
|> do_parse_response
3535
end
3636

3737
def execute({:delete, url, path}, %{access_token: access_token}) do
3838
(url <> path)
39-
|> HTTPoison.delete(get_headers(access_token))
39+
|> HTTPoison.delete(get_headers(access_token), timeout: :infinity, recv_timeout: :infinity)
4040
|> do_parse_response
4141
end
4242

4343
def execute({:put, url, path, data}, %{access_token: access_token}) do
4444
{:ok, json} = encode(data)
4545
(url <> path)
46-
|> HTTPoison.put(json, get_headers(access_token))
46+
|> HTTPoison.put(json, get_headers(access_token), timeout: :infinity, recv_timeout: :infinity)
4747
|> do_parse_response
4848
end
4949

5050
def execute({:patch, url, path, data}, %{access_token: access_token}) do
5151
{:ok, json} = encode(data)
5252
(url <> path)
53-
|> HTTPoison.patch(json, get_headers(access_token))
53+
|> HTTPoison.patch(json, get_headers(access_token), timeout: :infinity, recv_timeout: :infinity)
5454
|> do_parse_response
5555
end
5656

@@ -67,8 +67,8 @@ defmodule Gmail.HTTP do
6767
:ok
6868
end
6969

70-
defp do_parse_response({:error, %HTTPoison.Error{id: id, reason: reason}}) do
71-
{:error, reason}
70+
defp do_parse_response({:error, %HTTPoison.Error{reason: reason}}) do
71+
{:error, reason}
7272
end
7373

7474
@spec get_headers(String.t) :: [{String.t, String.t}]

lib/gmail/supervisor.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ defmodule Gmail.Supervisor do
1515
def init(:ok) do
1616
children = [
1717
supervisor(Gmail.UserManager, []),
18-
supervisor(Gmail.Thread.Supervisor, []),
18+
supervisor(Gmail.Thread.Pool, []),
1919
supervisor(Gmail.Message.Supervisor, [])
2020
]
2121
supervise(children, strategy: :one_for_one)

lib/gmail/thread.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ defmodule Gmail.Thread do
8585
@doc """
8686
Handles a thread resource response from the Gmail API.
8787
"""
88-
@spec handle_thread_response(atom | {atom, map | String.t}) :: {atom, String.t | map}
88+
@spec handle_thread_response(atom | {atom, map | String.t}) :: {atom, String.t | map}
8989
def handle_thread_response(response) do
9090
response
9191
|> handle_error
@@ -104,7 +104,7 @@ defmodule Gmail.Thread do
104104
@doc """
105105
Handles a thread list response from the Gmail API.
106106
"""
107-
@spec handle_thread_list_response(atom | {atom, map | String.t}) :: {atom, String.t | map} | {atom, map, String.t}
107+
@spec handle_thread_list_response(atom | {atom, map | String.t}) :: {atom, String.t | map} | {atom, map, String.t}
108108
def handle_thread_list_response(response) do
109109
response
110110
|> handle_error

lib/gmail/thread/pool.ex

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
defmodule Gmail.Thread.Pool do
2+
3+
@moduledoc """
4+
A pool of workers for handling thread operations.
5+
"""
6+
7+
alias Gmail.Thread.PoolWorker
8+
9+
@default_pool_size 20
10+
11+
@doc false
12+
def start_link do
13+
poolboy_config = [
14+
{:name, {:local, :thread_pool}},
15+
{:worker_module, PoolWorker},
16+
{:size, pool_size},
17+
{:max_overflow, 0}
18+
]
19+
20+
children = [
21+
:poolboy.child_spec(:thread_pool, poolboy_config, [])
22+
]
23+
24+
options = [
25+
strategy: :one_for_one,
26+
name: __MODULE__
27+
]
28+
29+
Supervisor.start_link(children, options)
30+
end
31+
32+
@doc """
33+
Gets a thread.
34+
"""
35+
@spec get(String.t, String.t, map, map) :: {atom, map}
36+
def get(user_id, thread_id, params, state) do
37+
:poolboy.transaction(
38+
:thread_pool,
39+
fn pid ->
40+
PoolWorker.get(pid, user_id, thread_id, params, state)
41+
end,
42+
:infinity)
43+
end
44+
45+
def pool_size do
46+
case Application.get_env(:gmail, :thread) do
47+
[pool: size] when is_integer(size) ->
48+
size
49+
_ ->
50+
@default_pool_size
51+
end
52+
end
53+
54+
end

lib/gmail/thread/pool_worker.ex

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
defmodule Gmail.Thread.PoolWorker do
2+
3+
@moduledoc """
4+
A thread pool worker.
5+
"""
6+
7+
use GenServer
8+
alias Gmail.{Thread, User}
9+
10+
@doc false
11+
def start_link([]) do
12+
GenServer.start_link(__MODULE__, [], [])
13+
end
14+
15+
@doc false
16+
def init(state) do
17+
{:ok, state}
18+
end
19+
20+
@doc false
21+
def handle_call({:get, user_id, thread_id, params, state}, _from, worker_state) do
22+
result =
23+
user_id
24+
|> Thread.get(thread_id, params)
25+
|> User.http_execute(state)
26+
|> Thread.handle_thread_response
27+
{:reply, result, worker_state}
28+
end
29+
30+
@doc """
31+
Gets a thread.
32+
"""
33+
@spec get(pid, String.t, String.t, map, map) :: {atom, map}
34+
def get(pid, user_id, thread_id, params, state) do
35+
GenServer.call(pid, {:get, user_id, thread_id, params, state}, :infinity)
36+
end
37+
38+
end

lib/gmail/thread/supervisor.ex

Lines changed: 0 additions & 21 deletions
This file was deleted.

lib/gmail/thread/worker.ex

Lines changed: 0 additions & 140 deletions
This file was deleted.

0 commit comments

Comments
 (0)