redis

package
v0.9.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2026 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package redis implements the "redis" protocol.Protocol driver, for load testing a Redis server or a store that speaks the same wire protocol (Valkey, and often KeyDB, Dragonfly and Garnet). It uses a pure Go client.

Each call runs one command, or several commands as a pipeline (one per line of -body). With transaction=true the commands run inside MULTI/EXEC. The client is shared by every user, as an application's client is. Waiting for a free connection counts as part of the measured time, so give -opt pool at least as many connections as users when you want to measure the server and not the queue.

Redis has no session read-only switch. This driver checks every command before it sends anything. A refused pipeline sends nothing. A read-only server user is still the real lock; allow_writes is a safety net.

A timed-out call is dropped by the client library, which closes that connection. Refused commands are never sent, so they do not change a connection and need no cleanup. This driver does not hold a connection while it does other work: it uses Do, Pipeline or TxPipeline, and it never takes a connection of its own.

Options (set with -opt key=value):

username      An ACL user. It can also be given in the URL. The default
              user needs no name. A password does not need a user.
password_env  The name of an environment variable that holds the
              password. The password is never put on the command line,
              and it is refused in the URL.
database      A number from 0 to 255 (default 0). It can also be the
              path of the URL, as redis://host:6379/2.
tls           false (default for redis://), true, or skip-verify.
              rediss:// means true. true checks the certificate and the
              host name. With -insecure, true does not check the
              certificate. tls=false with rediss:// is an error.
ca_file       PEM CAs. They replace the system roots. See tlsconf.
cert_file     PEM client certificate. It needs key_file.
key_file      PEM private key for cert_file. Encrypted keys are refused.
tls_server_name  Name to send as SNI and to check on the certificate.
pool          The most connections the run opens (default 10).
protocol      2 (default) or 3. 2 is RESP2. 3 is RESP3.
allow_writes  true to let a command change data. The default is
              read-only. A command that is not on the read list fails
              before it is sent, and the error says how to allow it.
allow_admin   true to allow admin commands such as FLUSHALL. It needs
              allow_writes as well. allow_admin without allow_writes is
              an error.
transaction   true to run the commands inside MULTI/EXEC, even when
              there is only one. The default sends one command on its
              own, and two or more as a pipeline, which is not atomic.
args          A JSON array of values for the ? placeholders, such as
              '["user:42", "ann"]'. A ? is one whole argument.
min_rows      The last reply must have at least this many elements.
expect        Some kept element of the last reply must contain this text.
max_rows      How many elements of an array reply a scenario script gets
              back (default 1000). Every element is read and counted
              either way.

The target is redis://host[:port][/database] or rediss:// for TLS, with port 6379 by default. The URL takes no query string. A password in the URL is refused.

Index

Constants

This section is empty.

Variables

View Source
var Options = append([]string{
	"username", "password_env", "database", "tls", "pool", "protocol",
	"allow_writes", "allow_admin", "transaction", "args", "min_rows", "expect", "max_rows",
}, tlsconf.Keys...)

Options are the -opt keys this driver accepts. The command's protocol table uses the same list, so the two cannot differ.

Functions

func JoinTokens added in v0.9.0

func JoinTokens(lines [][]string) string

JoinTokens writes lines of tokens as one redis-cli body. A token that parseCommands would split, or would read as an args placeholder, is quoted so the text comes back as one argument.

func SplitTokens added in v0.9.0

func SplitTokens(body string) ([][]string, error)

SplitTokens splits a redis-cli body into one token list per command line. Blank lines and comments are left out. A quoted token is returned as the text it stands for, so a later join can quote it again. An unquoted ? is the text "?", not an args placeholder, until parseCommands sees it.

Types

type Driver

type Driver struct {
	// contains filtered or unexported fields
}

Driver is a Redis protocol.Protocol.

func New

func New(target protocol.Target, timeout time.Duration) (*Driver, error)

New builds a driver for one target. It does not connect: the first call does.

func NewConn

func NewConn(target protocol.Target, timeout time.Duration, password *string) (*Driver, error)

NewConn is New for a scenario script. It makes only the client: the server, the login and the connection options. It needs no command. The job of each call is then set with Call. password is the password, which the script already holds, so password_env is not used with it.

func (*Driver) Call

func (d *Driver) Call(opts map[string]string, body []byte, timeout time.Duration) (*Driver, error)

Call returns a Driver for one call of a script: the same client as d, with the job that opts and body describe. Closing it does nothing. Closing d closes the client.

func (*Driver) Close

func (d *Driver) Close() error

Close closes the client. A driver returned by Call shares the client, so Close on it does nothing.

func (*Driver) Do

func (d *Driver) Do(parent context.Context) (protocol.Result, error)

Do runs one call. A command the server rejects, a timeout, and a refused command are a failed iteration (Result.Err set), not a reason to stop the run. Do returns a nil error so the engine keeps going.

func (*Driver) Name

func (d *Driver) Name() string

Name returns the protocol name.

func (*Driver) Run

func (d *Driver) Run(parent context.Context) (protocol.Result, Reply)

Run runs one call and returns the reply a scenario script reads.

type Reply

type Reply struct {
	Values   []any
	Value    any
	RowCount int
}

Reply is what one call read back. Values has one entry per command, in order. A command that returned an error has a nil entry. Value is the last entry. RowCount is the number of top-level elements of the last reply (the whole reply, including elements past max_rows), 1 for any other non-nil reply, and 0 for nil.

Directories

Path Synopsis
Package redistest is a small Redis server for tests.
Package redistest is a small Redis server for tests.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL