Skip to main content

cmd_handshake.go

cmd_handshake.go - Overview

This file defines the HANDSHAKE command, which is used to register a client's ID and execution mode with the DiceDB server.

Detailed Documentation

cHANDSHAKE

var cHANDSHAKE = &CommandMeta{
Name: "HANDSHAKE",
Syntax: "HANDSHAKE client_id execution_mode",
HelpShort: "HANDSHAKE tells the server the purpose of the connection",
HelpLong: `
HANDSHAKE is used to tell the DiceDB server the purpose of the connection. It
registers the client_id and execution_mode.

The client_id is a unique identifier for the client. It can be any string, typically
a UUID.

The execution_mode is the mode of the connection, it can be one of the following:

1. "command" - The client will send commands to the server and receive responses.
2. "watch" - The connection in the watch mode will be used to receive the responses of query subscriptions.

If you use DiceDB SDK or CLI then this HANDSHAKE command is automatically sent when the connection is established
or when you establish a subscription.
`,
Examples: `
localhost:7379> HANDSHAKE 4c9d0411-6b28-4ee5-b78a-e7e258afa52f command
OK OK
`,
Eval: evalHANDSHAKE,
Execute: executeHANDSHAKE,
}
  • Purpose: Defines the metadata for the HANDSHAKE command, including its name, syntax, help text, examples, and the functions to evaluate and execute the command.

init

func init() {
CommandRegistry.AddCommand(cHANDSHAKE)
}
  • Purpose: Registers the HANDSHAKE command with the CommandRegistry. This function is automatically called when the package is initialized.

evalHANDSHAKE

func evalHANDSHAKE(c *Cmd, s *dstore.Store) (*CmdRes, error) {
if len(c.C.Args) != 2 {
return cmdResNil, errors.ErrWrongArgumentCount("HANDSHAKE")
}
c.ClientID = c.C.Args[0]
c.Mode = c.C.Args[1]
return cmdResOK, nil
}
  • Purpose: Evaluates the HANDSHAKE command. It checks if the correct number of arguments is provided, and then sets the ClientID and Mode fields of the Cmd struct.
    • Parameters:
      • c: A pointer to the Cmd struct representing the command.
      • s: A pointer to a dstore.Store struct.
    • Returns:
      • *CmdRes: Returns cmdResOK if the evaluation is successful.
      • error: Returns an error if the number of arguments is incorrect.

executeHANDSHAKE

func executeHANDSHAKE(c *Cmd, sm *shardmanager.ShardManager) (*CmdRes, error) {
shard := sm.GetShardForKey("-")
return evalHANDSHAKE(c, shard.Thread.Store())
}
  • Purpose: Executes the HANDSHAKE command. It retrieves a shard from the ShardManager and then calls evalHANDSHAKE to perform the actual evaluation.
    • Parameters:
      • c: A pointer to the Cmd struct representing the command.
      • sm: A pointer to a shardmanager.ShardManager struct.
    • Returns:
      • *CmdRes: Returns the result of evalHANDSHAKE.
      • error: Returns the error from evalHANDSHAKE.

Code Examples

None

Getting Started Relevance