SLEEP
The SLEEP
command in DiceDB is used to pause the execution of the current client connection for a specified number of seconds. This command is primarily useful for testing purposes, such as simulating network latency or delaying operations to observe the behavior of other commands in a controlled environment.
Parameters
Syntax
Parameters
seconds
: (Required) A floating-point number representing the number of seconds to sleep. This value can be an integer or a decimal, allowing for sub-second precision.
Return Value
The SLEEP
command returns a simple string reply of “OK” after the specified sleep duration has elapsed.
Behaviour
When the SLEEP
command is executed, the following behavior is observed:
- The client connection that issued the
SLEEP
command will be paused for the specified duration. - During this sleep period, the client will not be able to send or receive any other commands.
- Other clients connected to the DiceDB server will not be affected and can continue to execute commands normally.
- After the sleep duration has elapsed, the client will receive an “OK” response and can resume normal operations.
Error Handling
The SLEEP
command can raise errors under the following conditions:
-
Invalid Number of Arguments
:Error Message
:ERR wrong number of arguments for 'sleep' command
Condition
: This error occurs if theSLEEP
command is called without the requiredseconds
parameter or with more than one parameter.
-
Invalid Parameter Type
:Error Message
:ERR value is not a valid float
Condition
: This error occurs if theseconds
parameter is not a valid floating-point number.
Example Usage
Example 1: Basic Usage
Pause the client for 5 seconds.
Example 2: Sub-second Precision
Pause the client for 1.5 seconds.
Example 3: Error Handling - Missing Parameter
Attempt to call SLEEP
without specifying the seconds
parameter.
Example 4: Error Handling - Invalid Parameter Type
Attempt to call SLEEP
with a non-numeric parameter.
Notes
- The
SLEEP
command is a blocking operation for the client that issues it. It does not affect the server’s ability to handle other clients. - This command is useful for testing and debugging purposes but should be used with caution in production environments to avoid unintended delays.