MXS-2329 Allow the restriction of duration units
It's now possible to specify in the config parameter declaration that the smallest allowed unit is seconds. For parameters whose granularity is seconds, allowing to specify a duration in milliseconds would open up a possibility for hard to detect errors.
This commit is contained in:
		@ -109,6 +109,7 @@ enum mxs_module_param_options
 | 
			
		||||
    MXS_MODULE_OPT_PATH_F_OK   = (1 << 4),  /**< PATH: Path must exist */
 | 
			
		||||
    MXS_MODULE_OPT_PATH_CREAT  = (1 << 5),  /**< PATH: Create path if it doesn't exist */
 | 
			
		||||
    MXS_MODULE_OPT_ENUM_UNIQUE = (1 << 6),  /**< ENUM: Only one value can be defined */
 | 
			
		||||
    MXS_MODULE_OPT_DURATION_S  = (1 << 7),  /**< DURATION: Cannot be specified in milliseconds */
 | 
			
		||||
 | 
			
		||||
    /**< Parameter is deprecated: Causes a warning to be logged if the parameter
 | 
			
		||||
     * is used but will not cause a configuration error. */
 | 
			
		||||
 | 
			
		||||
@ -4484,13 +4484,27 @@ bool config_param_is_valid(const MXS_MODULE_PARAM* params,
 | 
			
		||||
                    {
 | 
			
		||||
                        valid = true;
 | 
			
		||||
 | 
			
		||||
                        if (unit == mxs::config::DURATION_IN_DEFAULT)
 | 
			
		||||
                        switch (unit)
 | 
			
		||||
                        {
 | 
			
		||||
                        case mxs::config::DURATION_IN_MILLISECONDS:
 | 
			
		||||
                            if (params[i].options & MXS_MODULE_OPT_DURATION_S)
 | 
			
		||||
                            {
 | 
			
		||||
                                MXS_ERROR("Currently the granularity of '%s' is seconds. The value "
 | 
			
		||||
                                          "cannot be specified in milliseconds.", params[i].name);
 | 
			
		||||
                                valid = false;
 | 
			
		||||
                            }
 | 
			
		||||
                            break;
 | 
			
		||||
 | 
			
		||||
                        case mxs::config::DURATION_IN_DEFAULT:
 | 
			
		||||
                            MXS_WARNING("Specifying durations without a suffix denoting the unit "
 | 
			
		||||
                                        "has been deprecated: '%s=%s'. Use the suffixes 'h' (hour), "
 | 
			
		||||
                                        "'m' (minute) 's' (second) or 'ms' (milliseconds). "
 | 
			
		||||
                                        "For instance, '%s=%ss' or '%s=%sms.",
 | 
			
		||||
                                        key, value, key, value, key, value);
 | 
			
		||||
                            break;
 | 
			
		||||
 | 
			
		||||
                        default:
 | 
			
		||||
                            break;
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
@ -44,6 +44,7 @@ int test_validity()
 | 
			
		||||
        {"p7", MXS_MODULE_PARAM_SERVICE,  "my-service"            },
 | 
			
		||||
        {"p8", MXS_MODULE_PARAM_ENUM,     "a", MXS_MODULE_OPT_ENUM_UNIQUE, enum_values},
 | 
			
		||||
        {"p9", MXS_MODULE_PARAM_DURATION, "4711s"                 },
 | 
			
		||||
        {"p10", MXS_MODULE_PARAM_DURATION, "4711s", MXS_MODULE_OPT_DURATION_S },
 | 
			
		||||
        {MXS_END_MODULE_PARAMS}
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
@ -107,6 +108,7 @@ int test_validity()
 | 
			
		||||
    TEST(config_param_is_valid(params, "p9", "4711S", &ctx));
 | 
			
		||||
    TEST(config_param_is_valid(params, "p9", "4711MS", &ctx));
 | 
			
		||||
    TEST(!config_param_is_valid(params, "p9", "4711q", &ctx));
 | 
			
		||||
    TEST(!config_param_is_valid(params, "p10", "4711ms", &ctx));
 | 
			
		||||
 | 
			
		||||
    /** Service parameter */
 | 
			
		||||
    CONFIG_CONTEXT svc("test-service");
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user