Skip to content
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
5 changes: 3 additions & 2 deletions plugins/experimental/rate_limit/limiter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ RateLimiter::queue_process_cont(TSCont cont, TSEvent event, void *edata)
// Try to enable some queued txns (if any) if there are slots available
while (limiter->size() > 0 && limiter->reserve()) {
auto [txnp, contp, start_time] = limiter->pop();
std::chrono::microseconds delay = std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time);
std::chrono::milliseconds delay = std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time);

limiter->delayHeader(txnp, delay);
TSDebug(PLUGIN_NAME, "Enabling queued txn after %ldms", static_cast<long>(delay.count()));
Expand Down Expand Up @@ -112,7 +112,7 @@ RateLimiter::setupQueueCont()
// for logging, and other types of metrics.
//
void
RateLimiter::delayHeader(TSHttpTxn txnp, std::chrono::microseconds delay) const
RateLimiter::delayHeader(TSHttpTxn txnp, std::chrono::milliseconds delay) const
{
if (header.size() > 0) {
TSMLoc hdr_loc = nullptr;
Expand All @@ -122,6 +122,7 @@ RateLimiter::delayHeader(TSHttpTxn txnp, std::chrono::microseconds delay) const
if (TS_SUCCESS == TSHttpTxnClientReqGet(txnp, &bufp, &hdr_loc)) {
if (TS_SUCCESS == TSMimeHdrFieldCreateNamed(bufp, hdr_loc, header.c_str(), header.size(), &field_loc)) {
if (TS_SUCCESS == TSMimeHdrFieldValueIntSet(bufp, hdr_loc, field_loc, -1, static_cast<int>(delay.count()))) {
TSDebug(PLUGIN_NAME, "Added client request header; %s: %d", header.c_str(), static_cast<int>(delay.count()));
TSMimeHdrFieldAppend(bufp, hdr_loc, field_loc);
}
TSHandleMLocRelease(bufp, hdr_loc, field_loc);
Expand Down
6 changes: 3 additions & 3 deletions plugins/experimental/rate_limit/limiter.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,19 @@ class RateLimiter
}
}

void delayHeader(TSHttpTxn txpn, std::chrono::microseconds delay) const;
void delayHeader(TSHttpTxn txpn, std::chrono::milliseconds delay) const;
void retryAfter(TSHttpTxn txpn, unsigned after) const;

// Continuation creation and scheduling
void setupQueueCont();

void
setupTxnCont(void *ih, TSHttpTxn txnp, TSHttpHookID hook)
setupTxnCont(TSHttpTxn txnp, TSHttpHookID hook)
{
TSCont cont = TSContCreate(rate_limit_cont, nullptr);
TSReleaseAssert(cont);

TSContDataSet(cont, ih);
TSContDataSet(cont, this);
TSHttpTxnHookAdd(txnp, hook, cont);
}

Expand Down
6 changes: 3 additions & 3 deletions plugins/experimental/rate_limit/rate_limit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,14 @@ TSRemapDoRemap(void *ih, TSHttpTxn txnp, TSRemapRequestInfo *rri)
if (!limiter->max_queue || limiter->full()) {
// We are running at limit, and the queue has reached max capacity, give back an error and be done.
TSHttpTxnStatusSet(txnp, static_cast<TSHttpStatus>(limiter->error));
limiter->setupTxnCont(ih, txnp, TS_HTTP_SEND_RESPONSE_HDR_HOOK);
limiter->setupTxnCont(txnp, TS_HTTP_SEND_RESPONSE_HDR_HOOK);
TSDebug(PLUGIN_NAME, "Rejecting request, we're at capacity and queue is full");
} else {
limiter->setupTxnCont(ih, txnp, TS_HTTP_POST_REMAP_HOOK);
limiter->setupTxnCont(txnp, TS_HTTP_POST_REMAP_HOOK);
TSDebug(PLUGIN_NAME, "Adding rate limiting hook, we are at capacity");
}
} else {
limiter->setupTxnCont(ih, txnp, TS_HTTP_TXN_CLOSE_HOOK);
limiter->setupTxnCont(txnp, TS_HTTP_TXN_CLOSE_HOOK);
TSDebug(PLUGIN_NAME, "Adding txn-close hook, we're not at capacity");
}
}
Expand Down