Skip to content

Commit 0b846fe

Browse files
authored
Merge pull request #62 from configcat/fix/fetch-genserver-timeout
fix: prevent genserver calls from timing out during config fetch
2 parents ec2aed3 + 2fb0bc9 commit 0b846fe

File tree

6 files changed

+41
-24
lines changed

6 files changed

+41
-24
lines changed

lib/config_cat/cache_policy/auto.ex

+18-10
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ defmodule ConfigCat.CachePolicy.Auto do
33

44
use GenServer
55

6-
alias ConfigCat.CachePolicy
6+
alias ConfigCat.{CachePolicy, Constants}
77
alias ConfigCat.CachePolicy.{Behaviour, Helpers}
88

9+
require Constants
910
require Logger
1011

1112
defstruct mode: "a", on_changed: nil, poll_interval_seconds: 60
@@ -38,22 +39,32 @@ defmodule ConfigCat.CachePolicy.Auto do
3839

3940
@impl GenServer
4041
def handle_continue(:initial_fetch, state) do
41-
polled_refresh(state)
42+
refresh(state)
43+
schedule_next_refresh(state)
44+
45+
{:noreply, state}
4246
end
4347

4448
@impl GenServer
4549
def handle_info(:polled_refresh, state) do
46-
polled_refresh(state)
50+
pid = self()
51+
52+
Task.start_link(fn ->
53+
refresh(state)
54+
schedule_next_refresh(state, pid)
55+
end)
56+
57+
{:noreply, state}
4758
end
4859

4960
@impl Behaviour
5061
def get(policy_id) do
51-
GenServer.call(policy_id, :get)
62+
GenServer.call(policy_id, :get, Constants.fetch_timeout())
5263
end
5364

5465
@impl Behaviour
5566
def force_refresh(policy_id) do
56-
GenServer.call(policy_id, :force_refresh)
67+
GenServer.call(policy_id, :force_refresh, Constants.fetch_timeout())
5768
end
5869

5970
@impl GenServer
@@ -72,11 +83,8 @@ defmodule ConfigCat.CachePolicy.Auto do
7283
end
7384
end
7485

75-
defp polled_refresh(%{poll_interval_seconds: seconds} = state) do
76-
refresh(state)
77-
Process.send_after(self(), :polled_refresh, seconds * 1000)
78-
79-
{:noreply, state}
86+
defp schedule_next_refresh(%{poll_interval_seconds: seconds}, pid \\ self()) do
87+
Process.send_after(pid, :polled_refresh, seconds * 1000)
8088
end
8189

8290
defp refresh(state) do

lib/config_cat/cache_policy/lazy.ex

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ defmodule ConfigCat.CachePolicy.Lazy do
33

44
use GenServer
55

6-
alias ConfigCat.CachePolicy
6+
alias ConfigCat.{CachePolicy, Constants}
77
alias ConfigCat.CachePolicy.{Behaviour, Helpers}
88

9+
require Constants
10+
911
@enforce_keys [:cache_expiry_seconds]
1012
defstruct [:cache_expiry_seconds, mode: "l"]
1113

@@ -34,12 +36,12 @@ defmodule ConfigCat.CachePolicy.Lazy do
3436

3537
@impl Behaviour
3638
def get(policy_id) do
37-
GenServer.call(policy_id, :get)
39+
GenServer.call(policy_id, :get, Constants.fetch_timeout())
3840
end
3941

4042
@impl Behaviour
4143
def force_refresh(policy_id) do
42-
GenServer.call(policy_id, :force_refresh)
44+
GenServer.call(policy_id, :force_refresh, Constants.fetch_timeout())
4345
end
4446

4547
@impl GenServer

lib/config_cat/cache_policy/manual.ex

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ defmodule ConfigCat.CachePolicy.Manual do
33

44
use GenServer
55

