From: Fred Morcos Date: Tue, 12 Dec 2023 13:48:37 +0000 (+0100) Subject: Move Ewma impl to ws-auth.cc X-Git-Tag: auth-4.9.0-alpha1~23^2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2c03bdc8f37d5ad3f819f027e6e1172c2e0ef39e;p=thirdparty%2Fpdns.git Move Ewma impl to ws-auth.cc --- diff --git a/pdns/ws-auth.cc b/pdns/ws-auth.cc index 4d2482cc0d..ec429a30e1 100644 --- a/pdns/ws-auth.cc +++ b/pdns/ws-auth.cc @@ -52,6 +52,42 @@ using json11::Json; extern StatBag S; +Ewma::Ewma() { dt.set(); } + +void Ewma::submit(int val) +{ + int rate = val - d_last; + double difft = dt.udiff() / 1000000.0; + dt.set(); + + d_10 = ((600.0 - difft) * d_10 + (difft * rate)) / 600.0; + d_5 = ((300.0 - difft) * d_5 + (difft * rate)) / 300.0; + d_1 = ((60.0 - difft) * d_1 + (difft * rate)) / 60.0; + d_max = max(d_1, d_max); + + d_last = val; +} + +double Ewma::get10() const +{ + return d_10; +} + +double Ewma::get5() const +{ + return d_5; +} + +double Ewma::get1() const +{ + return d_1; +} + +double Ewma::getMax() const +{ + return d_max; +} + // NOLINTNEXTLINE(readability-identifier-length) static void patchZone(UeberBackend& B, const DNSName& zonename, DomainInfo& di, HttpRequest* req, HttpResponse* resp); diff --git a/pdns/ws-auth.hh b/pdns/ws-auth.hh index 42beee75f8..c306a1fa2a 100644 --- a/pdns/ws-auth.hh +++ b/pdns/ws-auth.hh @@ -31,40 +31,18 @@ class Ewma { public: - Ewma() : d_last(0), d_10(0), d_5(0), d_1(0), d_max(0){dt.set();} - void submit(int val) - { - int rate=val-d_last; - double difft=dt.udiff()/1000000.0; - dt.set(); - - d_10=((600.0-difft)*d_10+(difft*rate))/600.0; - d_5=((300.0-difft)*d_5+(difft*rate))/300.0; - d_1=((60.0-difft)*d_1+(difft*rate))/60.0; - d_max=max(d_1,d_max); - - d_last=val; - } - double get10() - { - return d_10; - } - double get5() - { - return d_5; - } - double get1() - { - return d_1; - } - double getMax() - { - return d_max; - } + Ewma(); + + void submit(int val); + double get10() const; + double get5() const; + double get1() const; + double getMax() const; + private: DTime dt; - int d_last; - double d_10, d_5, d_1, d_max; + int d_last{}; + double d_10{}, d_5{}, d_1{}, d_max{}; }; class AuthWebServer