diff --git a/.gitignore b/.gitignore index 0a1640e8..8a1995dd 100644 --- a/.gitignore +++ b/.gitignore @@ -24,6 +24,5 @@ erl_crash.dump .elixir_ls -.elixir_tools grpc-*.tar diff --git a/benchmark/lib/benchmark/application.ex b/benchmark/lib/benchmark/application.ex deleted file mode 100644 index 49e7a0d6..00000000 --- a/benchmark/lib/benchmark/application.ex +++ /dev/null @@ -1,14 +0,0 @@ -defmodule Benchmark.Application do - @moduledoc false - use Application - - @impl true - def start(_type, _args) do - children = [ - {GRPC.Client.Supervisor, []} - ] - - opts = [strategy: :one_for_one, name: Benchmark.Supervisor] - Supervisor.start_link(children, opts) - end -end diff --git a/benchmark/mix.exs b/benchmark/mix.exs index d6521360..3e0ac018 100644 --- a/benchmark/mix.exs +++ b/benchmark/mix.exs @@ -15,7 +15,6 @@ defmodule Benchmark.MixProject do def application do [ extra_applications: [:logger], - mod: {Benchmark.Application, []} ] end diff --git a/grpc_client/guides/advanced/load_balancing.md b/grpc_client/guides/advanced/load_balancing.md index 272470f6..a222ab20 100644 --- a/grpc_client/guides/advanced/load_balancing.md +++ b/grpc_client/guides/advanced/load_balancing.md @@ -27,7 +27,6 @@ You can connect using `DNS`, `Unix Domain sockets`, and `IPv4/IPv6` for now. ### DNS ```elixir -iex> {:ok, _pid} = GRPC.Client.Supervisor.start_link() iex> {:ok, channel} = GRPC.Stub.connect("dns://orders.prod.svc.cluster.local:50051") iex> request = Orders.GetOrderRequest.new(id: "123") iex> {:ok, reply} = channel |> Orders.OrderService.Stub.get_order(request) diff --git a/grpc_client/guides/getting_started/client.md b/grpc_client/guides/getting_started/client.md index 1d748640..41810da4 100644 --- a/grpc_client/guides/getting_started/client.md +++ b/grpc_client/guides/getting_started/client.md @@ -6,23 +6,7 @@ This section demonstrates how to establish client connections and perform RPC ca ## Basic Connection and RPC -Typically, you start this client supervisor as part of your application's supervision tree: - -```elixir -children = [ - GRPC.Client.Supervisor -] - -opts = [strategy: :one_for_one, name: MyApp.Supervisor] -Supervisor.start_link(children, opts) -``` - -You can also start it manually in scripts or test environments: -```elixir -{:ok, _pid} = GRPC.Client.Supervisor.start_link() -``` - -Then connect with gRPC server: +Connect with gRPC server: ```elixir iex> {:ok, channel} = GRPC.Stub.connect("localhost:50051") diff --git a/grpc_client/lib/grpc/client/application.ex b/grpc_client/lib/grpc/client/application.ex new file mode 100644 index 00000000..16d4a230 --- /dev/null +++ b/grpc_client/lib/grpc/client/application.ex @@ -0,0 +1,13 @@ +defmodule GRPC.Client.Application do + @moduledoc false + use Application + + def start(_type, _args) do + children = [ + {DynamicSupervisor, [name: GRPC.Client.Supervisor]} + ] + + opts = [strategy: :one_for_one, name: GRPC.Supervisor] + Supervisor.start_link(children, opts) + end +end diff --git a/grpc_client/lib/grpc/client/connection.ex b/grpc_client/lib/grpc/client/connection.ex index d880929c..87d573a0 100644 --- a/grpc_client/lib/grpc/client/connection.ex +++ b/grpc_client/lib/grpc/client/connection.ex @@ -153,26 +153,6 @@ defmodule GRPC.Client.Connection do """ @spec connect(String.t(), keyword()) :: {:ok, Channel.t()} | {:error, any()} def connect(target, opts \\ []) do - supervisor_pid = Process.whereis(GRPC.Client.Supervisor) - - if is_nil(supervisor_pid) or !Process.alive?(supervisor_pid) do - raise """ - GRPC.Client.Supervisor is not running. Please ensure it is started as part of your - application's supervision tree: - - children = [ - {GRPC.Client.Supervisor, []} - ] - - opts = [strategy: :one_for_one, name: MyApp.Supervisor] - Supervisor.start_link(children, opts) - - You can also start it manually in scripts or test environments: - - {:ok, _pid} = DynamicSupervisor.start_link(strategy: :one_for_one, name: GRPC.Client.Supervisor) - """ - end - ref = make_ref() case build_initial_state(target, Keyword.merge(opts, ref: ref)) do @@ -184,7 +164,6 @@ defmodule GRPC.Client.Connection do {:ok, ch} {:error, {:already_started, _pid}} -> - # race: someone else started it first, ask the running process for its current channel case pick_channel(opts) do {:ok, %Channel{} = channel} -> {:ok, channel} diff --git a/grpc_client/lib/grpc/client/supervisor.ex b/grpc_client/lib/grpc/client/supervisor.ex deleted file mode 100644 index ba2d53ed..00000000 --- a/grpc_client/lib/grpc/client/supervisor.ex +++ /dev/null @@ -1,63 +0,0 @@ -defmodule GRPC.Client.Supervisor do - @moduledoc """ - A DynamicSupervisor responsible for managing gRPC client connections (`GRPC.Client.Connection`). - - This supervisor allows you to dynamically start and stop gRPC client connections at runtime. - Each connection is run as a separate `GenServer` under this supervisor, which ensures proper - supervision and isolation between connections. - - ## Starting the Supervisor - - Typically, you start this supervisor as part of your application's supervision tree: - - children = [ - GRPC.Client.Supervisor - ] - - opts = [strategy: :one_for_one, name: MyApp.Supervisor] - Supervisor.start_link(children, opts) - - You can also start it manually in scripts or test environments: - - {:ok, _pid} = GRPC.Client.Supervisor.start_link([]) - - ## Supervision Strategy - - This supervisor uses `:one_for_one` strategy: - - * If a connection process crashes, only that process is restarted. - * Other running connections remain unaffected. - - ## Establishing a gRPC Connection - - To create a new gRPC connection, you typically use the `GRPC.Stub.connect/1` function, - which internally starts a `GRPC.Client.Connection` process under this supervisor. For example: - - iex> {:ok, ch} = GRPC.Stub.connect("127.0.0.1:50051") - iex> Grpc.Testing.TestService.Stub.empty_call(ch, %{}) - - ## Notes - - * You can dynamically start multiple connections under the supervisor for different targets. - * Each connection runs in isolation as its own GenServer. - """ - use DynamicSupervisor - - def start_link(opts) do - case DynamicSupervisor.start_link(__MODULE__, opts, name: __MODULE__) do - {:ok, _pid} = started -> - started - - {:error, {:already_started, pid}} -> - {:ok, pid} - - other -> - other - end - end - - @impl true - def init(_opts) do - DynamicSupervisor.init(strategy: :one_for_one) - end -end diff --git a/grpc_client/mix.exs b/grpc_client/mix.exs index 0fb4fcb6..7e01b3a3 100644 --- a/grpc_client/mix.exs +++ b/grpc_client/mix.exs @@ -22,6 +22,7 @@ defmodule GrpcClient.MixProject do def application do [ + mod: {GRPC.Client.Application, []}, extra_applications: [:logger] ] end diff --git a/grpc_client/test/grpc/supervisor_test.exs b/grpc_client/test/grpc/supervisor_test.exs deleted file mode 100644 index cd83c871..00000000 --- a/grpc_client/test/grpc/supervisor_test.exs +++ /dev/null @@ -1,14 +0,0 @@ -defmodule GRPC.Client.SupervisorTest do - use ExUnit.Case, async: false - - alias GRPC.Client - - describe "start_link/1" do - test "allows multiple start_links" do - {:ok, second_pid} = Client.Supervisor.start_link([]) - {:ok, third_pid} = Client.Supervisor.start_link([]) - - assert second_pid == third_pid - end - end -end diff --git a/grpc_client/test/test_helper.exs b/grpc_client/test/test_helper.exs index b98144e0..5d228d32 100644 --- a/grpc_client/test/test_helper.exs +++ b/grpc_client/test/test_helper.exs @@ -5,11 +5,4 @@ Mox.defmock(GRPC.Client.Resolver.DNS.MockAdapter, for: GRPC.Client.Resolver.DNS.Adapter ) -# Start client supervisor for integration tests -{:ok, _pid} = - DynamicSupervisor.start_link( - strategy: :one_for_one, - name: GRPC.Client.Supervisor - ) - ExUnit.start(capture_log: true) diff --git a/interop/lib/interop/app.ex b/interop/lib/interop/app.ex index 9e880ab5..f67a072e 100644 --- a/interop/lib/interop/app.ex +++ b/interop/lib/interop/app.ex @@ -3,7 +3,6 @@ defmodule Interop.App do def start(_type, _args) do children = [ - GRPC.Client.Supervisor, {GRPC.Server.Supervisor, endpoint: Interop.Endpoint, port: 10000} ]