elixir-grpc / grpc

An Elixir implementation of gRPC
https://hex.pm/packages/grpc
Apache License 2.0
1.4k stars 214 forks source link

Running multiple servers with decision in runtime with GRPC.Endpoint #306

Open hamir-suspect opened 1 year ago

hamir-suspect commented 1 year ago

Describe the question I tried incoorporating GRPC.Endpoint and interceptors into my project, the issue I have with it is that it does not seem posible to decide in runtime what servers to run. I have some mock grpc servers that are used for dev and test enviorments, but also in runtime I start some of the servers based on some enviorment flags, any ideas on how this can be accomplished? If needed I would be glad to contribute to the project as well if provided with some guidance. For the dev and test envs I can leave those servers out when building the prod app, but the decision with env flags does not seem possible since macros expand when compiling. Versions:

beligante commented 1 year ago

Not sure if I fully understand your issue, but you can decide which of them to run by passing a list of endpoints to your app supervision tree.

  def start(_type, _args) do
    endpoints = [MyApp.Endpoint.One, MyApp.Endpoint.Two]
    children = [{GRPC.Server.Supervisor, endpoint: endoints, port: 10000}]

    opts = [strategy: :one_for_one, name: __MODULE__]
    Supervisor.start_link(children, opts)
  end

The list of endpoints you can decide in run time when starting the app by checking some config. You can also decide not to append the server supervisor into your app supervision tree

hamir-suspect commented 1 year ago

I will try that out, the issue might have happened because I had a mix of endpoints and servers in the list of arguments for GRPC.Server.Supervisor

polvalente commented 1 year ago

@hamir-suspect you can have that list be decided at startup though config, for instance.

If you need those to be changed during your application's runtime instead, you should look up Dynamic Supervisors