masaori335 commented on code in PR #11271:
URL: https://github.com/apache/trafficserver/pull/11271#discussion_r1573981094
##########
plugins/experimental/rate_limit/limiter.h:
##########
@@ -24,16 +24,129 @@
#include <string>
#include <climits>
#include <mutex>
+#include <thread>
#include "tscore/ink_config.h"
#include "ts/ts.h"
#include <yaml-cpp/yaml.h>
#include "utilities.h"
-constexpr auto QUEUE_DELAY_TIME = std::chrono::milliseconds{300}; // Examine
the queue every 300ms
-using QueueTime =
std::chrono::time_point<std::chrono::system_clock>;
+constexpr auto BUCKET_REFILL_INTERVAL = std::chrono::milliseconds{25}; //
Increase rate limit buckets every 25ms
+constexpr auto QUEUE_DELAY_TIME = std::chrono::milliseconds{300}; //
Examine the queue every 300ms
+using QueueTime =
std::chrono::time_point<std::chrono::system_clock>;
-enum { RATE_LIMITER_TYPE_SNI = 0, RATE_LIMITER_TYPE_REMAP,
RATE_LIMITER_TYPE_MAX };
+int bucket_refill_cont(TSCont cont, TSEvent event, void *edata);
+class BucketManager
+{
+ using self_type = BucketManager;
+
+public:
+ class RateBucket
+ {
+ using self_type = RateBucket;
+
+ public:
+ RateBucket(uint32_t max) : _count(0), _max(max) {}
+ ~RateBucket() = default;
+
+ RateBucket(self_type &&) = delete;
+ self_type &operator=(const self_type &) = delete;
+ self_type &operator=(self_type &&) = delete;
+
+ uint32_t
+ count() const
+ {
+ return _count.load(std::memory_order_acquire);
+ }
+
+ bool
+ consume()
+ {
+ uint32_t val = _count.load(std::memory_order_acquire);
+
+ while (val > 0) {
+ if (_count.compare_exchange_weak(val, val - 1,
std::memory_order_release, std::memory_order_acquire)) {
Review Comment:
It looks like the `val` can be changed by other thread. In such case, this
`compare_exchange_weak` is failed and this loop keeps running. Is it bette to
load the `_count` again in this loop?
Another approach is simple `fetch_sub(1, std::memory_order_relaxed)`? It
doesn't need loop, I think.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]