mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-15 04:30:25 +00:00
Update cowboy_loop manual
This commit is contained in:
parent
b13c07932d
commit
5da7d1ef05
3 changed files with 90 additions and 71 deletions
|
@ -2,37 +2,64 @@
|
||||||
|
|
||||||
== Name
|
== Name
|
||||||
|
|
||||||
cowboy_loop - loop handlers
|
cowboy_loop - Loop handlers
|
||||||
|
|
||||||
== Description
|
== Description
|
||||||
|
|
||||||
The `cowboy_loop` module implements a handler interface for
|
The module `cowboy_loop` defines a callback interface for
|
||||||
long running HTTP connections. It is the recommended interface
|
long running HTTP connections.
|
||||||
for long polling and server-sent events, amongst others.
|
|
||||||
|
|
||||||
This module is a sub protocol that defines three callbacks to
|
You should switch to this behavior for long polling,
|
||||||
be implemented by handlers. The `init/2` and `terminate/3`
|
server-sent events and similar long-running requests.
|
||||||
callbacks are common to all handler types and are documented
|
|
||||||
in the manual for the link:cowboy_handler.asciidoc[cowboy_handler] module.
|
|
||||||
|
|
||||||
The `info/3` callback is specific to loop handlers and will be
|
There are generally two usage patterns:
|
||||||
called as many times as necessary until a reply is sent.
|
|
||||||
|
|
||||||
It is highly recommended to return a timeout value from the
|
* Loop until receiving a specific message, then send
|
||||||
`init/2` callback to ensure that the process is terminated
|
a response and stop execution (for example long polling);
|
||||||
when no data has been received during that timespan. The
|
|
||||||
default timeout is `infinity`, which should only be used if
|
|
||||||
you have alternate means of ending inactive connections.
|
|
||||||
|
|
||||||
== Terminate reasons
|
* Or initiate a response in `init/2` and stream the
|
||||||
|
body in `info/3` as necessary (for example server-sent events).
|
||||||
|
|
||||||
The following values may be received as the terminate reason
|
== Callbacks
|
||||||
in the optional `terminate/3` callback.
|
|
||||||
|
|
||||||
normal::
|
Loop handlers implement the following interface:
|
||||||
The connection was closed normally before switching to the
|
|
||||||
loop sub protocol. This typically happens if an `ok` tuple is
|
[source,erlang]
|
||||||
returned from the `init/2` callback.
|
----
|
||||||
|
init(Req, State)
|
||||||
|
-> {cowboy_loop, Req, State}
|
||||||
|
| {cowboy_loop, Req, State, hibernate}
|
||||||
|
| {cowboy_loop, Req, State, timeout()}
|
||||||
|
| {cowboy_loop, Req, State, timeout(), hibernate}
|
||||||
|
|
||||||
|
info(Info, Req, State)
|
||||||
|
-> {ok, Req, State}
|
||||||
|
| {ok, Req, State, hibernate}
|
||||||
|
| {stop, Req, State}
|
||||||
|
|
||||||
|
terminate(Reason, Req, State) -> ok %% optional
|
||||||
|
|
||||||
|
Req :: cowboy_req:req()
|
||||||
|
State :: any()
|
||||||
|
Info :: any()
|
||||||
|
Reason :: stop | timeout
|
||||||
|
| {crash, error | exit | throw, any()}
|
||||||
|
----
|
||||||
|
|
||||||
|
The `init/2` callback is common to all handlers. To switch
|
||||||
|
to the loop behavior, it must return `cowboy_loop` as the
|
||||||
|
first element of the tuple.
|
||||||
|
|
||||||
|
The `info/3` callback will be called for every Erlang message
|
||||||
|
received. It may choose to continue the receive loop or stop
|
||||||
|
it.
|
||||||
|
|
||||||
|
The optional `terminate/3` callback will ultimately be called
|
||||||
|
with the reason for the termination of the handler.
|
||||||
|
Cowboy will terminate the process right after this. There
|
||||||
|
is no need to perform any cleanup in this callback.
|
||||||
|
|
||||||
|
The following terminate reasons are defined for loop handlers:
|
||||||
|
|
||||||
stop::
|
stop::
|
||||||
The handler requested to close the connection by returning
|
The handler requested to close the connection by returning
|
||||||
|
@ -49,38 +76,29 @@ timeout::
|
||||||
`erlang:get_stacktrace/0` can also be called to obtain the
|
`erlang:get_stacktrace/0` can also be called to obtain the
|
||||||
stacktrace of the process when the crash occurred.
|
stacktrace of the process when the crash occurred.
|
||||||
|
|
||||||
{error, overflow}::
|
//{error, overflow}::
|
||||||
The connection is being closed and the process terminated
|
// The connection is being closed and the process terminated
|
||||||
because the buffer Cowboy uses to keep data sent by the
|
// because the buffer Cowboy uses to keep data sent by the
|
||||||
client has reached its maximum. The buffer size can be
|
// client has reached its maximum. The buffer size can be
|
||||||
configured through the environment value `loop_max_buffer`
|
// configured through the environment value `loop_max_buffer`
|
||||||
and defaults to 5000 bytes.
|
// and defaults to 5000 bytes.
|
||||||
+
|
// +
|
||||||
If the long running request comes with a body it is recommended
|
// If the long running request comes with a body it is recommended
|
||||||
to process this body before switching to the loop sub protocol.
|
// to process this body before switching to the loop sub protocol.
|
||||||
|
//
|
||||||
|
//{error, closed}::
|
||||||
|
// The socket has been closed brutally without a close frame being
|
||||||
|
// received first.
|
||||||
|
//
|
||||||
|
//{error, Reason}::
|
||||||
|
// A socket error ocurred.
|
||||||
|
|
||||||
{error, closed}::
|
== Changelog
|
||||||
The socket has been closed brutally without a close frame being
|
|
||||||
received first.
|
|
||||||
|
|
||||||
{error, Reason}::
|
* *2.0*: Cowboy temporarily no longer checks the socket for data with HTTP/1.1.
|
||||||
A socket error ocurred.
|
* *1.0*: Behavior introduced.
|
||||||
|
|
||||||
== Callbacks
|
== See also
|
||||||
|
|
||||||
=== info(Info, Req, State) -> {ok, Req, State} | {ok, Req, State, hibernate} | {stop, Req, State}
|
link:man:cowboy(7)[cowboy(7)],
|
||||||
|
link:man:cowboy_handler(3)[cowboy_handler(3)]
|
||||||
Info = any():: Message received by the process.
|
|
||||||
Req = cowboy_req:req():: The Req object.
|
|
||||||
State = any():: Handler state.
|
|
||||||
|
|
||||||
Handle the Erlang message received.
|
|
||||||
|
|
||||||
This function will be called every time an Erlang message
|
|
||||||
has been received. The message can be any Erlang term.
|
|
||||||
|
|
||||||
The `stop` return value can be used to stop the receive loop,
|
|
||||||
typically because a response has been sent.
|
|
||||||
|
|
||||||
The `hibernate` option will hibernate the process until
|
|
||||||
it receives another message.
|
|
||||||
|
|
|
@ -161,5 +161,6 @@ timeout::
|
||||||
== See also
|
== See also
|
||||||
|
|
||||||
link:man:cowboy(7)[cowboy(7)],
|
link:man:cowboy(7)[cowboy(7)],
|
||||||
|
link:man:cowboy_handler(3)[cowboy_handler(3)],
|
||||||
link:man:cowboy_http(3)[cowboy_http(3)],
|
link:man:cowboy_http(3)[cowboy_http(3)],
|
||||||
link:man:cowboy_http2(3)[cowboy_http2(3)]
|
link:man:cowboy_http2(3)[cowboy_http2(3)]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue