Skip to content

Commit d4621f4

Browse files
committed
UPD | slices (SSL): 4K instead of 64K
1 parent 12a59b1 commit d4621f4

File tree

4 files changed

+98
-56
lines changed

4 files changed

+98
-56
lines changed

include/worker/TLS.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ namespace manapi::net::worker {
7777
private:
7878
int check_read_stack_full_ (connection_interface *data);
7979

80-
int ssl_bio_flush_write_ (const shared_conn &conn, void *wbio, connection_io_part *top, int *cnt, int max_cnt);
80+
int ssl_bio_flush_write_ (const shared_conn &conn, void *wbio, connection_io *m, int max_cnt);
8181

8282
int ssl_bio_flush_read_ (const shared_conn &conn, void *rbio, connection_io_part *top, int *cnt, int max_cnt);
8383

include/worker/base_worker.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ namespace manapi::net::worker {
8686
struct connection_io {
8787
connection_io_part send;
8888
int send_size;
89+
int cur_send_size;
8990
connection_io_part recv;
9091
int recv_size;
9192
};

src/worker/TCP.cpp

Lines changed: 75 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -550,59 +550,96 @@ int manapi::net::worker::TCP::event_flags(const shared_conn & conn) {
550550

551551
void manapi::net::worker::TCP::flush_write_(const worker::shared_conn &connection, bool flush) {
552552
auto conn = connection->as<connection_interface>();
553-
bool flg = false;
554553

555-
while (conn->top->send.last_deque && ((conn->top->send.deque.get() != conn->top->send.last_deque
556-
|| (conn->top->send.deque->buffer.size() == conn->top->send.deque_cursor)) || flush)) {
557-
flg = true;
554+
if ((conn->top->cur_send_size >= 16
555+
&& ((conn->top->send.last_deque->buffer.size() == conn->top->send.deque_cursor)))
556+
|| (flush && conn->top->cur_send_size)) {
557+
std::unique_ptr<ev::buff_t, ev::buffer_deleter> s;
558+
s.reset(new ev::buff_t[conn->top->cur_send_size]);
558559

559-
auto object = std::move(conn->top->send.deque->buffer);
560-
conn->top->send.deque = std::move(conn->top->send.deque->next);
560+
auto const buffptr = s.get();
561+
std::unique_ptr<buffer_deque> sent = std::move(conn->top->send.deque);
562+
auto current = sent.get();
561563

562-
if (!conn->top->send.deque) {
563-
conn->top->send.last_deque = nullptr;
564-
object.resize(conn->top->send.deque_cursor);
565-
conn->top->send.deque_cursor = 0;
564+
for (int i = 0; i < conn->top->cur_send_size; i++) {
565+
auto &object = current->buffer;
566+
567+
if (current == conn->top->send.last_deque) {
568+
conn->top->send.last_deque = nullptr;
569+
object.resize(conn->top->send.deque_cursor);
570+
conn->top->send.deque_cursor = 0;
571+
}
572+
573+
if (conn->top->send.deque_current) {
574+
object.shift_add(conn->top->send.deque_current);
575+
conn->top->send.deque_current = 0;
576+
}
577+
578+
buffptr[i].base = object.data();
579+
buffptr[i].len = object.size();
580+
581+
if (i + 1 != conn->top->cur_send_size)
582+
current = current->next.get();
566583
}
567584

568-
if (conn->top->send.deque_current) {
569-
object.shift_add(conn->top->send.deque_current);
570-
conn->top->send.deque_current = 0;
585+
if (current && current->next)
586+
conn->top->send.deque = std::move(current->next);
587+
588+
auto rhs = conn->watcher->try_write(buffptr, conn->top->cur_send_size);
589+
uint32_t cursor = 0;
590+
while (cursor != conn->top->cur_send_size
591+
&& rhs >= buffptr[cursor].len) {
592+
rhs -= static_cast<ssize_t>(buffptr[cursor].len);
593+
sent = std::move(sent->next);
594+
cursor++;
571595
}
572596

573-
auto s = std::make_unique<ev::buff_t>();
574-
s->base = object.data();
575-
s->len = static_cast<std::size_t>(object.size());
597+
conn->top->cur_send_size -= cursor;
598+
conn->top->send_size -= cursor;
576599

577-
auto w = manapi::async::current()->eventloop()
578-
->create_watcher_write(conn->watcher.get(), [connection, b = std::move(object), s = std::move(s)]
579-
(std::shared_ptr<ev::write> &w, int status)
580-
-> void {
581-
auto conn = connection->as<connection_interface>();
600+
if (rhs && sent) {
601+
sent->buffer.shift_add(rhs);
602+
buffptr[cursor].base += rhs;
603+
buffptr[cursor].len -= rhs;
604+
}
605+
606+
if (conn->top->cur_send_size >= 16) {
607+
auto w = manapi::async::current()->eventloop()
608+
->create_watcher_write(conn->watcher.get(), [connection, b = std::move(sent), s = std::move(s)]
609+
(std::shared_ptr<ev::write> &w, int status)
610+
-> void {
611+
auto conn = connection->as<connection_interface>();
582612

583-
conn->top->send_size--;
613+
conn->top->send_size -= w->custom()->nbufs;
584614

585-
if (status) {
586-
/* error */
587-
conn->status |= ev::DISCONNECT;
588-
connection->cancellation.cancel();
615+
if (status) {
616+
/* error */
617+
conn->status |= ev::DISCONNECT;
618+
connection->cancellation.cancel();
589619

590-
if (conn->ev_callback) {
591-
conn->ev_callback->operator()(connection, ev::DISCONNECT, nullptr, 0, nullptr);
620+
if (conn->ev_callback)
621+
conn->ev_callback->operator()(connection, ev::DISCONNECT, nullptr, 0, nullptr);
592622
}
593-
}
594-
else {
595-
if (conn->status & ev::WRITE) {
596-
conn->ev_callback->operator()(connection, ev::WRITE, nullptr, 0, nullptr);
623+
else {
624+
if (conn->status & ev::WRITE)
625+
conn->ev_callback->operator()(connection, ev::WRITE, nullptr, 0, nullptr);
597626
}
598-
}
599627

600-
manapi::async::current()->eventloop()->stop_watcher(w);
601-
}, s.get(), 1 /* nbuf */);
602-
}
603-
604-
if (flg) {
628+
manapi::async::current()->eventloop()->stop_watcher(w);
629+
}, s.get() + cursor, conn->top->cur_send_size /* nbuf */);
605630

631+
conn->top->cur_send_size = 0;
632+
}
633+
else {
634+
if (sent) {
635+
assert (current);
636+
if (conn->top->send.last_deque) {
637+
current->next = std::move(conn->top->send.deque);
638+
}
639+
conn->top->send.deque = std::move(sent);
640+
conn->top->send.last_deque = current;
641+
}
642+
}
606643
}
607644
}
608645

src/worker/TLS.cpp

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,8 @@ ssize_t manapi::net::worker::TLS::sync_write_ex(const shared_conn &conn, ev::buf
9595
ssize_t res = 0;
9696

9797
while (res != buff[i].len) {
98-
if (connection->top->send_size >= maxcnt) {
99-
return res;
100-
}
98+
if (connection->top->send_size > maxcnt)
99+
return total;
101100

102101
auto rhs = this->ssl_write_(connection->ssl, static_cast<const char *> (buff[i].base) + res,
103102
static_cast<int>(buff[i].len - res));
@@ -118,17 +117,17 @@ ssize_t manapi::net::worker::TLS::sync_write_ex(const shared_conn &conn, ev::buf
118117
}
119118

120119
else if (err == this->ssl_error_want_read_) {
121-
return res;
120+
return total;
122121
}
123122

124123
else if (err == this->ssl_error_want_write_) {
125124
err = this->ssl_bio_flush_write_(conn, connection->wbio,
126-
&connection->top->send, &connection->top->send_size, maxcnt);
125+
connection->top.get(), maxcnt);
127126

128127
if (err) {
129128
if (err == CONN_IO_WANT_WRITE) {
130129
this->flush_write_(conn, cfinish);
131-
return res;
130+
return total;
132131
}
133132

134133
return CONN_IO_ERROR;
@@ -143,12 +142,12 @@ ssize_t manapi::net::worker::TLS::sync_write_ex(const shared_conn &conn, ev::buf
143142
}
144143

145144
err = this->ssl_bio_flush_write_(conn, connection->wbio,
146-
&connection->top->send, &connection->top->send_size, maxcnt);
145+
connection->top.get(), maxcnt);
147146

148147
if (err) {
149148
if (err == CONN_IO_WANT_WRITE) {
150149
this->flush_write_(conn, cfinish);
151-
return res;
150+
return total;
152151
}
153152

154153
return CONN_IO_ERROR;
@@ -300,7 +299,7 @@ void manapi::net::worker::TLS::onrecv(std::shared_ptr<ev::tcp> &watcher, const s
300299

301300
if (status == this->ssl_error_want_read_ || status == this->ssl_error_want_write_) {
302301
/* force write all data */
303-
if (this->ssl_bio_flush_write_(conn, data->wbio, &data->top->send, &data->top->send_size, 1e5)) {
302+
if (this->ssl_bio_flush_write_(conn, data->wbio, data->top.get(), 1e5)) {
304303
goto err;
305304
}
306305

@@ -380,7 +379,7 @@ void manapi::net::worker::TLS::accept_work_(const shared_conn &conn, int flags,
380379

381380
if (status == this->ssl_error_want_write_ || status == this->ssl_error_want_read_) {
382381
/* force write all data */
383-
if (this->ssl_bio_flush_write_(conn, data->wbio, &data->top->send, &data->top->send_size, 1e5)) {
382+
if (this->ssl_bio_flush_write_(conn, data->wbio, data->top.get(), 1e5)) {
384383
goto err;
385384
}
386385

@@ -421,7 +420,8 @@ void manapi::net::worker::TLS::accept_work_(const shared_conn &conn, int flags,
421420

422421
void manapi::net::worker::TLS::flush_write_(const shared_conn &connection, bool flush) {
423422
auto const data = connection->as<TLS::connection_interface>();
424-
if (data->top->send_size == 1 && data->top->send.last_deque) {
423+
if (flush && data->top->send_size == 1) {
424+
assert(data->top->send.last_deque);
425425
/* in the stack */
426426
auto &buffer = data->top->send.deque->buffer;
427427
auto copy = static_cast<int>(data->top->send.deque_cursor - data->top->send.deque_current);
@@ -433,6 +433,7 @@ void manapi::net::worker::TLS::flush_write_(const shared_conn &connection, bool
433433
data->top->send.deque_current = 0;
434434
data->top->send.deque_cursor = 0;
435435
data->top->send_size--;
436+
data->top->cur_send_size--;
436437
return;
437438
}
438439

@@ -453,20 +454,21 @@ int manapi::net::worker::TLS::check_read_stack_full_(connection_interface *data)
453454
return 0;
454455
}
455456

456-
int manapi::net::worker::TLS::ssl_bio_flush_write_(const shared_conn &conn, void *wbio, connection_io_part *top, int *cnt, int max_cnt) {
457+
int manapi::net::worker::TLS::ssl_bio_flush_write_(const shared_conn &conn, void *wbio, connection_io *m, int max_cnt) {
457458
int rhs;
458459
int flags = 0;
459460
buffer_deque *parent = nullptr;
461+
auto top = &m->send;
460462

461463
try {
462464
do {
463465
if (!top->last_deque || top->last_deque->buffer.size() == top->deque_cursor) {
464466
this->flush_write_(conn, false);
465467

466-
if (cnt && *cnt >= max_cnt)
468+
if (m->send_size > max_cnt)
467469
return CONN_IO_WANT_WRITE;
468470

469-
auto buffer = this->bufferpool().buffer(1, this->config_->buffer_size);
471+
auto buffer = this->bufferpool().buffer(this->config_->buffer_size);
470472

471473
auto obj = std::make_unique<buffer_deque>(std::move(buffer), nullptr);
472474
if (top->last_deque) {
@@ -483,8 +485,8 @@ int manapi::net::worker::TLS::ssl_bio_flush_write_(const shared_conn &conn, void
483485

484486
top->deque_cursor = 0;
485487
flags |= 1 /* an empty buffer was created */;
486-
if (cnt)
487-
(*cnt)++;
488+
m->send_size++;
489+
m->cur_send_size++;
488490
}
489491
else
490492
flags = 0;
@@ -503,7 +505,9 @@ int manapi::net::worker::TLS::ssl_bio_flush_write_(const shared_conn &conn, void
503505

504506
if (!rhs && (flags /* an empty buffer was created */ )) {
505507
/* remove an empty buffer at the end */
506-
connection_io_trim(top, parent, cnt);
508+
auto const prev = m->send_size;
509+
connection_io_trim(top, parent, &m->send_size);
510+
m->cur_send_size -= prev - m->send_size;
507511
}
508512
}
509513
while (rhs > 0);

0 commit comments

Comments
 (0)