Skip to content

Commit

Permalink
Move Ewma impl to ws-auth.cc
Browse files Browse the repository at this point in the history
  • Loading branch information
fredmorcos committed Dec 21, 2023
1 parent 212f57b commit 2c03bdc
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 32 deletions.
36 changes: 36 additions & 0 deletions pdns/ws-auth.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
42 changes: 10 additions & 32 deletions pdns/ws-auth.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2c03bdc

Please sign in to comment.