-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #201 from coryodaniel/mint-genserver
A GenServer for Mint connections
- Loading branch information
Showing
31 changed files
with
1,298 additions
and
490 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,8 +9,8 @@ jobs: | |
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
k8s_version: [v1.21.9-k3s1, v1.22.6-k3s1, latest] | ||
otp: [25.x] # with 24.3.0 hackney returns :checkout_failure | ||
k8s_version: [v1.24.9-k3s1, v1.25.5-k3s1, v1.26.0-k3s1] | ||
otp: [25.x] | ||
elixir: [1.14.x] | ||
steps: | ||
- uses: debianmaster/[email protected] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule K8s.Application do | ||
@moduledoc false | ||
|
||
use Application | ||
@impl true | ||
def start(_type, _args) do | ||
children = [ | ||
K8s.Client.Mint.ConnectionRegistry, | ||
{DynamicSupervisor, name: K8s.Client.Mint.ConnectionSupervisor, strategy: :one_for_one} | ||
] | ||
|
||
opts = [strategy: :one_for_one, name: K8s.Supervisor] | ||
Supervisor.start_link(children, opts) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
defmodule K8s.Client.Mint.ConnectionRegistry do | ||
@moduledoc """ | ||
Opens `Mint.HTTP2` connections and registers them in the GenServer state. | ||
""" | ||
|
||
use GenServer | ||
|
||
alias K8s.Client.HTTPError | ||
alias K8s.Client.Mint.HTTPAdapter | ||
|
||
@type uriopts :: {URI.t(), keyword()} | ||
|
||
@doc """ | ||
Starts the registry. | ||
""" | ||
@spec start_link(any) :: GenServer.on_start() | ||
def start_link(_) do | ||
GenServer.start_link(__MODULE__, :ok, name: __MODULE__) | ||
end | ||
|
||
@doc """ | ||
Ensures there is an adapter associated with the given `key`. | ||
""" | ||
@spec get(uriopts()) :: {:ok, pid()} | ||
def get({uri, opts}) do | ||
GenServer.call(__MODULE__, {:get_or_open, HTTPAdapter.connection_args(uri, opts)}) | ||
end | ||
|
||
@impl true | ||
def init(:ok) do | ||
adapters = %{} | ||
refs = %{} | ||
{:ok, {adapters, refs}} | ||
end | ||
|
||
@impl true | ||
def handle_call({:get_or_open, key}, _from, {adapters, refs}) do | ||
if Map.has_key?(adapters, key) do | ||
{:reply, {:ok, Map.get(adapters, key)}, {adapters, refs}} | ||
else | ||
case DynamicSupervisor.start_child( | ||
K8s.Client.Mint.ConnectionSupervisor, | ||
{HTTPAdapter, key} | ||
) do | ||
{:ok, adapter} -> | ||
ref = Process.monitor(adapter) | ||
refs = Map.put(refs, ref, adapter) | ||
adapters = Map.put(adapters, key, adapter) | ||
{:reply, {:ok, adapter}, {adapters, refs}} | ||
|
||
{:error, error} -> | ||
{:reply, {:error, HTTPError.from_exception(error)}, {adapters, refs}} | ||
end | ||
end | ||
end | ||
|
||
@impl true | ||
def handle_info({:DOWN, ref, :process, _pid, _reason}, {adapters, refs}) do | ||
{key, refs} = Map.pop(refs, ref) | ||
adapters = Map.delete(adapters, key) | ||
{:noreply, {adapters, refs}} | ||
end | ||
end |
Oops, something went wrong.