rqlite

package
v0.26.1 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 21 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package rqlite is a database/sql driver for rqlite, which runs SQLite on each node of a cluster, and takes SQL over HTTP. It registers the one name "rqlite", and takes a DSN of the form rqlite://user:pass@host:4001?key=value (D141).

db, err := sql.Open("rqlite", "rqlite://admin:pass@localhost:4001")

Each call sends one statement in one request (D142). A query goes to /db/request, so that a write with RETURNING returns its rows, and Exec goes to /db/execute. The server binds each argument (D143). A value has the Go type of the affinity of its declared type, and a value that SQLite stored with another storage class keeps the Go type of its JSON form (D140). rqlite shares one write connection among every client, so the driver has no transactions (D144). When the context has a deadline, the server stops the statement at that deadline (D145). docs/RQLITE.md holds what the driver knows about the server.

Index

Constants

View Source
const (
	// LevelNone reads the SQLite of the node, with no check of the leader.
	LevelNone = "none"
	// LevelWeak reads on the leader, after the node checks that it leads.
	LevelWeak = "weak"
	// LevelLinearizable reads on the leader, after a round of Raft that
	// shows that it still leads.
	LevelLinearizable = "linearizable"
	// LevelStrong sends the read through the log of Raft.
	LevelStrong = "strong"
	// LevelAuto is LevelNone on a read-only node, and LevelWeak on any
	// other.
	LevelAuto = "auto"
)

The values of the key level, the read consistency of a query (D141). An empty level sends none, and the server reads at LevelWeak.

View Source
const Name = "rqlite"

Name is the name that the driver registers with database/sql, and the scheme of its DSN.

Variables

This section is empty.

Functions

func WithOptions

func WithOptions(ctx context.Context, opts ...Option) context.Context

WithOptions returns a context that carries opts. Each statement started with the context applies them after the options of the DSN.

Types

type Config

type Config struct {
	// Host is the host of the node.
	Host string
	// Port is the port of the HTTP API of the node, 4001 by default.
	Port int
	// TLS is true to speak HTTPS.
	TLS bool
	// User and Password are the credentials, which go by HTTP Basic
	// authentication. With no user, the driver sends no credentials.
	User     string
	Password string
	// Level is the read consistency of a query, or "" for the default of
	// the server (D141).
	Level string
	// Freshness is how old the data of a read with LevelNone can be, or zero
	// for no bound.
	Freshness time.Duration
}

Config is the configuration of a connector, which the DSN holds. The caller owns it (D7).

func ParseDSN

func ParseDSN(dsn string) (*Config, error)

ParseDSN parses a DSN of the form rqlite://user:pass@host:port?key=value (D27, D35 and D141). A DSN with a path is refused, because rqlite has one database.

func (*Config) FormatDSN

func (cfg *Config) FormatDSN() string

FormatDSN returns the DSN of cfg. ParseDSN reads it back as cfg, for a Config that ParseDSN or NewConnector filled.

type Connector

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

Connector opens connections to one node. It owns its transport, and every connection shares it. A caller can build one from a Config and open it with sql.OpenDB.

func NewConnector

func NewConnector(cfg Config) *Connector

NewConnector returns a Connector for cfg. The connector keeps a copy of cfg, and fills each field that is zero with its default.

func (*Connector) Close

func (c *Connector) Close() error

Close closes the idle connections of the transport.

func (*Connector) Connect

func (c *Connector) Connect(context.Context) (driver.Conn, error)

Connect satisfies driver.Connector. A connection holds nothing on the server, so it sends no request.

func (*Connector) Driver

func (c *Connector) Driver() driver.Driver

Driver satisfies driver.Connector.

type Driver

type Driver struct{}

Driver is the database/sql driver for rqlite.

func (Driver) Open

func (Driver) Open(string) (driver.Conn, error)

Open satisfies driver.Driver. database/sql opens a connection through OpenConnector, and Open returns an error, because opening a connection needs a context.

func (Driver) OpenConnector

func (Driver) OpenConnector(dsn string) (driver.Connector, error)

OpenConnector satisfies driver.DriverContext. It parses the DSN.

type Error

type Error struct {
	// HTTPStatus is the status code of the response.
	HTTPStatus int
	// Message is the message of the server.
	Message string
	// contains filtered or unexported fields
}

Error is an error that rqlite reported. An error of SQL arrives with HTTP 200, as the key error of the result of the statement, such as `no such table: t` (measured). A request that the server refused arrives with another status: HTTP 400 for a body that is not JSON, HTTP 401 for a wrong password, and HTTP 500 for a result that JSON cannot hold, such as an infinity (measured).

func (*Error) AuthenticationRefused added in v0.24.0

func (err *Error) AuthenticationRefused() bool

AuthenticationRefused reports whether the server refused the credential. It returns what Is reports for dbimp.ErrAuthentication, and a caller that does not import this module reads it through an interface of its own (D227).

func (*Error) ErrVerbose added in v0.25.0

func (err *Error) ErrVerbose(_ string) string

ErrVerbose returns the error as psql writes it for \errverbose: the code and the message on the first line, and then each field that the server sent, one on each line with its name in upper case. A caller that does not import this module reads it through an interface of its own (D231).

func (*Error) Error

func (err *Error) Error() string

Error satisfies the error interface.

func (*Error) Is added in v0.18.0

func (err *Error) Is(target error) bool

Is reports whether err matches target. It matches dbimp.ErrAuthentication for HTTP 401, which is the answer for a wrong password (recorded: "a wrong password"). rqlite also sends HTTP 401, with the same empty body, for an endpoint that a user has no permission for (recorded: "the status"), but the driver sends only /db/execute, /db/query and /db/request, so a 401 that the driver sees is a refused credential (D197).

func (*Error) Unwrap

func (err *Error) Unwrap() error

Unwrap returns the *dbimp.StatusError of a response whose status is not 2xx, or nil.

type Option

type Option = dbimp.Option[options]

Option sets an option of one statement (D109). An option comes from the DSN, then from the context through WithOptions, then from an argument of the statement, and a later one wins.

func WithDatabase

func WithDatabase(name string) Option

WithDatabase would choose a database. rqlite has one database (D141), so a statement with it fails with dbimp.ErrNotSupported.

func WithFreshness

func WithFreshness(d time.Duration) Option

WithFreshness sets how old the data of a read with LevelNone can be, as the key freshness of the DSN does (D141).

func WithLevel

func WithLevel(level string) Option

WithLevel sets the read consistency of a query, as the key level of the DSN does (D141).

func WithParameter

func WithParameter(name string, value any) Option

WithParameter sets any key of the URL of the request by its name, such as "timings" or "qualify_columns", with the text of value. The server reads a flag by its presence, so nil and "" send the key alone. A key named here replaces one that the driver sets itself, such as "db_timeout" or "level".

func WithReadonly

func WithReadonly(readonly bool) Option

WithReadonly sends the statement to /db/query, which runs it on a read-only connection, so the server refuses a write with the error `attempt to change database via query operation` (measured and D142). A user with only the permission query can run a statement with it.

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout sets the time that the server gives the statement, as the key db_timeout. The server stops a read that runs longer with the error `query timeout`, and a write with `execute timeout`, and keeps nothing of the write (measured). When the context has a deadline too, the shorter of the two goes (D145). The driver also ends the request at that time, because 9.4.5 ignores db_timeout for a read on /db/request, and a read stops when its client leaves (D146). Zero sets no timeout.

Jump to

Keyboard shortcuts

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