Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2.0.0 rest refactor wip #67

Merged
merged 2 commits into from
May 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions include/discord-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "jsmn.h"
#include "jsmn-find.h"

#include "logconf.h" /* struct logconf */
#include "logconf.h"
#include "user-agent.h"
#include "websockets.h"
#include "work.h"
Expand Down Expand Up @@ -251,11 +251,13 @@ struct discord_context {
char key[DISCORD_ROUTE_LEN];
/** the connection handler assigned */
struct ua_conn *conn;
/** the request bucket's queue entry */
QUEUE entry;

/** current retry attempt (stop at rest->retry_limit) */
int retry_attempt;
/** the request bucket's queue entry */
QUEUE entry;
/** synchronize synchronous requests */
pthread_cond_t *cond;
};

/** @brief The handle used for handling asynchronous requests */
Expand Down Expand Up @@ -352,7 +354,8 @@ struct discord_context *discord_async_start_context(
struct ccord_szbuf *body,
enum http_method method,
char endpoint[DISCORD_ENDPT_LEN],
char key[DISCORD_ROUTE_LEN]);
char key[DISCORD_ROUTE_LEN],
struct discord_bucket *b);

/** @} DiscordInternalRESTAsync */

Expand Down Expand Up @@ -470,11 +473,8 @@ struct discord_bucket {
* @note @ref DISCORD_BUCKET_TIMEOUT if bucket is being ratelimited
*/
struct discord_context *performing_cxt;
/** wait and notify synchronous requests */
struct {
pthread_cond_t cond;
pthread_mutex_t lock;
} sync;
/** synchronize bucket */
pthread_mutex_t lock;
};

/**
Expand Down
24 changes: 13 additions & 11 deletions src/discord-rest.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,7 @@ _discord_rest_check_action(struct discord_rest *rest, struct CURLMsg *msg)

curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &cxt);

pthread_mutex_lock(&cxt->b->sync.lock);

pthread_mutex_lock(&cxt->b->lock);
resp = (struct discord_response){ .data = cxt->dispatch.data,
.keep = cxt->dispatch.keep,
.code = CCORD_OK };
Expand Down Expand Up @@ -312,9 +311,9 @@ _discord_rest_check_action(struct discord_rest *rest, struct CURLMsg *msg)
cxt->b->performing_cxt = NULL;
if (!retry || !discord_async_retry_context(&rest->async, cxt, wait_ms)) {
discord_async_recycle_context(&rest->async, cxt);
pthread_cond_signal(&cxt->b->sync.cond);
if (cxt->cond) pthread_cond_signal(cxt->cond);
}
pthread_mutex_unlock(&cxt->b->sync.lock);
pthread_mutex_unlock(&cxt->b->lock);

return resp.code;
}
Expand Down Expand Up @@ -426,17 +425,20 @@ _discord_rest_start_context(struct discord_rest *rest,
char endpoint[DISCORD_ENDPT_LEN],
char key[DISCORD_ROUTE_LEN])
{
struct discord_context *cxt = discord_async_start_context(
&rest->async, req, body, method, endpoint, key);
struct discord_bucket *b = discord_bucket_get(&rest->ratelimiter, key);
struct discord_context *cxt;

pthread_mutex_lock(&cxt->b->sync.lock);
pthread_mutex_lock(&b->lock);

discord_bucket_add_context(cxt->b, cxt, cxt->dispatch.high_p);
cxt = discord_async_start_context(
&rest->async, req, body, method, endpoint, key, b);

if (cxt->dispatch.sync)
pthread_cond_wait(&cxt->b->sync.cond, &cxt->b->sync.lock);
if (cxt->dispatch.sync) {
cxt->cond = &(pthread_cond_t)PTHREAD_COND_INITIALIZER;
pthread_cond_wait(cxt->cond, &b->lock);
}

pthread_mutex_unlock(&cxt->b->sync.lock);
pthread_mutex_unlock(&b->lock);

return CCORD_OK;
}
Expand Down
13 changes: 9 additions & 4 deletions src/discord-rest_async.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ discord_async_add_request(struct discord_async *async,
curl_easy_setopt(ehandle, CURLOPT_PRIVATE, cxt);

/* initiate libcurl transfer */
return curl_multi_add_handle(async->mhandle, ehandle)
return (curl_multi_add_handle(async->mhandle, ehandle) != CURLM_OK)
? CCORD_CURLM_INTERNAL
: CCORD_OK;
}
Expand Down Expand Up @@ -198,7 +198,8 @@ discord_async_start_context(struct discord_async *async,
struct ccord_szbuf *body,
enum http_method method,
char endpoint[DISCORD_ENDPT_LEN],
char key[DISCORD_ROUTE_LEN])
char key[DISCORD_ROUTE_LEN],
struct discord_bucket *b)
{
struct discord_rest *rest = CONTAINEROF(async, struct discord_rest, async);
struct discord *client = CLIENT(rest, rest);
Expand Down Expand Up @@ -227,8 +228,8 @@ discord_async_start_context(struct discord_async *async,
memcpy(cxt->endpoint, endpoint, sizeof(cxt->endpoint));
/* copy bucket's key */
memcpy(cxt->key, key, sizeof(cxt->key));
/* bucket pertaining to the request */
cxt->b = discord_bucket_get(&rest->ratelimiter, key);

cxt->cond = NULL;

if (req->dispatch.keep) {
CCORDcode code = discord_refcounter_incr(&client->refcounter,
Expand All @@ -246,6 +247,10 @@ discord_async_start_context(struct discord_async *async,
req->dispatch.cleanup, false);
}

/* bucket pertaining to the request */
discord_bucket_add_context(b, cxt, cxt->dispatch.high_p);

io_poller_wakeup(async->io_poller);

return cxt;
}
9 changes: 4 additions & 5 deletions src/discord-rest_ratelimit.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ struct _discord_route {
static void
_discord_bucket_cleanup(struct discord_bucket *b)
{
pthread_cond_destroy(&b->sync.cond);
pthread_mutex_destroy(&b->sync.lock);
pthread_mutex_destroy(&b->lock);
free(b);
}

Expand Down Expand Up @@ -131,9 +130,7 @@ _discord_bucket_init(struct discord_ratelimiter *rl,
b->remaining = 1;
b->limit = limit;

ASSERT_S(!pthread_cond_init(&b->sync.cond, NULL),
"Couldn't initialize bucket's cond");
ASSERT_S(!pthread_mutex_init(&b->sync.lock, NULL),
ASSERT_S(!pthread_mutex_init(&b->lock, NULL),
"Couldn't initialize bucket's mutex");

QUEUE_INIT(&b->pending_queue);
Expand Down Expand Up @@ -423,6 +420,8 @@ discord_bucket_add_context(struct discord_bucket *b,
QUEUE_INSERT_HEAD(&b->pending_queue, &cxt->entry);
else
QUEUE_INSERT_TAIL(&b->pending_queue, &cxt->entry);

cxt->b = b;
}

struct discord_context *
Expand Down