Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional configuration information to service handlers #70

Open
srstrong opened this issue Jan 13, 2022 · 2 comments
Open

Add optional configuration information to service handlers #70

srstrong opened this issue Jan 13, 2022 · 2 comments

Comments

@srstrong
Copy link

I have an issue where it would be nice to add an additional parameter to every service method with some configuration/state information provided when starting the service - so something along the lines of:

AdditionalOptions = ... %% some term
Server = #{grpc_opts => #{service_protos => [my_service], 
                                              services => #{'my.myService' => {my_service_handler, AdditionalOptions}}
                  ,...}
grpcbox:start_server(Server)

and then in my_service.erl:

my_service_function(Ctx, Input, AdditionalOptions) ->
 ...

or alternatively:

my_service_function(Ctx, Input) ->
  AdditionalOptions = ctx:get(Ctx, ctx_additional_options),
  ...

I guess the latter means that the generated behaviours don't need changing...

Any thoughts on a) whether this is a PR that you'd be interested in taking and b) if it is something you'd be interested in, views on what the usage should look like?

@tsloughter
Copy link
Owner

So just some additional configuration you can pass to the server handler? Makes sense to me. Surprised that isn't already possible, hehe, since the only other option I guess would be to be pulling that out of like application;get_env and that is poor form.

I wouldn't put it in the context, no. Assuming I understand the purpose, that it is values you need in the handler that you might otherwise read out of the application environment on each handler call instead of once at the start.

@srstrong
Copy link
Author

My scenario is that I'm spinning up multiple instances of a service dynamically on different ports, and each instance needs its own configuration information. I've already got a version working that extends #method to include the extra information, and then grpcbox_stream pulls it out and stores it in the ctx instance for that call. The handler can then pull it from the ctx and do whatever it needs to do. If I pass it as a parameter on the method calls, then I'm inclined to make it a list with a default of [] and always pass it, and make the corresponding update to grpcbox_plugin to add that extra argument (so a list(term())) to the callbacks that go in the behaviour.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants