Limiter
Limiter provides quantitative restrictions on command execution. By saving restriction configurations and verifying them before each command runs, you can prevent excessive usage.
Overview
The Limiter functionality is implemented using the following modules:
cmdbox.app.commons.limiter: Core logic for restriction checks and counter updates.
cmdbox.app.features.cli.cmdbox_limiter_save: Add or save a restriction configuration.
cmdbox.app.features.cli.cmdbox_limiter_load: Load a restriction configuration.
cmdbox.app.features.cli.cmdbox_limiter_list: List registered restriction configurations.
cmdbox.app.features.cli.cmdbox_limiter_del: Delete a restriction configuration.
cmdbox.app.features.cli.cmdbox_limiter_counter: Retrieve restriction counters.
cmdbox.app.features.cli.cmdbox_limiter_targets: List Features that inherit from LimitedFeature.
cmdbox.app.features.cli.cmdbox_limiter_evidences: Retrieve evidence files for limiter configurations.
cmdbox.app.features.cli.cmdbox_limiter_plan_save: Add or save plan configurations that bundle multiple limiters.
cmdbox.app.features.cli.cmdbox_limiter_plan_load: Load a plan configuration.
cmdbox.app.features.cli.cmdbox_limiter_plan_list: List registered plan configurations.
cmdbox.app.features.cli.cmdbox_limiter_plan_del: Delete a plan configuration.
cmdbox.app.features.cli.cmdbox_limiter_billing_calc: Calculate billing data based on plan evidences.
cmdbox.app.features.cli.cmdbox_limiter_billing_load: Load billing data for a plan.
Restriction configurations are stored at data_dir/.limiter/limiter-{name}.json and counters are stored at data_dir/.limiter/counter-{name}.json.
Scope
Limiter supports two scopes:
server: Stores restriction configurations in the server-side data directory (recommended).
client: Stores restriction configurations in the client-side data directory specified by --client_data.
Saving a Restriction Configuration
Use the save command to register a restriction configuration.
Example (limit command executions to 100 times on the server side):
cmdbox -m limiter -c save \
--host localhost --port 6379 --password password --svname cmdbox \
--limiter_name my_limit \
--scope server \
--target_mode mymode \
--target_cmd mycommand \
--max_total_count 100
Example (set an execution period and periodic reset on the client side):
cmdbox -m limiter -c save \
--host localhost --port 6379 --password password --svname cmdbox \
--limiter_name my_limit2 \
--scope client \
--target_mode mymode \
--exec_period_start 2024-01-01T00:00:00 \
--exec_period_end 2024-12-31T23:59:59 \
--refresh_interval 86400 \
--max_history_interval 2678400
Main options:
Option |
Description |
|---|---|
|
Identifier name for the restriction configuration (required). |
|
Scope: client or server (required). |
|
Mode name of the target command to restrict. If omitted, all modes are targeted. |
|
Command name of the target command to restrict. If omitted, all commands are targeted. |
|
Conditions for the target command in dictionary format (can be specified multiple times). |
|
Maximum number of registrations (or maximum registration size). If omitted, no limit is applied. |
|
Maximum number of command executions. If omitted, no limit is applied. |
|
Total executable time in seconds for the command. If omitted, no limit is applied. |
|
Maximum total number of input bytes. If omitted, no limit is applied. |
|
Maximum total number of process bytes. If omitted, no limit is applied. |
|
Maximum total number of output bytes. If omitted, no limit is applied. |
|
Maximum number of credits for the command. If omitted, no limit is applied. |
|
Number of service credits. |
|
Start datetime of the executable period (e.g. 2024-01-01T00:00:00). If omitted, no limit is applied. |
|
End datetime of the executable period (e.g. 2024-12-31T23:59:59). If omitted, no limit is applied. |
|
Datetime at which the restriction counters are reset. If omitted, no reset is performed. |
|
Interval in seconds after which the restriction counters are reset. If omitted, no reset is performed. |
|
Maximum duration in seconds for which counter history is retained. Default is 31 days (2678400 seconds). |
Loading a Restriction Configuration
Use the load command to inspect a registered restriction configuration.
cmdbox -m limiter -c load \
--host localhost --port 6379 --password password --svname cmdbox \
--limiter_name my_limit \
--scope server
Listing Restriction Configurations
Use the list command to display all registered restriction configurations.
cmdbox -m limiter -c list \
--host localhost --port 6379 --password password --svname cmdbox \
--scope server
Example with keyword search (partial match):
cmdbox -m limiter -c list \
--host localhost --port 6379 --password password --svname cmdbox \
--scope server \
--kwd my_
Deleting a Restriction Configuration
Use the del command to delete a registered restriction configuration.
cmdbox -m limiter -c del \
--host localhost --port 6379 --password password --svname cmdbox \
--limiter_name my_limit \
--scope server
Retrieving Counters
Use the counter command to retrieve the counter for a restriction configuration.
cmdbox -m limiter -c counter \
--host localhost --port 6379 --password password --svname cmdbox \
--limiter_name my_limit \
--scope server
To also retrieve counter history:
cmdbox -m limiter -c counter \
--host localhost --port 6379 --password password --svname cmdbox \
--limiter_name my_limit \
--scope server \
--load_history
Available counter fields:
Field |
Description |
|---|---|
|
Identifier name of the restriction configuration. |
|
Number of executions. |
|
Total execution time in seconds. |
|
Total input bytes. |
|
Total process bytes. |
|
Total output bytes. |
|
Total credits consumed. |
|
Total number of registrations. |
|
Datetime of the last counter reset. |
|
Datetime of the last counter update. |
Listing Restriction Targets
Use the targets command to list all Features that inherit from LimitedFeature (i.e. commands that support restriction).
cmdbox -m limiter -c targets \
--host localhost --port 6379 --password password --svname cmdbox \
--scope server
To filter by mode or command:
cmdbox -m limiter -c targets \
--host localhost --port 6379 --password password --svname cmdbox \
--scope server \
--filter_target_mode mymode \
--filter_target_cmd mycommand
Retrieving Evidence Files
Use the evidences command to retrieve evidence files for a limiter configuration. Evidence files contain counter history and other information saved when the counter reset timing is reached.
cmdbox -m limiter -c evidences \
--host localhost --port 6379 --password password --svname cmdbox \
--limiter_name my_limit \
--scope server
To include counter history in the results:
cmdbox -m limiter -c evidences \
--host localhost --port 6379 --password password --svname cmdbox \
--limiter_name my_limit \
--scope server \
--include_history
Managing Plans
Plans bundle multiple restriction configurations together to support various billing models and service management scenarios.
Creating a Plan
Use the plan_save command to create or update a plan configuration that bundles multiple limiters.
cmdbox -m limiter -c plan_save \
--host localhost --port 6379 --password password --svname cmdbox \
--plan_name my_plan \
--plan_title "My Service Plan" \
--plan_desc "Description of my plan" \
--limiters limiter1 limiter2 limiter3 \
--plan_start 2024-01-01T00:00:00 \
--plan_end 2024-12-31T23:59:59 \
--billing_type period \
--billing_period_unit month \
--billing_period_qty 1 \
--billing_currency JPY \
--billing_unit_price 10000
Key plan configuration options:
Option |
Description |
|---|---|
|
Identifier name for the plan (required). |
|
Title of the plan. |
|
Description of the plan. |
|
Limiter configuration names to include in the plan (can be specified multiple times, required). |
|
Start datetime when the plan becomes active. |
|
End datetime when the plan expires. |
|
Datetime when user access begins. |
|
Datetime when user access is suspended. |
|
Datetime to notify about suspension before the actual suspension date. |
|
Billing model: period (fixed period billing) or metered (usage-based billing). |
|
Period unit for period-based billing: hour, day, month, or year. |
|
Number of period units for period-based billing. |
|
Limiter name to calculate metered billing from. |
|
Counter item for metered billing: registrations, count, time, input, process, output, or credits. Default is credits. |
|
Minimum billing amount for metered billing. |
|
Maximum billing amount for metered billing. |
|
Currency code for billing (default: JPY). |
|
Unit price for billing (required). |
Loading a Plan
Use the plan_load command to retrieve a plan configuration.
cmdbox -m limiter -c plan_load \
--host localhost --port 6379 --password password --svname cmdbox \
--plan_name my_plan
Listing Plans
Use the plan_list command to display all registered plans.
cmdbox -m limiter -c plan_list \
--host localhost --port 6379 --password password --svname cmdbox
To search by keyword (partial match):
cmdbox -m limiter -c plan_list \
--host localhost --port 6379 --password password --svname cmdbox \
--kwd my_
Deleting a Plan
Use the plan_del command to delete a plan configuration.
cmdbox -m limiter -c plan_del \
--host localhost --port 6379 --password password --svname cmdbox \
--plan_name my_plan
Billing Data Calculation
The Limiter module supports automatic billing calculation based on plan configurations and evidence files.
Calculating Billing Data
Use the billing_calc command to calculate billing data for plans based on their evidences.
cmdbox -m limiter -c billing_calc \
--host localhost --port 6379 --password password --svname cmdbox
To calculate billing for a specific plan:
cmdbox -m limiter -c billing_calc \
--host localhost --port 6379 --password password --svname cmdbox \
--plan_name my_plan
Billing calculation:
Retrieves all active plan configurations (or a specific plan if specified).
Gathers evidence files for each limiter in the plan.
Calculates billing amounts based on the plan's billing type (period or metered).
For period billing: Returns the fixed unit price for the period.
For metered billing: Calculates based on the specified counter item (e.g., credits consumed, execution count, etc.) multiplied by the unit price, with optional minimum and maximum limits.
Saves billing data files without overwriting existing files.
Output fields:
Field |
Description |
|---|---|
|
Path to the saved billing data file. |
|
Identifier name of the plan. |
|
Limiter name used for metered billing calculation. |
|
Datetime of the last counter reset. |
|
Calculated billing amount. |
|
Currency code for the billing. |
|
Set to true if a billing data file already exists for this billing period. |
Loading Billing Data
Use the billing_load command to retrieve billing data for a plan.
cmdbox -m limiter -c billing_load \
--host localhost --port 6379 --password password --svname cmdbox \
--plan_name my_plan
Developer Guide: Implementing a Limiter-Enabled Command
To apply the Limiter to a custom command, inherit from LimitedFeature and apply the appropriate decorator to apprun or svrun.
Applying to a client-side command
from cmdbox.app.commons.limiter import LimitedFeature, apprun_check_limit
class MyFeature(LimitedFeature):
def get_mode(self):
return 'mymode'
def get_cmd(self):
return 'mycommand'
@apprun_check_limit
def apprun(self, logger, args, tm, pf=[]):
# The restriction check runs automatically before execution.
# The counter is updated automatically after execution.
result = dict(success="done")
return self.RESP_SUCCESS, result, None
Applying to a server-side command
from cmdbox.app.commons.limiter import LimitedFeature, svrun_check_limit
class MyFeature(LimitedFeature):
@svrun_check_limit
def svrun(self, data_dir, logger, redis_cli, msg, sessions):
# The restriction check runs automatically before execution.
# The counter is updated automatically after execution.
...
return self.RESP_SUCCESS
Applying to async commands
Use async_apprun_check_limit / async_svrun_check_limit for async apprun / svrun.
from cmdbox.app.commons.limiter import LimitedFeature, async_apprun_check_limit
class MyAsyncFeature(LimitedFeature):
@async_apprun_check_limit
async def apprun(self, logger, args, tm, pf=[]):
result = dict(success="done")
return self.RESP_SUCCESS, result, None
Customizing counter calculation methods
LimitedFeature provides the following methods that can be overridden to customize how counter values are calculated:
Method |
Description |
|---|---|
|
Returns the input byte count for a client-side command. Default is the byte length of the argument string. |
|
Returns the output byte count for a client-side command. Default is the byte length of the result string. |
|
Returns the credits consumed by a client-side command. Default is 0. |
|
Returns the execution count for a client-side command. Default is 1. |
|
Returns the process byte count for a client-side command. Default is 0. |
|
Returns the registration count for a client-side command. Default is 0. |
|
Returns the input byte count for a server-side command. |
|
Returns the output byte count for a server-side command. |
|
Returns the credits consumed by a server-side command. Default is 0. |
|
Returns the execution count for a server-side command. Default is 1. |
|
Returns the process byte count for a server-side command. Default is 0. |
|
Returns the registration count for a server-side command. Default is 0. |
Command Reference
For the full list of options for each command, see Command Reference ( limiter mode ).