From ddb18d2657578bb39ffad4795cedee215f9b36b0 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Thu, 4 Apr 2024 20:45:16 +0200 Subject: [PATCH] uclient: add function for getting the amount of pending read/write data This can be used to throttle writes when doing large POST requests Signed-off-by: Felix Fietkau --- uclient-backend.h | 1 + uclient-http.c | 9 +++++++++ uclient.c | 8 ++++++++ uclient.h | 1 + 4 files changed, 19 insertions(+) diff --git a/uclient-backend.h b/uclient-backend.h index b013cde..1b808ac 100644 --- a/uclient-backend.h +++ b/uclient-backend.h @@ -34,6 +34,7 @@ struct uclient_backend { int (*read)(struct uclient *cl, char *buf, unsigned int len); int (*write)(struct uclient *cl, const char *buf, unsigned int len); + int (*pending_bytes)(struct uclient *cl, bool write); }; void uclient_backend_set_error(struct uclient *cl, int code); diff --git a/uclient-http.c b/uclient-http.c index 33f6513..d6e9dcf 100644 --- a/uclient-http.c +++ b/uclient-http.c @@ -1229,6 +1229,14 @@ int uclient_http_set_address_family(struct uclient *cl, int af) return 0; } +static int +uclient_http_pending_bytes(struct uclient *cl, bool write) +{ + struct uclient_http *uh = container_of(cl, struct uclient_http, uc); + + return ustream_pending_data(uh->us, write); +} + const struct uclient_backend uclient_backend_http = { .prefix = uclient_http_prefix, @@ -1242,4 +1250,5 @@ const struct uclient_backend uclient_backend_http = { .read = uclient_http_read, .write = uclient_http_send_data, .request = uclient_http_request_done, + .pending_bytes = uclient_http_pending_bytes, }; diff --git a/uclient.c b/uclient.c index a309de8..0fb92b8 100644 --- a/uclient.c +++ b/uclient.c @@ -406,6 +406,14 @@ int uclient_read(struct uclient *cl, char *buf, int len) return cl->backend->read(cl, buf, len); } +int uclient_pending_bytes(struct uclient *cl, bool write) +{ + if (!cl->backend->pending_bytes) + return -1; + + return cl->backend->pending_bytes(cl, write); +} + void uclient_disconnect(struct uclient *cl) { uloop_timeout_cancel(&cl->connection_timeout); diff --git a/uclient.h b/uclient.h index 29563b3..5ccda74 100644 --- a/uclient.h +++ b/uclient.h @@ -113,6 +113,7 @@ void uclient_disconnect(struct uclient *cl); int uclient_read(struct uclient *cl, char *buf, int len); int uclient_write(struct uclient *cl, const char *buf, int len); +int uclient_pending_bytes(struct uclient *cl, bool write); int uclient_request(struct uclient *cl); char *uclient_get_addr(char *dest, int *port, union uclient_addr *a); -- 2.30.2