From 246058891bd0d9f476c4086596a7c614ab1b81a5 Mon Sep 17 00:00:00 2001 From: x3 Date: Sun, 9 Jan 2022 13:07:08 +0100 Subject: [PATCH] Ratelimit 2nd part --- README.md | 3 ++- src/api.c | 17 +++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 6a7d5d5..9f65d66 100644 --- a/README.md +++ b/README.md @@ -23,13 +23,14 @@ make - NAT handling - Multi thread hashing - Read/write timeout in net -- Api ratelimit (the other part) - Decode escaping from server - Use a config file - Add newline escape to server - Better field parsing, remove the horrors at code 310 - Add myliststats cmd as --stats arg - Add support for compression +- Implement session saving between different invocations, and session destroying +- Automatically remove found anime from wishlist - Make deleting from mylist possible, with - Name regexes, - If file is not found at a scan diff --git a/src/api.c b/src/api.c index cb5ab31..6c37fa3 100644 --- a/src/api.c +++ b/src/api.c @@ -55,7 +55,7 @@ static bool api_ka_now = false; /* Are we doing keepalive now? */ static struct timespec api_last_packet = {0}; /* Last packet time */ static int32_t api_packet_count = 0; /* Only increment */ -static int32_t api_fast_packet_count = 0; /* Incremented or decrement */ +//static int32_t api_fast_packet_count = 0; /* Increment or decrement */ static int api_escaped_string(FILE *io, const struct printf_info *info, const void *const *args) @@ -314,7 +314,6 @@ enum error api_clock_init() struct timespec ts; memset(&api_last_packet, 0, sizeof(api_last_packet)); api_packet_count = 0; - api_fast_packet_count = 0; if (clock_getres(API_CLOCK, &ts) != 0) { uio_error("Cannot get clock resolution: %s", strerror(errno)); @@ -436,6 +435,7 @@ void api_free() */ static void api_ratelimit_sent() { + api_packet_count++; clock_gettime(API_CLOCK, &api_last_packet); } @@ -443,16 +443,25 @@ static void api_ratelimit() { struct timespec ts = {0}; uint64_t msdiff, mswait; + uint64_t msrate = api_packet_count >= API_LONGTERM_PACKETS ? + API_SENDWAIT_LONG : API_SENDWAIT; + + /* First of all, the first N packets are unmetered */ + if (api_packet_count <= API_FREESEND) { + uio_debug("This packet is for free! Yay :D (%d/%d)", + api_packet_count, API_FREESEND); + return; + } clock_gettime(API_CLOCK, &ts); msdiff = util_timespec_diff(&api_last_packet, &ts); uio_debug("Time since last packet: %ld ms", msdiff); - if (msdiff >= API_SENDWAIT) + if (msdiff >= msrate) return; /* No ratelimiting is needed */ /* Need ratelimit, so do it here for now */ - mswait = API_SENDWAIT - msdiff; + mswait = msrate - msdiff; uio_debug("Ratelimit is needed, sleeping for %ld ms", mswait); MS_TO_TIMESPEC_L(ts, mswait);