cowboy_loop.ezdoc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. ::: cowboy_loop
  2. The `cowboy_loop` module implements a handler interface for
  3. long running HTTP connections. It is the recommended interface
  4. for long polling and server-sent events, amongst others.
  5. This module is a sub protocol that defines three callbacks to
  6. be implemented by handlers. The `init/2` and `terminate/3`
  7. callbacks are common to all handler types and are documented
  8. in the manual for the ^cowboy_handler module.
  9. The `info/3` callback is specific to loop handlers and will be
  10. called as many times as necessary until a reply is sent.
  11. It is highly recommended to return a timeout value from the
  12. `init/2` callback to ensure that the process is terminated
  13. when no data has been received during that timespan. The
  14. default timeout is `infinity`, which should only be used if
  15. you have alternate means of ending inactive connections.
  16. :: Terminate reasons
  17. The following values may be received as the terminate reason
  18. in the optional `terminate/3` callback.
  19. : normal
  20. The connection was closed normally before switching to the
  21. loop sub protocol. This typically happens if an `ok` tuple is
  22. returned from the `init/2` callback.
  23. : stop
  24. The handler requested to close the connection by returning
  25. a `stop` tuple.
  26. : timeout
  27. The connection has been closed due to inactivity. The timeout
  28. value can be configured from `init/2`. The response sent when
  29. this happens is a `204 No Content`.
  30. : {crash, Class, Reason}
  31. A crash occurred in the handler. `Class` and `Reason` can be
  32. used to obtain more information about the crash. The function
  33. `erlang:get_stacktrace/0` can also be called to obtain the
  34. stacktrace of the process when the crash occurred.
  35. : {error, overflow}
  36. The connection is being closed and the process terminated
  37. because the buffer Cowboy uses to keep data sent by the
  38. client has reached its maximum. The buffer size can be
  39. configured through the environment value `loop_max_buffer`
  40. and defaults to 5000 bytes.
  41. If the long running request comes with a body it is recommended
  42. to process this body before switching to the loop sub protocol.
  43. : {error, closed}
  44. The socket has been closed brutally without a close frame being
  45. received first.
  46. : {error, Reason}
  47. A socket error ocurred.
  48. :: Callbacks
  49. : info(Info, Req, State)
  50. -> {ok, Req, State}
  51. | {ok, Req, State, hibernate}
  52. | {stop, Req, State}
  53. Types:
  54. * Info = any()
  55. * Req = cowboy_req:req()
  56. * State = any()
  57. Handle the Erlang message received.
  58. This function will be called every time an Erlang message
  59. has been received. The message can be any Erlang term.
  60. The `stop` return value can be used to stop the receive loop,
  61. typically because a response has been sent.
  62. The `hibernate` option will hibernate the process until
  63. it receives another message.