Compare commits

..

2 Commits

Author SHA1 Message Date
x3
9a00b28279
Define API_VERSION 2022-01-09 13:09:40 +01:00
x3
246058891b
Ratelimit 2nd part 2022-01-09 13:07:08 +01:00
3 changed files with 20 additions and 7 deletions

View File

@ -23,13 +23,14 @@ make
- NAT handling - NAT handling
- Multi thread hashing - Multi thread hashing
- Read/write timeout in net - Read/write timeout in net
- Api ratelimit (the other part)
- Decode escaping from server - Decode escaping from server
- Use a config file - Use a config file
- Add newline escape to server - Add newline escape to server
- Better field parsing, remove the horrors at code 310 - Better field parsing, remove the horrors at code 310
- Add myliststats cmd as --stats arg - Add myliststats cmd as --stats arg
- Add support for compression - 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 - Make deleting from mylist possible, with
- Name regexes, - Name regexes,
- If file is not found at a scan - If file is not found at a scan

View File

@ -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 struct timespec api_last_packet = {0}; /* Last packet time */
static int32_t api_packet_count = 0; /* Only increment */ 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, static int api_escaped_string(FILE *io, const struct printf_info *info,
const void *const *args) const void *const *args)
@ -314,7 +314,6 @@ enum error api_clock_init()
struct timespec ts; struct timespec ts;
memset(&api_last_packet, 0, sizeof(api_last_packet)); memset(&api_last_packet, 0, sizeof(api_last_packet));
api_packet_count = 0; api_packet_count = 0;
api_fast_packet_count = 0;
if (clock_getres(API_CLOCK, &ts) != 0) { if (clock_getres(API_CLOCK, &ts) != 0) {
uio_error("Cannot get clock resolution: %s", strerror(errno)); uio_error("Cannot get clock resolution: %s", strerror(errno));
@ -436,6 +435,7 @@ void api_free()
*/ */
static void api_ratelimit_sent() static void api_ratelimit_sent()
{ {
api_packet_count++;
clock_gettime(API_CLOCK, &api_last_packet); clock_gettime(API_CLOCK, &api_last_packet);
} }
@ -443,16 +443,25 @@ static void api_ratelimit()
{ {
struct timespec ts = {0}; struct timespec ts = {0};
uint64_t msdiff, mswait; 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); clock_gettime(API_CLOCK, &ts);
msdiff = util_timespec_diff(&api_last_packet, &ts); msdiff = util_timespec_diff(&api_last_packet, &ts);
uio_debug("Time since last packet: %ld ms", msdiff); uio_debug("Time since last packet: %ld ms", msdiff);
if (msdiff >= API_SENDWAIT) if (msdiff >= msrate)
return; /* No ratelimiting is needed */ return; /* No ratelimiting is needed */
/* Need ratelimit, so do it here for now */ /* 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); uio_debug("Ratelimit is needed, sleeping for %ld ms", mswait);
MS_TO_TIMESPEC_L(ts, mswait); MS_TO_TIMESPEC_L(ts, mswait);
@ -593,8 +602,9 @@ static enum error api_cmd_auth(const char *uname, const char *pass,
char buffer[API_BUFSIZE]; char buffer[API_BUFSIZE];
long code; long code;
size_t res_len = api_send(buffer, snprintf(buffer, sizeof(buffer), size_t res_len = api_send(buffer, snprintf(buffer, sizeof(buffer),
"AUTH user=%s&pass=%B&protover=3&client=caniadd&clientver=" "AUTH user=%s&pass=%B&protover=" API_VERSION "&client=caniadd&"
PROG_VERSION "&enc=UTF-8", uname, pass), sizeof(buffer)); "clientver=" PROG_VERSION "&enc=UTF-8", uname, pass),
sizeof(buffer));
enum error err = NOERR; enum error err = NOERR;
if (res_len == -1) { if (res_len == -1) {

View File

@ -5,6 +5,8 @@
#include "error.h" #include "error.h"
#define API_VERSION "3"
/* Maximum length of one response/request */ /* Maximum length of one response/request */
#define API_BUFSIZE 1400 #define API_BUFSIZE 1400
/* Session key maximum size, including '\0' */ /* Session key maximum size, including '\0' */