| 1 |
[[loop_handlers]] |
| 2 |
== Loop handlers |
| 3 |
|
| 4 |
Loop handlers are a special kind of HTTP handlers used when the |
| 5 |
response can not be sent right away. The handler enters instead |
| 6 |
a receive loop waiting for the right message before it can send |
| 7 |
a response. |
| 8 |
|
| 9 |
Loop handlers are used for requests where a response might not |
| 10 |
be immediately available, but where you would like to keep the |
| 11 |
connection open for a while in case the response arrives. The |
| 12 |
most known example of such practice is known as long polling. |
| 13 |
|
| 14 |
Loop handlers can also be used for requests where a response is |
| 15 |
partially available and you need to stream the response body |
| 16 |
while the connection is open. The most known example of such |
| 17 |
practice is server-sent events, but it also applies to any |
| 18 |
response that takes a long time to send. |
| 19 |
|
| 20 |
While the same can be accomplished using plain HTTP handlers, |
| 21 |
it is recommended to use loop handlers because they are well-tested |
| 22 |
and allow using built-in features like hibernation and timeouts. |
| 23 |
|
| 24 |
Loop handlers essentially wait for one or more Erlang messages |
| 25 |
and feed these messages to the `info/3` callback. It also features |
| 26 |
the `init/2` and `terminate/3` callbacks which work the same as |
| 27 |
for plain HTTP handlers. |
| 28 |
|
| 29 |
=== Initialization |
| 30 |
|
| 31 |
The `init/2` function must return a `cowboy_loop` tuple to enable |
| 32 |
loop handler behavior. This tuple may optionally contain |
| 33 |
the atom `hibernate` to make the process enter hibernation |
| 34 |
until a message is received. Alternatively, the tuple may |
| 35 |
optionally contain a positive integer to create a `timeout` |
| 36 |
message when the process has not received messages for too |
| 37 |
long. |
| 38 |
|
| 39 |
This snippet enables the loop handler: |
| 40 |
|
| 41 |
[source,erlang] |
| 42 |
---- |
| 43 |
init(Req, State) -> |
| 44 |
{cowboy_loop, Req, State}. |
| 45 |
---- |
| 46 |
|
| 47 |
This also makes the process hibernate: |
| 48 |
|
| 49 |
[source,erlang] |
| 50 |
---- |
| 51 |
init(Req, State) -> |
| 52 |
{cowboy_loop, Req, State, hibernate}. |
| 53 |
---- |
| 54 |
|
| 55 |
This makes the process time out after 1000ms of idle time. |
| 56 |
|
| 57 |
[source,erlang] |
| 58 |
---- |
| 59 |
init(Req, State) -> |
| 60 |
{cowboy_loop, Req, State, 1000}. |
| 61 |
---- |
| 62 |
|
| 63 |
=== Receive loop |
| 64 |
|
| 65 |
Once initialized, Cowboy will wait for messages to arrive |
| 66 |
in the process' mailbox. When a message arrives, Cowboy |
| 67 |
calls the `info/3` function with the message, the Req object |
| 68 |
and the handler's state. |
| 69 |
|
| 70 |
The following snippet sends a reply when it receives a |
| 71 |
`reply` message from another process, or waits for another |
| 72 |
message otherwise. |
| 73 |
|
| 74 |
[source,erlang] |
| 75 |
---- |
| 76 |
info({reply, Body}, Req, State) -> |
| 77 |
cowboy_req:reply(200, #{}, Body, Req), |
| 78 |
{stop, Req, State}; |
| 79 |
info(_Msg, Req, State) -> |
| 80 |
{ok, Req, State, hibernate}. |
| 81 |
---- |
| 82 |
|
| 83 |
Do note that the `reply` tuple here may be any message |
| 84 |
and is simply an example. |
| 85 |
|
| 86 |
This callback may perform any necessary operation including |
| 87 |
sending all or parts of a reply, and will subsequently |
| 88 |
return a tuple indicating if more messages are to be expected. |
| 89 |
|
| 90 |
The callback may also choose to do nothing at all and just |
| 91 |
skip the message received. |
| 92 |
|
| 93 |
If a reply is sent, then the `stop` tuple should be returned. |
| 94 |
This will instruct Cowboy to end the request. |
| 95 |
|
| 96 |
Otherwise an `ok` tuple should be returned. |
| 97 |
|
| 98 |
=== Streaming loop |
| 99 |
|
| 100 |
Another common case well suited for loop handlers is |
| 101 |
streaming data received in the form of Erlang messages. |
| 102 |
This can be done by initiating a chunked reply in the |
| 103 |
`init/2` callback and then using `cowboy_req:chunk/2` |
| 104 |
every time a message is received. |
| 105 |
|
| 106 |
The following snippet does exactly that. As you can see |
| 107 |
a chunk is sent every time an `event` message is received, |
| 108 |
and the loop is stopped by sending an `eof` message. |
| 109 |
|
| 110 |
[source,erlang] |
| 111 |
---- |
| 112 |
init(Req, State) -> |
| 113 |
Req2 = cowboy_req:stream_reply(200, Req), |
| 114 |
{cowboy_loop, Req2, State}. |
| 115 |
|
| 116 |
info(eof, Req, State) -> |
| 117 |
{stop, Req, State}; |
| 118 |
info({event, Data}, Req, State) -> |
| 119 |
cowboy_req:stream_body(Data, nofin, Req), |
| 120 |
{ok, Req, State}; |
| 121 |
info(_Msg, Req, State) -> |
| 122 |
{ok, Req, State}. |
| 123 |
---- |
| 124 |
|
| 125 |
=== Cleaning up |
| 126 |
|
| 127 |
Please refer to the xref:handlers[Handlers chapter] |
| 128 |
for general instructions about cleaning up. |
| 129 |
|
| 130 |
=== Hibernate |
| 131 |
|
| 132 |
To save memory, you may hibernate the process in between |
| 133 |
messages received. This is done by returning the atom |
| 134 |
`hibernate` as part of the `loop` tuple callbacks normally |
| 135 |
return. Just add the atom at the end and Cowboy will hibernate |
| 136 |
accordingly. |
| 137 |
|
| 138 |
=== Idle timeout |
| 139 |
|
| 140 |
You may activate timeout events by returning a positive integer |
| 141 |
`N` as part of the `loop` tuple callbacks return. The default |
| 142 |
value is `infinity`. The `info` callback will be called with the |
| 143 |
atom `timeout` unless a message is received within `N` milliseconds: |
| 144 |
|
| 145 |
[source,erlang] |
| 146 |
---- |
| 147 |
info(timeout, Req, State) -> |
| 148 |
%% Do something... |
| 149 |
{ok, Req, State, 1000}. |
| 150 |
---- |