Skip to content

Commit 2127f4b

Browse files
committed
UPD | graceful shutdown
1 parent 56f7de3 commit 2127f4b

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

src/worker/ManapiTcp.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ int manapi::net::worker::TCP::flush_write_(const worker::shared_conn &connection
667667
auto conn = connection->as<tcp_connection_t>();
668668

669669
try {
670-
if (conn->top->cur_send_size) {
670+
if (conn->top && conn->top->cur_send_size) {
671671
//std::cout << "flush " << flush << " "<<(bool)conn->top->cur_send_size << " " << (bool)conn->top->send.deque << "\n";
672672
while (conn->top->cur_send_size && ((conn->top->cur_send_size >= this->config_->max_merge_buffer_stack)
673673
//|| ((conn->top->cur_send_size == this->config_->max_merge_buffer_stack) && (conn->top->send.last_deque->buffer.size() == conn->top->send.deque_cursor))
@@ -865,8 +865,6 @@ void manapi::net::worker::TCP::timeout_(shared_conn conn) MANAPIHTTP_NOEXCEPT {
865865

866866
prepared::timer_clear(std::move(data->t));
867867

868-
data->flags |= (ev::DISCONNECT);
869-
870868
if (conn->wrk.flags & WRK_INTERFACE_TCP_KEEP_ALIVE)
871869
conn->wrk.flags ^= WRK_INTERFACE_TCP_KEEP_ALIVE;
872870

src/worker/ManapiTlsOverTcp.cpp

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
struct ssl_bio_deleter_t;
2222

2323
enum conn_tls_flags {
24-
CONN_TLS_SHUTDOWN = manapi::net::worker::base::CONN_MAX_CODE * 2,
25-
CONN_TLS_EARLY_DATA = CONN_TLS_SHUTDOWN * 2,
26-
CONN_TLS_EARLY_FINISHED = CONN_TLS_EARLY_DATA * 2
24+
CONN_TLS_SHUTDOWN = manapi::net::worker::base::CONN_MAX_CODE << 1,
25+
CONN_TLS_EARLY_DATA = CONN_TLS_SHUTDOWN << 1,
26+
CONN_TLS_EARLY_FINISHED = CONN_TLS_EARLY_DATA<<1
2727
};
2828

2929
manapi::net::worker::TLS::TLS(net::http::site site, std::shared_ptr<multithread_storage::worker_t> wdata, manapi::net::http::config *config) : TCP(std::move(site), std::move(wdata), config) {}
@@ -77,6 +77,8 @@ void manapi::net::worker::TLS::close_connection(shared_conn conn, int flags) MAN
7777
)
7878
))) {
7979

80+
assert(!(flags & CLOSE_CONN_ERR));
81+
8082
if (conn->wrk.flags & WRK_INTERFACE_TCP_KEEP_ALIVE)
8183
conn->wrk.flags ^= WRK_INTERFACE_TCP_KEEP_ALIVE;
8284

@@ -98,12 +100,12 @@ void manapi::net::worker::TLS::close_connection(shared_conn conn, int flags) MAN
98100
if (flags & CLOSE_CONN_EOF)
99101
this->ssl_set_shutdown_(connection->ssl,
100102
this->ssl_recv_shutdown_|this->ssl_send_shutdown_);
101-
102-
if (connection->accept_timer) {
103-
prepared::timer_clear(std::move(connection->accept_timer));
104-
105-
flags = CLOSE_CONN_EOF;
106-
}
103+
//
104+
// if (connection->accept_timer) {
105+
// prepared::timer_clear(std::move(connection->accept_timer));
106+
//
107+
// flags = CLOSE_CONN_EOF;
108+
// }
107109

108110
TCP::close_connection(conn, flags);
109111

@@ -275,7 +277,7 @@ void manapi::net::worker::TLS::update_limit_rate_connection(const shared_conn &s
275277

276278
this->read_start_(data);
277279

278-
if (!(data->flags & ev::DISCONNECT) && data->flags & ev::WRITE && data->ev_callback) {
280+
if (!(data->flags & ev::DISCONNECT) && (data->flags & ev::WRITE) && data->ev_callback) {
279281
if (manapi::net::worker::base::call_user_callback(&data->ev_callback, sconn, ev::WRITE, nullptr, 0, nullptr)) {
280282
this->close_connection(sconn, CLOSE_CONN_ERR);
281283
return;
@@ -351,8 +353,8 @@ void manapi::net::worker::TLS::shutdown_async_(shared_conn conn) {
351353
if (s->accept_timer) {
352354
s->accept_timer.stop();
353355
s->accept_timer = nullptr;
354-
355356
}
357+
356358
auto accept_timer_res = manapi::async::current()->timerpool()->append_timer_sync(5000, [wconn = std::weak_ptr (conn)] (manapi::timer t)
357359
-> void { auto conn = wconn.lock(); if (conn) { auto w = conn->as<tcp_connection_t>(); w->worker->close_connection(conn, CLOSE_CONN_EOF); } });
358360
if (accept_timer_res)
@@ -367,7 +369,9 @@ void manapi::net::worker::TLS::shutdown_async_(shared_conn conn) {
367369
auto rhs = this->ssl_shutdown_(s->ssl);
368370
manapi_log_trace(debug::LOG_TRACE_LOW, "TLS:Shutdown %p = %d", s, rhs);
369371

372+
370373
if (!rhs) {
374+
this->read_start_(s);
371375
goto write;
372376
}
373377

@@ -454,10 +458,13 @@ void manapi::net::worker::TLS::onrecv(const std::shared_ptr<ev::tcp> &watcher, c
454458
this->read_stop_(data);
455459
}
456460

461+
manapi_log_trace_hard("TLS:recv %p flags=%d size=%zu", data, data->flags, buffer.size());
462+
457463
while (size) {
458464
auto rhs = this->ssl_bio_write_(data->rbio, buff, static_cast<int>(size));
459465
if (rhs <= 0) {
460466
/* error */
467+
data->flags |= ev::DISCONNECT;
461468
goto err;
462469
}
463470

@@ -466,6 +473,10 @@ void manapi::net::worker::TLS::onrecv(const std::shared_ptr<ev::tcp> &watcher, c
466473

467474
/* TODO: resolve this dump logic */
468475

476+
if (data->flags & CONN_TLS_SHUTDOWN) {
477+
assert(!(data->flags & CONN_CLOSED));
478+
}
479+
469480
while (!(data->flags & CONN_CLOSED)) {
470481
if (ssl_is_init_fininshed_(data->ssl)) {
471482
rhs = manapi_do_process (conn, data);
@@ -515,7 +526,7 @@ void manapi::net::worker::TLS::onrecv(const std::shared_ptr<ev::tcp> &watcher, c
515526
}
516527

517528
break;
518-
}
529+
}
519530

520531
// if (err == this->ssl_error_syscall_
521532
// && !this->ssl_is_init_fininshed_(data->ssl))
@@ -531,7 +542,7 @@ void manapi::net::worker::TLS::onrecv(const std::shared_ptr<ev::tcp> &watcher, c
531542
rhs = manapi_do_process (conn, data);
532543
if (rhs == CONN_IO_ERROR)
533544
goto err;
534-
}
545+
}
535546
else {
536547
rhs = manapi_do_handshake_ (conn, data);
537548
switch (rhs) {
@@ -563,7 +574,6 @@ void manapi::net::worker::TLS::onrecv(const std::shared_ptr<ev::tcp> &watcher, c
563574
err: {
564575
auto const cdata = conn->as<tls_connection_t>();
565576

566-
cdata->flags |= ev::DISCONNECT;
567577
conn->cancellation.cancel();
568578

569579
this->close_connection(conn, CLOSE_CONN_SHUTDOWN);
@@ -633,6 +643,9 @@ int manapi::net::worker::TLS::manapi_do_process(const shared_conn &conn, tls_con
633643
this->shutdown_async_(conn);
634644
}
635645
else {
646+
if (data->accept_timer)
647+
prepared::timer_clear(std::move(data->accept_timer));
648+
636649
if (conn->wrk.flags & WRK_INTERFACE_CUSTOM_READ) {
637650
int cursor = 0;
638651

@@ -784,7 +797,7 @@ int manapi::net::worker::TLS::ssl_bio_flush_write_(const shared_conn &conn, tls_
784797
ssize_t nfastfast = 0;
785798

786799
try {
787-
while (!(m->flags & CONN_CLOSED)) {
800+
while (m->top) {
788801
if (max_cnt < m->top->send_size)
789802
break;
790803

@@ -799,7 +812,8 @@ int manapi::net::worker::TLS::ssl_bio_flush_write_(const shared_conn &conn, tls_
799812

800813
if (rhs > 0) {
801814
ssize_t alr = 0;
802-
if (!m->top->send_size && rhs > 32) {
815+
if (!m->top->send_size && (rhs > 32 || (m->flags & CONN_TLS_SHUTDOWN))) {
816+
manapi_log_trace_hard("TLS:write %p flags=%d size=%zu", m, m->flags, rhs);
803817
alr = m->watcher->try_write(fastfast, rhs);
804818
if (alr < 0) {
805819
if (alr == ev::ERR_AGAIN)

0 commit comments

Comments
 (0)