2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-31 06:25:31 +00:00

Refactor the isc__nm_uvreq_t to have idle callback

Change the isc__nm_uvreq_t to have the idle callback as a separate
member as we always need to use it to properly close the uvreq.

Slightly refactor uvreq_put and uvreq_get to remove the unneeded
arguments - in uvreq_get(), we always use sock->worker, and in
uvreq_put, we always use req->sock, so there's not reason to pass those
extra arguments.
This commit is contained in:
Ondřej Surý
2023-03-24 12:11:44 +01:00
parent 476198f26c
commit 639d5065a3
7 changed files with 81 additions and 92 deletions

View File

@@ -48,13 +48,6 @@
#include "openssl_shim.h"
#include "trampoline_p.h"
/*%
* How many isc_nmhandles and isc_nm_uvreqs will we be
* caching for reuse in a socket.
*/
#define ISC_NM_HANDLES_STACK_SIZE 600
#define ISC_NM_REQS_STACK_SIZE 600
/*%
* Shortcut index arrays to get access to statistics counters.
*/
@@ -127,6 +120,9 @@ nmsocket_maybe_destroy(isc_nmsocket_t *sock FLARG);
static void
nmhandle_free(isc_nmsocket_t *sock, isc_nmhandle_t *handle);
static void
uvreq_free(uv_handle_t *handle);
/*%<
* Issue a 'handle closed' callback on the socket.
*/
@@ -249,8 +245,6 @@ isc_netmgr_create(isc_mem_t *mctx, isc_loopmgr_t *loopmgr, isc_nm_t **netmgrp) {
isc_mempool_create(worker->mctx, sizeof(isc__nm_uvreq_t),
&worker->uvreq_pool);
isc_mempool_setfreemax(worker->uvreq_pool,
ISC_NM_REQS_STACK_SIZE);
isc_loop_attach(loop, &worker->loop);
isc_loop_teardown(loop, networker_teardown, worker);
@@ -872,7 +866,7 @@ dequeue_handle(isc_nmsocket_t *sock) {
return (handle);
}
#else
UNUSED(sock);
INSIST(ISC_LIST_EMPTY(sock->inactive_handles));
#endif /* !__SANITIZE_ADDRESS__ && !__SANITIZE_THREAD__ */
return (NULL);
}
@@ -1081,7 +1075,7 @@ isc__nm_failed_send_cb(isc_nmsocket_t *sock, isc__nm_uvreq_t *req,
if (req->cb.send != NULL) {
isc__nm_sendcb(sock, req, eresult, async);
} else {
isc__nm_uvreq_put(&req, sock);
isc__nm_uvreq_put(&req);
}
}
@@ -1348,7 +1342,7 @@ isc__nm_uvreq_t *
isc__nm_get_read_req(isc_nmsocket_t *sock, isc_sockaddr_t *sockaddr) {
isc__nm_uvreq_t *req = NULL;
req = isc__nm_uvreq_get(sock->worker, sock);
req = isc__nm_uvreq_get(sock);
req->cb.recv = sock->recv_cb;
req->cbarg = sock->recv_cbarg;
@@ -1595,57 +1589,62 @@ isc_nmhandle_netmgr(isc_nmhandle_t *handle) {
}
isc__nm_uvreq_t *
isc___nm_uvreq_get(isc__networker_t *worker, isc_nmsocket_t *sock FLARG) {
isc__nm_uvreq_t *req = NULL;
REQUIRE(worker != NULL);
isc___nm_uvreq_get(isc_nmsocket_t *sock FLARG) {
REQUIRE(VALID_NMSOCK(sock));
REQUIRE(sock->tid == isc_tid());
req = isc_mempool_get(worker->uvreq_pool);
isc__networker_t *worker = sock->worker;
isc__nm_uvreq_t *req = isc_mempool_get(worker->uvreq_pool);
*req = (isc__nm_uvreq_t){
.connect_tries = 3,
.link = ISC_LINK_INITIALIZER,
.inactive_link = ISC_LINK_INITIALIZER,
.uv_req.req.data = req,
.active_link = ISC_LINK_INITIALIZER,
.magic = UVREQ_MAGIC,
};
uv_handle_set_data(&req->uv_req.handle, req);
int r = uv_idle_init(&worker->loop->loop, &req->idle);
UV_RUNTIME_CHECK(uv_idle_init, r);
uv_handle_set_data(&req->idle, req);
isc___nmsocket_attach(sock, &req->sock FLARG_PASS);
ISC_LIST_APPEND(sock->active_uvreqs, req, active_link);
return (req);
}
static void
uvreq_free(uv_handle_t *handle) {
isc__nm_uvreq_t *req = uv_handle_get_data(handle);
isc_nmsocket_t *sock = req->sock;
isc_mempool_put(sock->worker->uvreq_pool, req);
isc___nmsocket_detach(&sock FLARG_PASS);
}
void
isc___nm_uvreq_put(isc__nm_uvreq_t **req0, isc_nmsocket_t *sock FLARG) {
REQUIRE(req0 != NULL);
REQUIRE(VALID_UVREQ(*req0));
REQUIRE(VALID_NMSOCK(sock));
REQUIRE(sock->tid == isc_tid());
isc___nm_uvreq_put(isc__nm_uvreq_t **reqp FLARG) {
REQUIRE(reqp != NULL && VALID_UVREQ(*reqp));
isc__nm_uvreq_t *req = NULL;
isc_nmhandle_t *handle = NULL;
isc__networker_t *worker = sock->worker;
isc__nm_uvreq_t *req = *reqp;
isc_nmhandle_t *handle = req->handle;
isc_nmsocket_t *sock = req->sock;
req = *req0;
*req0 = NULL;
*reqp = NULL;
req->handle = NULL;
INSIST(sock == req->sock);
REQUIRE(VALID_UVREQ(req));
req->magic = 0;
/*
* We need to save this first to make sure that handle,
* sock, and the netmgr won't all disappear.
*/
ISC_SWAP(handle, req->handle);
isc_mempool_put(worker->uvreq_pool, req);
ISC_LIST_UNLINK(sock->active_uvreqs, req, active_link);
if (handle != NULL) {
isc__nmhandle_detach(&handle FLARG_PASS);
}
isc___nmsocket_detach(&sock FLARG_PASS);
uv_close(&req->idle, uvreq_free);
}
void
@@ -1838,13 +1837,6 @@ isc__nmsocket_barrier_init(isc_nmsocket_t *listener) {
listener->barriers_initialised = true;
}
static void
isc__nm_uvreq_free(uv_handle_t *handle) {
isc__nm_uvreq_t *uvreq = uv_handle_get_data(handle);
isc__nm_uvreq_put(&uvreq, uvreq->handle->sock);
}
static void
isc___nm_connectcb(uv_idle_t *handle) {
isc__nm_uvreq_t *uvreq = uv_handle_get_data(handle);
@@ -1852,7 +1844,7 @@ isc___nm_connectcb(uv_idle_t *handle) {
uvreq->cb.connect(uvreq->handle, uvreq->result, uvreq->cbarg);
uv_idle_stop(handle);
uv_close(handle, isc__nm_uvreq_free);
isc__nm_uvreq_put(&uvreq);
}
void
@@ -1865,15 +1857,13 @@ isc__nm_connectcb(isc_nmsocket_t *sock, isc__nm_uvreq_t *uvreq,
if (!async) {
uvreq->cb.connect(uvreq->handle, eresult, uvreq->cbarg);
isc__nm_uvreq_put(&uvreq, uvreq->handle->sock);
isc__nm_uvreq_put(&uvreq);
return;
}
uvreq->result = eresult;
uv_idle_init(&sock->worker->loop->loop, &uvreq->uv_req.idle);
uv_idle_start(&uvreq->uv_req.idle, isc___nm_connectcb);
uv_handle_set_data(&uvreq->uv_req.idle, uvreq);
uv_idle_start(&uvreq->idle, isc___nm_connectcb);
}
static void
@@ -1887,7 +1877,7 @@ isc___nm_readcb(uv_idle_t *handle) {
uvreq->cb.recv(uvreq->handle, uvreq->result, &region, uvreq->cbarg);
uv_idle_stop(handle);
uv_close(handle, isc__nm_uvreq_free);
isc__nm_uvreq_put(&uvreq);
}
void
@@ -1904,15 +1894,13 @@ isc__nm_readcb(isc_nmsocket_t *sock, isc__nm_uvreq_t *uvreq,
region.length = uvreq->uvbuf.len;
uvreq->cb.recv(uvreq->handle, eresult, &region, uvreq->cbarg);
isc__nm_uvreq_put(&uvreq, uvreq->handle->sock);
isc__nm_uvreq_put(&uvreq);
return;
}
uvreq->result = eresult;
uv_idle_init(&sock->worker->loop->loop, &uvreq->uv_req.idle);
uv_idle_start(&uvreq->uv_req.idle, isc___nm_readcb);
uv_handle_set_data(&uvreq->uv_req.idle, uvreq);
uv_idle_start(&uvreq->idle, isc___nm_readcb);
}
static void
@@ -1922,7 +1910,7 @@ isc___nm_sendcb(uv_idle_t *handle) {
uvreq->cb.send(uvreq->handle, uvreq->result, uvreq->cbarg);
uv_idle_stop(handle);
uv_close(handle, isc__nm_uvreq_free);
isc__nm_uvreq_put(&uvreq);
}
void
@@ -1935,13 +1923,11 @@ isc__nm_sendcb(isc_nmsocket_t *sock, isc__nm_uvreq_t *uvreq,
if (!async) {
uvreq->cb.send(uvreq->handle, uvreq->result, uvreq->cbarg);
isc__nm_uvreq_put(&uvreq, uvreq->handle->sock);
isc__nm_uvreq_put(&uvreq);
return;
}
uv_idle_init(&sock->worker->loop->loop, &uvreq->uv_req.idle);
uv_idle_start(&uvreq->uv_req.idle, isc___nm_sendcb);
uv_handle_set_data(&uvreq->uv_req.idle, uvreq);
uv_idle_start(&uvreq->idle, isc___nm_sendcb);
}
static void