mirror of
https://github.com/XRPLF/rippled.git
synced 2025-12-06 17:27:55 +00:00
Fix DecayingSample treatment of the window
This commit is contained in:
committed by
Nik Bougalis
parent
baf0d09455
commit
64d0f7fffd
@@ -20,9 +20,13 @@
|
|||||||
#ifndef RIPPLE_BASICS_DECAYINGSAMPLE_H_INCLUDED
|
#ifndef RIPPLE_BASICS_DECAYINGSAMPLE_H_INCLUDED
|
||||||
#define RIPPLE_BASICS_DECAYINGSAMPLE_H_INCLUDED
|
#define RIPPLE_BASICS_DECAYINGSAMPLE_H_INCLUDED
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
namespace ripple {
|
namespace ripple {
|
||||||
|
|
||||||
/** Sampling function using exponential decay to provide a continuous value. */
|
/** Sampling function using exponential decay to provide a continuous value.
|
||||||
|
@tparam The number of seconds in the decay window.
|
||||||
|
*/
|
||||||
template <int Window, typename Clock>
|
template <int Window, typename Clock>
|
||||||
class DecayingSample
|
class DecayingSample
|
||||||
{
|
{
|
||||||
@@ -69,26 +73,20 @@ private:
|
|||||||
|
|
||||||
if (m_value != value_type())
|
if (m_value != value_type())
|
||||||
{
|
{
|
||||||
typename Clock::duration n (now - m_when);
|
std::size_t elapsed = std::chrono::duration_cast<
|
||||||
|
std::chrono::seconds>(now - m_when).count();
|
||||||
|
|
||||||
// A span larger than four times the window decays the
|
// A span larger than four times the window decays the
|
||||||
// value to an insignificant amount so just reset it.
|
// value to an insignificant amount so just reset it.
|
||||||
//
|
//
|
||||||
typename Clock::duration window (Window);
|
if (elapsed > 4 * Window)
|
||||||
if (n > 4 * window)
|
|
||||||
{
|
{
|
||||||
m_value = value_type();
|
m_value = value_type();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
value_type const tick_value = 1;
|
while (elapsed--)
|
||||||
typename Clock::duration const tick (tick_value);
|
m_value -= (m_value + Window - 1) / Window;
|
||||||
typename Clock::duration const zero (0);
|
|
||||||
while (n > zero)
|
|
||||||
{
|
|
||||||
n -= tick;
|
|
||||||
m_value -= (m_value + Window - tick_value) / Window;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user