Python: creating and reusing asgi_add_reader() wrapper.

This commit is contained in:
Max Romanov
2021-10-28 17:46:52 +03:00
parent 86138113eb
commit 803e037302

View File

@@ -36,6 +36,7 @@ static PyObject *nxt_py_asgi_create_subprotocols(nxt_unit_field_t *f);
static int nxt_python_asgi_ready(nxt_unit_ctx_t *ctx); static int nxt_python_asgi_ready(nxt_unit_ctx_t *ctx);
static int nxt_py_asgi_add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port); static int nxt_py_asgi_add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port);
static int nxt_py_asgi_add_reader(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port);
static void nxt_py_asgi_remove_port(nxt_unit_t *lib, nxt_unit_port_t *port); static void nxt_py_asgi_remove_port(nxt_unit_t *lib, nxt_unit_port_t *port);
static void nxt_py_asgi_quit(nxt_unit_ctx_t *ctx); static void nxt_py_asgi_quit(nxt_unit_ctx_t *ctx);
static void nxt_py_asgi_shm_ack_handler(nxt_unit_ctx_t *ctx); static void nxt_py_asgi_shm_ack_handler(nxt_unit_ctx_t *ctx);
@@ -365,7 +366,6 @@ nxt_python_asgi_run(nxt_unit_ctx_t *ctx)
nxt_py_asgi_remove_reader(ctx, ctx_data->port); nxt_py_asgi_remove_reader(ctx, ctx_data->port);
if (ctx_data->port != NULL) { if (ctx_data->port != NULL) {
ctx_data->port->data = NULL;
ctx_data->port = NULL; ctx_data->port = NULL;
} }
@@ -894,10 +894,7 @@ fail:
static int static int
nxt_python_asgi_ready(nxt_unit_ctx_t *ctx) nxt_python_asgi_ready(nxt_unit_ctx_t *ctx)
{ {
int rc;
PyObject *res, *fd, *py_ctx, *py_port;
nxt_unit_port_t *port; nxt_unit_port_t *port;
nxt_py_asgi_ctx_data_t *ctx_data;
if (nxt_slow_path(nxt_py_shared_port == NULL)) { if (nxt_slow_path(nxt_py_shared_port == NULL)) {
return NXT_UNIT_ERROR; return NXT_UNIT_ERROR;
@@ -907,66 +904,14 @@ nxt_python_asgi_ready(nxt_unit_ctx_t *ctx)
nxt_unit_debug(ctx, "asgi_ready %d %p %p", port->in_fd, ctx, port); nxt_unit_debug(ctx, "asgi_ready %d %p %p", port->in_fd, ctx, port);
ctx_data = ctx->data; return nxt_py_asgi_add_reader(ctx, port);
rc = NXT_UNIT_ERROR;
fd = PyLong_FromLong(port->in_fd);
if (nxt_slow_path(fd == NULL)) {
nxt_unit_alert(ctx, "Python failed to create fd");
nxt_python_print_exception();
return rc;
}
py_ctx = PyLong_FromVoidPtr(ctx);
if (nxt_slow_path(py_ctx == NULL)) {
nxt_unit_alert(ctx, "Python failed to create py_ctx");
nxt_python_print_exception();
goto clean_fd;
}
py_port = PyLong_FromVoidPtr(port);
if (nxt_slow_path(py_port == NULL)) {
nxt_unit_alert(ctx, "Python failed to create py_port");
nxt_python_print_exception();
goto clean_py_ctx;
}
res = PyObject_CallFunctionObjArgs(ctx_data->loop_add_reader,
fd, nxt_py_port_read,
py_ctx, py_port, NULL);
if (nxt_slow_path(res == NULL)) {
nxt_unit_alert(ctx, "Python failed to add_reader");
nxt_python_print_exception();
} else {
Py_DECREF(res);
rc = NXT_UNIT_OK;
}
Py_DECREF(py_port);
clean_py_ctx:
Py_DECREF(py_ctx);
clean_fd:
Py_DECREF(fd);
return rc;
} }
static int static int
nxt_py_asgi_add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port) nxt_py_asgi_add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port)
{ {
int nb, rc; int nb;
PyObject *res, *fd, *py_ctx, *py_port;
nxt_py_asgi_ctx_data_t *ctx_data; nxt_py_asgi_ctx_data_t *ctx_data;
if (port->in_fd == -1) { if (port->in_fd == -1) {
@@ -993,18 +938,32 @@ nxt_py_asgi_add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port)
ctx_data = ctx->data; ctx_data = ctx->data;
ctx_data->port = port; ctx_data->port = port;
port->data = ctx_data;
rc = NXT_UNIT_ERROR; return nxt_py_asgi_add_reader(ctx, port);
}
static int
nxt_py_asgi_add_reader(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port)
{
int rc;
PyObject *res, *fd, *py_ctx, *py_port;
nxt_py_asgi_ctx_data_t *ctx_data;
nxt_unit_debug(ctx, "asgi_add_reader %d %p %p", port->in_fd, ctx, port);
ctx_data = ctx->data;
fd = PyLong_FromLong(port->in_fd); fd = PyLong_FromLong(port->in_fd);
if (nxt_slow_path(fd == NULL)) { if (nxt_slow_path(fd == NULL)) {
nxt_unit_alert(ctx, "Python failed to create fd"); nxt_unit_alert(ctx, "Python failed to create fd");
nxt_python_print_exception(); nxt_python_print_exception();
return rc; return NXT_UNIT_ERROR;
} }
rc = NXT_UNIT_ERROR;
py_ctx = PyLong_FromVoidPtr(ctx); py_ctx = PyLong_FromVoidPtr(ctx);
if (nxt_slow_path(py_ctx == NULL)) { if (nxt_slow_path(py_ctx == NULL)) {
nxt_unit_alert(ctx, "Python failed to create py_ctx"); nxt_unit_alert(ctx, "Python failed to create py_ctx");