MXS-2329 Make duration misuse harder
Now the desired type must be specified when getting a duration. The type also dictates how durations without suffixes should be interpreted. That removes the need for remembering that to convert a returned millisecond duration to a second duration.
This commit is contained in:
@ -339,8 +339,26 @@ public:
|
|||||||
*
|
*
|
||||||
* @return Duration in milliseconds; 0 if the parameter is not found.
|
* @return Duration in milliseconds; 0 if the parameter is not found.
|
||||||
*/
|
*/
|
||||||
std::chrono::milliseconds get_duration(const std::string& key,
|
std::chrono::milliseconds get_duration_in_ms(const std::string& key,
|
||||||
mxs::config::DurationInterpretation interpretation) const;
|
mxs::config::DurationInterpretation interpretation) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Get a duration in a specific unit.
|
||||||
|
*
|
||||||
|
* @param key Parameter name
|
||||||
|
*
|
||||||
|
* @return The duration in the desired unit.
|
||||||
|
*
|
||||||
|
* @note The type the function is specialized with dictates how values without a
|
||||||
|
* suffix should be interpreted; if @c std::chrono::seconds, they will be
|
||||||
|
* interpreted as seconds, if @c std::chrono::milliseconds, they will be
|
||||||
|
* interpreted as milliseconds.
|
||||||
|
*
|
||||||
|
* @note There is no default implementation, but only specializations for
|
||||||
|
* @c std::chrono::seconds and @c std::chrono::milliseconds.
|
||||||
|
*/
|
||||||
|
template<class T>
|
||||||
|
T get_duration(const std::string& key) const = delete;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get a service value
|
* @brief Get a service value
|
||||||
@ -441,6 +459,22 @@ private:
|
|||||||
ContainerType m_contents;
|
ContainerType m_contents;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
inline std::chrono::milliseconds
|
||||||
|
MXS_CONFIG_PARAMETER::get_duration<std::chrono::milliseconds>(const std::string& key) const
|
||||||
|
{
|
||||||
|
return get_duration_in_ms(key, mxs::config::INTERPRET_AS_MILLISECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
inline std::chrono::seconds
|
||||||
|
MXS_CONFIG_PARAMETER::get_duration<std::chrono::seconds>(const std::string& key) const
|
||||||
|
{
|
||||||
|
std::chrono::milliseconds ms = get_duration_in_ms(key, mxs::config::INTERPRET_AS_SECONDS);
|
||||||
|
return std::chrono::duration_cast<std::chrono::seconds>(ms);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The config context structure, used to build the configuration
|
* The config context structure, used to build the configuration
|
||||||
* data during the parse process
|
* data during the parse process
|
||||||
|
|||||||
@ -1947,8 +1947,9 @@ uint64_t MXS_CONFIG_PARAMETER::get_size(const std::string& key) const
|
|||||||
return intval;
|
return intval;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::chrono::milliseconds MXS_CONFIG_PARAMETER::get_duration(const std::string& key,
|
std::chrono::milliseconds
|
||||||
mxs::config::DurationInterpretation interpretation)
|
MXS_CONFIG_PARAMETER::get_duration_in_ms(const std::string& key,
|
||||||
|
mxs::config::DurationInterpretation interpretation)
|
||||||
const
|
const
|
||||||
{
|
{
|
||||||
string value = get_string(key);
|
string value = get_string(key);
|
||||||
|
|||||||
@ -90,9 +90,7 @@ public:
|
|||||||
if (new_instance)
|
if (new_instance)
|
||||||
{
|
{
|
||||||
new_instance->m_count = params->get_integer("count");
|
new_instance->m_count = params->get_integer("count");
|
||||||
new_instance->m_time = params->get_duration("time", mxs::config::INTERPRET_AS_SECONDS).count();
|
new_instance->m_time = params->get_duration<std::chrono::seconds>("time").count();
|
||||||
// The window is in seconds.
|
|
||||||
new_instance->m_time = std::lround(new_instance->m_time / 1000.0);
|
|
||||||
new_instance->m_match = params->get_string(PARAM_MATCH);
|
new_instance->m_match = params->get_string(PARAM_MATCH);
|
||||||
new_instance->m_nomatch = params->get_string(PARAM_IGNORE);
|
new_instance->m_nomatch = params->get_string(PARAM_IGNORE);
|
||||||
|
|
||||||
|
|||||||
@ -65,11 +65,11 @@ ThrottleFilter* ThrottleFilter::create(const char* zName, MXS_CONFIG_PARAMETER*
|
|||||||
{
|
{
|
||||||
int max_qps = pParams->get_integer(MAX_QPS_CFG);
|
int max_qps = pParams->get_integer(MAX_QPS_CFG);
|
||||||
int sample_msecs =
|
int sample_msecs =
|
||||||
pParams->get_duration(SAMPLING_DURATION_CFG, mxs::config::INTERPRET_AS_MILLISECONDS).count();
|
pParams->get_duration<std::chrono::milliseconds>(SAMPLING_DURATION_CFG).count();
|
||||||
int throttle_msecs =
|
int throttle_msecs =
|
||||||
pParams->get_duration(THROTTLE_DURATION_CFG, mxs::config::INTERPRET_AS_MILLISECONDS).count();
|
pParams->get_duration<std::chrono::milliseconds>(THROTTLE_DURATION_CFG).count();
|
||||||
int cont_msecs =
|
int cont_msecs =
|
||||||
pParams->get_duration(CONTINUOUS_DURATION_CFG, mxs::config::INTERPRET_AS_MILLISECONDS).count();
|
pParams->get_duration<std::chrono::milliseconds>(CONTINUOUS_DURATION_CFG).count();
|
||||||
bool config_ok = true;
|
bool config_ok = true;
|
||||||
|
|
||||||
if (max_qps < 2)
|
if (max_qps < 2)
|
||||||
|
|||||||
Reference in New Issue
Block a user