I have cowboy rest handler which should spawn couple gen_servers. What is the best way to send/handle messages in my cowboy handler? Is there only one possibility to send message from gen_server to handler using "!" operator?
Asked
Active
Viewed 885 times
2 Answers
4
Cowboy spawns an Erlang process with each request. You could use standard message passing but I don't think it would lead to an elegant solution.
Why not use gen_server:call
and gen_server:cast
to send a message from the cowboy handler? Even better when you wrap call
and cast
in a proper API.

Tilman
- 2,015
- 14
- 16
-
Yes, I use cast/call when message goes from cowboy to custom gen_server, but then gen_server should send an answer and it seems it's only possible with "!" operator. – Max Grigoriev Jun 05 '13 at 18:02
-
`cast` will always return `ok`. It's intended for asynchronous processing. For `call`s you can reply with `{reply, my_response_message, State}.` at the end of a `handle_call`. The caller will receive `my_response_message`. – Tilman Jun 05 '13 at 18:17
0
It sounds like you want to use Cowboy's loop handler concept. Make your handler a loop handler and then send messages to it from your gen_servers using !

Roger Lipscombe
- 89,048
- 55
- 235
- 380

johlo
- 5,422
- 1
- 18
- 32
-
-
No, that's not possible, REST handler in cowboy designed around concept of "operation on resource", so it is not the good place to do some hard stuff. Anyway, you could handcraft your own loop handler for your problem. But for me, it seems that existing REST handler, which basically spawn new `gen_server`s and do `call` inside them, should suffice. – Keynslug Jun 10 '13 at 12:39
-
yes, it's pity that erlang world is not fully implemented as Java and many actions should be reimplemented by each developer again and again :( – Max Grigoriev Jun 10 '13 at 18:46