Skip to content

Instantly share code, notes, and snippets.

@yongboy
Last active June 6, 2020 22:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yongboy/5476302 to your computer and use it in GitHub Desktop.
Save yongboy/5476302 to your computer and use it in GitHub Desktop.
-module(cowboy_loop_handler).
-type opts() :: any().
-type state() :: any().
-type terminate_reason() :: {normal, shutdown}
| {normal, timeout}
| {error, atom()}.
%% 处理用户第一次请求,可以处理请求,为当前会话生成状态数据,进行等待或者休眠,或者关闭掉当前会话。
-callback init({atom(), http}, Req, opts())
-> {ok, Req, state()}
| {loop, Req, state()}
| {loop, Req, state(), hibernate}
| {loop, Req, state(), timeout()}
| {loop, Req, state(), timeout(), hibernate}
| {shutdown, Req, state()}
| {upgrade, protocol, module()}
| {upgrade, protocol, module(), Req, opts()}
when Req::cowboy_req:req().
%% init若返回loop状态信息,等待接收消息,可以继续接收,或者继续休眠
-callback info(any(), Req, State)
-> {ok, Req, State}
| {loop, Req, State}
| {loop, Req, State, hibernate}
when Req::cowboy_req:req(), State::state().
%% 会话终端时被调用,可执行会话清理工作
-callback terminate(terminate_reason(), cowboy_req:req(), state()) -> ok.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment