Skip to content

Commit 3745582

Browse files
committed
Support graceful shutdown on both HTTP/1.1 and HTTP/2
For HTTP/1.1, add Connection: close header For HTTP/2, send GOAWAY frames
1 parent 72b55ab commit 3745582

20 files changed

+90
-12
lines changed

iocore/net/I_NetProcessor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ class NetProcessor : public Processor
152152
153153
*/
154154
virtual Action *main_accept(Continuation *cont, SOCKET listen_socket_in, AcceptOptions const &opt = DEFAULT_ACCEPT_OPTIONS);
155+
virtual void stop_accept();
155156

156157
/**
157158
Open a NetVConnection for connection oriented I/O. Connects

iocore/net/P_NetAccept.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ struct NetAccept : public Continuation {
9898
void init_accept_loop(const char *);
9999
virtual void init_accept(EThread *t = nullptr);
100100
virtual void init_accept_per_thread();
101+
virtual void stop_accept();
101102
virtual NetAccept *clone() const;
102103

103104
// 0 == success

iocore/net/UnixNetAccept.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,15 @@ NetAccept::init_accept_per_thread()
197197
}
198198
}
199199

200+
void
201+
NetAccept::stop_accept()
202+
{
203+
if (!action_->cancelled) {
204+
action_->cancel();
205+
}
206+
server.close();
207+
}
208+
200209
int
201210
NetAccept::do_listen(bool non_blocking)
202211
{

iocore/net/UnixNetProcessor.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,14 @@ UnixNetProcessor::accept_internal(Continuation *cont, int fd, AcceptOptions cons
196196
return na->action_.get();
197197
}
198198

199+
void
200+
NetProcessor::stop_accept()
201+
{
202+
for (auto na = naVec.begin(); na != naVec.end(); ++na) {
203+
(*na)->stop_accept();
204+
}
205+
}
206+
199207
Action *
200208
UnixNetProcessor::connect_re_internal(Continuation *cont, sockaddr const *target, NetVCOptions *opt)
201209
{

proxy/Main.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ extern "C" int plock(int);
7575
#include "ProxyConfig.h"
7676
#include "HttpProxyServerMain.h"
7777
#include "HttpBodyFactory.h"
78+
#include "ProxyClientSession.h"
7879
#include "logging/Log.h"
7980
#include "CacheControl.h"
8081
#include "IPAllow.h"
@@ -276,7 +277,8 @@ class SignalContinuation : public Continuation
276277
REC_ReadConfigInteger(timeout, "proxy.config.stop.shutdown_timeout");
277278

278279
if (timeout) {
279-
http2_drain = true;
280+
http_client_session_draining = true;
281+
stop_HttpProxyServer();
280282
}
281283

282284
Debug("server", "received SIGTERM, shutting down in %" PRId64 "secs", timeout);

proxy/ProxyClientSession.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include "HttpDebugNames.h"
2626
#include "ProxyClientSession.h"
2727

28+
volatile bool http_client_session_draining = false;
29+
2830
static int64_t next_cs_id = 0;
2931

3032
ProxyClientSession::ProxyClientSession() : VConnection(nullptr)

proxy/ProxyClientSession.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#include "InkAPIInternal.h"
3131
#include "http/HttpServerSession.h"
3232

33+
extern volatile bool http_client_session_draining;
34+
3335
// Emit a debug message conditional on whether this particular client session
3436
// has debugging enabled. This should only be called from within a client session
3537
// member function.
@@ -119,6 +121,12 @@ class ProxyClientSession : public VConnection
119121
return m_active;
120122
}
121123

124+
bool
125+
is_draining() const
126+
{
127+
return http_client_session_draining;
128+
}
129+
122130
// Initiate an API hook invocation.
123131
void do_api_callout(TSHttpHookID id);
124132

proxy/http/HttpProxyServerMain.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,3 +321,10 @@ start_HttpProxyServerBackDoor(int port, int accept_threads)
321321
// The backdoor only binds the loopback interface
322322
netProcessor.main_accept(new HttpSessionAccept(ha_opt), NO_FD, opt);
323323
}
324+
325+
void
326+
stop_HttpProxyServer()
327+
{
328+
sslNetProcessor.stop_accept();
329+
netProcessor.stop_accept();
330+
}

proxy/http/HttpProxyServerMain.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ void init_HttpProxyServer(int n_accept_threads = 0);
3232
*/
3333
void start_HttpProxyServer();
3434

35+
void stop_HttpProxyServer();
36+
3537
void start_HttpProxyServerBackDoor(int port, int accept_threads = 0);
3638

3739
NetProcessor::AcceptOptions make_net_accept_options(const HttpProxyPort *port, unsigned nthreads);

proxy/http/HttpTransact.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7822,6 +7822,10 @@ HttpTransact::build_response(State *s, HTTPHdr *base_response, HTTPHdr *outgoing
78227822

78237823
HttpTransactHeaders::add_server_header_to_response(s->txn_conf, outgoing_response);
78247824

7825+
if (s->state_machine->ua_session && s->state_machine->ua_session->get_parent()->is_draining()) {
7826+
HttpTransactHeaders::add_connection_close(outgoing_response);
7827+
}
7828+
78257829
// auth-response update
78267830
// if (!s->state_machine->authAdapter.disabled()) {
78277831
// s->state_machine->authAdapter.UpdateResponseHeaders(outgoing_response);

0 commit comments

Comments
 (0)