6-
alias ConfigCat.CachePolicy
6+
alias ConfigCat.{CachePolicy, Constants}
77
alias ConfigCat.CachePolicy.{Behaviour, Helpers}
88

9+
require Constants
10+
911
defstruct mode: "m"
1012

1113
@type t :: %__MODULE__{mode: String.t()}
@@ -34,7 +36,7 @@ defmodule ConfigCat.CachePolicy.Manual do
3436

3537
@impl Behaviour
3638
def force_refresh(policy_id) do
37-
GenServer.call(policy_id, :force_refresh)
39+
GenServer.call(policy_id, :force_refresh, Constants.fetch_timeout())
3840
end
3941

4042
@impl GenServer

lib/config_cat/client.ex

+10-6
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,37 @@ defmodule ConfigCat.Client do
2323

2424
@spec get_all_keys(client()) :: [Config.key()]
2525
def get_all_keys(client) do
26-
GenServer.call(client, :get_all_keys)
26+
GenServer.call(client, :get_all_keys, Constants.fetch_timeout())
2727
end
2828

2929
@spec get_value(client(), Config.key(), Config.value(), User.t() | nil) :: Config.value()
3030
def get_value(client, key, default_value, user \\ nil) do
31-
GenServer.call(client, {:get_value, key, default_value, user})
31+
GenServer.call(client, {:get_value, key, default_value, user}, Constants.fetch_timeout())
3232
end
3333

3434
@spec get_variation_id(client(), Config.key(), Config.variation_id(), User.t() | nil) ::
3535
Config.variation_id()
3636
def get_variation_id(client, key, default_variation_id, user \\ nil) do
37-
GenServer.call(client, {:get_variation_id, key, default_variation_id, user})
37+
GenServer.call(
38+
client,
39+
{:get_variation_id, key, default_variation_id, user},
40+
Constants.fetch_timeout()
41+
)
3842
end
3943

4044
@spec get_all_variation_ids(client(), User.t() | nil) :: [Config.variation_id()]
4145
def get_all_variation_ids(client, user \\ nil) do
42-
GenServer.call(client, {:get_all_variation_ids, user})
46+
GenServer.call(client, {:get_all_variation_ids, user}, Constants.fetch_timeout())
4347
end
4448

4549
@spec get_key_and_value(client(), Config.variation_id()) :: {Config.key(), Config.value()} | nil
4650
def get_key_and_value(client, variation_id) do
47-
GenServer.call(client, {:get_key_and_value, variation_id})
51+
GenServer.call(client, {:get_key_and_value, variation_id}, Constants.fetch_timeout())
4852
end
4953

5054
@spec force_refresh(client()) :: refresh_result()
5155
def force_refresh(client) do
52-
GenServer.call(client, :force_refresh)
56+
GenServer.call(client, :force_refresh, Constants.fetch_timeout())
5357
end
5458

5559
@impl GenServer

lib/config_cat/config_fetcher.ex

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ defmodule ConfigCat.CacheControlConfigFetcher do
2828
alias ConfigFetcher.RedirectMode
2929
alias HTTPoison.Response
3030

31-
require ConfigCat.Constants
32-
require ConfigFetcher.RedirectMode
31+
require Constants
32+
require RedirectMode
3333
require Logger
3434

3535
@type option ::
@@ -76,7 +76,7 @@ defmodule ConfigCat.CacheControlConfigFetcher do
7676

7777
@impl ConfigFetcher
7878
def fetch(fetcher) do
79-
GenServer.call(fetcher, :fetch)
79+
GenServer.call(fetcher, :fetch, Constants.fetch_timeout())
8080
end
8181

8282
@impl GenServer

lib/config_cat/constants.ex

+1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ defmodule ConfigCat.Constants do
1919
defmacro percentage, do: "p"
2020
defmacro value, do: "v"
2121
defmacro variation_id, do: "i"
22+
defmacro fetch_timeout, do: 10_000
2223
end

0 commit comments

Comments
 (0)