loop_handlers.ezdoc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. ::: Loop handlers
  2. Loop handlers are a special kind of HTTP handlers used when the
  3. response can not be sent right away. The handler enters instead
  4. a receive loop waiting for the right message before it can send
  5. a response.
  6. Loop handlers are used for requests where a response might not
  7. be immediately available, but where you would like to keep the
  8. connection open for a while in case the response arrives. The
  9. most known example of such practice is known as long polling.
  10. Loop handlers can also be used for requests where a response is
  11. partially available and you need to stream the response body
  12. while the connection is open. The most known example of such
  13. practice is known as server-sent events.
  14. While the same can be accomplished using plain HTTP handlers,
  15. it is recommended to use loop handlers because they are well-tested
  16. and allow using built-in features like hibernation and timeouts.
  17. Loop handlers essentially wait for one or more Erlang messages
  18. and feed these messages to the `info/3` callback. It also features
  19. the `init/2` and `terminate/3` callbacks which work the same as
  20. for plain HTTP handlers.
  21. :: Initialization
  22. The `init/2` function must return a `cowboy_loop` tuple to enable
  23. loop handler behavior. This tuple may optionally contain
  24. a timeout value and/or the atom `hibernate` to make the
  25. process enter hibernation until a message is received.
  26. This snippet enables the loop handler.
  27. ``` erlang
  28. init(Req, Opts) ->
  29. {cowboy_loop, Req, Opts}.
  30. ```
  31. However it is largely recommended that you set a timeout
  32. value. The next example sets a timeout value of 30s and
  33. also makes the process hibernate.
  34. ``` erlang
  35. init(Req, Opts) ->
  36. {cowboy_loop, Req, Opts, 30000, hibernate}.
  37. ```
  38. :: Receive loop
  39. Once initialized, Cowboy will wait for messages to arrive
  40. in the process' mailbox. When a message arrives, Cowboy
  41. calls the `info/3` function with the message, the Req object
  42. and the handler's state.
  43. The following snippet sends a reply when it receives a
  44. `reply` message from another process, or waits for another
  45. message otherwise.
  46. ``` erlang
  47. info({reply, Body}, Req, State) ->
  48. Req2 = cowboy_req:reply(200, [], Body, Req),
  49. {stop, Req2, State};
  50. info(_Msg, Req, State) ->
  51. {ok, Req, State, hibernate}.
  52. ```
  53. Do note that the `reply` tuple here may be any message
  54. and is simply an example.
  55. This callback may perform any necessary operation including
  56. sending all or parts of a reply, and will subsequently
  57. return a tuple indicating if more messages are to be expected.
  58. The callback may also choose to do nothing at all and just
  59. skip the message received.
  60. If a reply is sent, then the `stop` tuple should be returned.
  61. This will instruct Cowboy to end the request.
  62. Otherwise an `ok` tuple should be returned.
  63. :: Streaming loop
  64. Another common case well suited for loop handlers is
  65. streaming data received in the form of Erlang messages.
  66. This can be done by initiating a chunked reply in the
  67. `init/2` callback and then using `cowboy_req:chunk/2`
  68. every time a message is received.
  69. The following snippet does exactly that. As you can see
  70. a chunk is sent every time a `chunk` message is received,
  71. and the loop is stopped by sending an `eof` message.
  72. ``` erlang
  73. init(Req, Opts) ->
  74. Req2 = cowboy_req:chunked_reply(200, [], Req),
  75. {cowboy_loop, Req2, Opts}.
  76. info(eof, Req, State) ->
  77. {stop, Req, State};
  78. info({chunk, Chunk}, Req, State) ->
  79. cowboy_req:chunk(Chunk, Req),
  80. {ok, Req, State};
  81. info(_Msg, Req, State) ->
  82. {ok, Req, State}.
  83. ```
  84. :: Cleaning up
  85. It is recommended that you set the connection header to
  86. `close` when replying, as this process may be reused for
  87. a subsequent request.
  88. Please refer to the ^"Handlers chapter^handlers
  89. for general instructions about cleaning up.
  90. :: Timeout
  91. By default Cowboy will not attempt to close the connection
  92. if there is no activity from the client. This is not always
  93. desirable, which is why you can set a timeout. Cowboy will
  94. close the connection if no data was received from the client
  95. after the configured time. The timeout only needs to be set
  96. once and can't be modified afterwards.
  97. Because the request may have had a body, or may be followed
  98. by another request, Cowboy is forced to buffer all data it
  99. receives. This data may grow to become too large though,
  100. so there is a configurable limit for it. The default buffer
  101. size is of 5000 bytes, but it may be changed by setting the
  102. `loop_max_buffer` middleware environment value.
  103. :: Hibernate
  104. To save memory, you may hibernate the process in between
  105. messages received. This is done by returning the atom
  106. `hibernate` as part of the `loop` tuple callbacks normally
  107. return. Just add the atom at the end and Cowboy will hibernate
  108. accordingly.