NopeCharon commented on code in PR #838:
URL: https://github.com/apache/logging-log4cxx/pull/838#discussion_r4650315574


##########
src/main/cpp/burstfilter.cpp:
##########
@@ -0,0 +1,212 @@
+/*
+ * 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 <log4cxx/logstring.h>
+#include <log4cxx/filter/burstfilter.h>
+#include <log4cxx/spi/loggingevent.h>
+#include <log4cxx/helpers/stringhelper.h>
+#include <log4cxx/helpers/optionconverter.h>
+#include <log4cxx/helpers/loglog.h>
+#include <log4cxx/private/filter_priv.h>
+#include <algorithm>
+#include <chrono>
+#include <limits>
+#include <mutex>
+
+using namespace LOG4CXX_NS;
+using namespace LOG4CXX_NS::filter;
+using namespace LOG4CXX_NS::spi;
+using namespace LOG4CXX_NS::helpers;
+
+#define priv static_cast<BurstFilterPrivate*>(m_priv.get())
+
+struct BurstFilter::BurstFilterPrivate : public FilterPrivate
+{
+       using Clock = std::chrono::steady_clock;
+
+       BurstFilterPrivate()
+               : level(Level::getWarn())
+               , rate(10)
+               , maxBurst(0)
+       {
+               fill();
+       }
+
+       /**
+       The most severe level that is subject to the rate limit.
+       */
+       LevelPtr level;
+       /**
+       The number of tokens added to the bucket each second.
+       */
+       int rate;
+       /**
+       The bucket capacity, or zero to use ten times the rate.
+       */
+       int maxBurst;
+
+       std::mutex mutex;
+       double tokens;
+       Clock::time_point lastRefill;
+
+       int getCapacity() const
+       {
+               if (0 < this->maxBurst)
+                       return this->maxBurst;
+               const int maxRate = (std::numeric_limits<int>::max)() / 10;
+               return (std::min)(this->rate, maxRate) * 10;
+       }
+
+       void fill()
+       {
+               this->tokens = getCapacity();
+               this->lastRefill = Clock::now();
+       }
+
+       /**
+       Remove a token from the bucket if one is available.
+       */
+       bool tryAcquire()
+       {
+               std::lock_guard<std::mutex> lock(this->mutex);
+               auto now = Clock::now();
+               std::chrono::duration<double> elapsed = now - this->lastRefill;
+               if (0 < elapsed.count())

Review Comment:
   Added a comment on the decrement and one at the declaration of tokens 
describing its use and range, in 
https://github.com/NopeCharon/logging-log4cxx/commit/d22df2075d79e53d88955b12cc97a323b1030993



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

Reply via email to