You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@trafficserver.apache.org by GitBox <gi...@apache.org> on 2021/03/27 00:34:06 UTC

[GitHub] [trafficserver] shukitchan commented on a change in pull request #7623: New rate_limit plugin for simple resource limitations

shukitchan commented on a change in pull request #7623:
URL: https://github.com/apache/trafficserver/pull/7623#discussion_r602642295



##########
File path: plugins/experimental/rate_limit/limiter.h
##########
@@ -0,0 +1,162 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <deque>
+#include <tuple>
+#include <climits>
+#include <atomic>
+#include <cstring>
+#include <cstdio>
+#include <chrono>
+
+#include <ts/ts.h>
+
+constexpr char const PLUGIN_NAME[]  = "rate_limit";
+constexpr unsigned QUEUE_DELAY_TIME = 100; // Examine the queue every 100ms.
+
+using QueueTime = std::chrono::time_point<std::chrono::system_clock>;
+using QueueItem = std::tuple<TSHttpTxn, TSCont, QueueTime>;
+
+///////////////////////////////////////////////////////////////////////////////
+// Configuration object for a rate limiting remap rule.
+//
+class RateLimiter
+{
+public:
+  RateLimiter() : _queue_lock(TSMutexCreate()), _active_lock(TSMutexCreate()) {}
+
+  ~RateLimiter()
+  {
+    if (_action) {
+      TSActionCancel(_action);
+    }
+    if (_queue_cont) {
+      TSContDestroy(_queue_cont);
+    }
+    TSMutexDestroy(_queue_lock);
+    TSMutexDestroy(_active_lock);
+  }
+
+  // Reserve / release a slot from the active connect limits. Reserve will return
+  // false if we are unable to reserve a slot.
+  bool
+  reserve()
+  {
+    TSReleaseAssert(_active <= limit);
+    TSMutexLock(_active_lock);
+    if (_active == limit) {
+      TSMutexUnlock(_active_lock);
+      return false;
+    } else {
+      ++_active;
+      TSDebug(PLUGIN_NAME, "Active txns == %u", active());
+      TSMutexUnlock(_active_lock);
+      return true;
+    }
+  }
+
+  void
+  release()
+  {
+    TSMutexLock(_active_lock);
+    --_active;
+    TSMutexUnlock(_active_lock);
+  }
+
+  // Current size of the active_in connections
+  unsigned
+  active() const
+  {
+    return _active.load();
+  }
+
+  // Current size of the queue
+  unsigned
+  size() const
+  {
+    return _size.load();
+  }
+
+  // Is the queue full (at it's max size)?
+  bool
+  full() const
+  {
+    return (_size == max_queue);
+  }
+
+  void
+  push(TSHttpTxn txnp, TSCont cont)
+  {
+    QueueTime now = std::chrono::system_clock::now();

Review comment:
       Should we use TShrtime() instead? 




-- 
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org