From c974b4334e7ab660f9bf95653696c3663c02ead3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Thu, 8 Aug 2019 16:33:09 +0200 Subject: Implement graceful shutdown The graceful shutdown is implemented through a new 'closing' state. This state is entered under different circumstances depending on the protocol. The gun:shutdown/1 function is now implemented and documented. It allows shutting down the connection gracefully regardless of the current state of the connection and for all protocols. The behavior is entirely dependent on the protocol. For HTTP/1.1 the connection stays up only until after the current stream is complete; other streams are immediately canceled. For HTTP/2 a GOAWAY frame is sent and existing streams continue to be processed. The connection is closed after all streams are processed and the server's GOAWAY frame is received. For Websocket a close frame is sent. The connection is closed when receiving the server's close frame. In all cases the closing_timeout option defines how long we wait, as a maximum, before closing the connection after the graceful shutdown was started. The graceful shutdown is also initiated when the owner process goes away; when sending an HTTP/1.1 request with the connection: close header; when receiving an HTTP/1.1 response with the connection: close header; when receiving an HTTP/1.0 response without a connection header; when the server sends a GOAWAY HTTP/2 frame; or when we send or receive a Websocket close frame. Along with these changes, the gun:ws_send/2 function now accepts a list of frames as argument. Those frames may include a close frame that initiates the graceful shutdown. --- test/ws_SUITE.erl | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'test/ws_SUITE.erl') diff --git a/test/ws_SUITE.erl b/test/ws_SUITE.erl index 5cc50ec..1abf046 100644 --- a/test/ws_SUITE.erl +++ b/test/ws_SUITE.erl @@ -68,3 +68,30 @@ reject_upgrade(Config) -> after 1000 -> error(timeout) end. + +send_many(Config) -> + doc("Ensure we can send a list of frames in one gun:ws_send call."), + {ok, ConnPid} = gun:open("localhost", config(port, Config)), + {ok, _} = gun:await_up(ConnPid), + StreamRef = gun:ws_upgrade(ConnPid, "/", []), + {upgrade, [<<"websocket">>], _} = gun:await(ConnPid, StreamRef), + Frame1 = {text, <<"Hello!">>}, + Frame2 = {binary, <<"World!">>}, + gun:ws_send(ConnPid, [Frame1, Frame2]), + {ws, Frame1} = gun:await(ConnPid, StreamRef), + {ws, Frame2} = gun:await(ConnPid, StreamRef), + gun:close(ConnPid). + +send_many_close(Config) -> + doc("Ensure we can send a list of frames in one gun:ws_send call, including a close frame."), + {ok, ConnPid} = gun:open("localhost", config(port, Config)), + {ok, _} = gun:await_up(ConnPid), + StreamRef = gun:ws_upgrade(ConnPid, "/", []), + {upgrade, [<<"websocket">>], _} = gun:await(ConnPid, StreamRef), + Frame1 = {text, <<"Hello!">>}, + Frame2 = {binary, <<"World!">>}, + gun:ws_send(ConnPid, [Frame1, Frame2, close]), + {ws, Frame1} = gun:await(ConnPid, StreamRef), + {ws, Frame2} = gun:await(ConnPid, StreamRef), + {ws, close} = gun:await(ConnPid, StreamRef), + gun:close(ConnPid). -- cgit v1.2.3