Documentation
¶
Overview ¶
Package skylite is the Go database/sql driver for skylite, speaking the client protocol of ADR 0005 (docs/content/docs/decisions/0005-client-protocol.md) over HTTPS, with bearer tokens as ADR 0006 requires.
A query outside a transaction goes to the server's stateless query endpoint; one the server refuses there as not a pure read, every Exec, and every statement of a transaction go on the connection's write channel, which the driver opens on first use. A request on a channel whose response is lost is sent again with the same sequence number and the same body, which the server answers from its record: that is the protocol's replay, and the driver retries nothing else. An outcome the driver cannot learn is an error matching ErrOutcomeUnknown, never a success, and the connection is then discarded.
Open a database with a Connector, which carries the token source:
c, err := skylite.NewConnector(skylite.Config{...})
if err != nil { ... }
db := sql.OpenDB(c)
db.Close closes the Connector, which releases the idle connections of the driver's own HTTP client.
The driver is also registered as "skylite", but a DSN cannot carry a token source, so sql.Open("skylite", dsn) fails on first use.
Index ¶
Examples ¶
Constants ¶
const ( CodeInvalidRequest = wire.CodeInvalidRequest CodeUnauthenticated = wire.CodeUnauthenticated CodeForbidden = wire.CodeForbidden CodeStatementForbidden = wire.CodeStatementForbidden CodeDatabaseNotFound = wire.CodeDatabaseNotFound CodeDatabaseExists = wire.CodeDatabaseExists CodeNeedsChannel = wire.CodeNeedsChannel CodeChannelBusy = wire.CodeChannelBusy CodeInFlight = wire.CodeInFlight CodeStaleSequence = wire.CodeStaleSequence CodeSequenceGap = wire.CodeSequenceGap CodeSequenceConflict = wire.CodeSequenceConflict CodeChannelLost = wire.CodeChannelLost CodeRequestTooLarge = wire.CodeRequestTooLarge CodeTooManyChannels = wire.CodeTooManyChannels CodeOverloaded = wire.CodeOverloaded CodeInvalidState = wire.CodeInvalidState CodeTransactionAborted = wire.CodeTransactionAborted CodeBusy = wire.CodeBusy CodeTimeout = wire.CodeTimeout CodeCancelled = wire.CodeCancelled CodeResultTooLarge = wire.CodeResultTooLarge CodeSQLError = wire.CodeSQLError CodeCommitFailed = wire.CodeCommitFailed CodeCommitOutcomeUnknown = wire.CodeCommitOutcomeUnknown CodeNoLeader = wire.CodeNoLeader CodeNotServing = wire.CodeNotServing CodeOwnerUnreachable = wire.CodeOwnerUnreachable CodeBehind = wire.CodeBehind CodeMaintenance = wire.CodeMaintenance CodeInternal = wire.CodeInternal )
The error codes of ADR 0005.
Variables ¶
var ErrNoDSN = errors.New("skylite: a DSN cannot carry a token source; use NewConnector and sql.OpenDB")
ErrNoDSN is Driver.Open's error.
var ErrOutcomeUnknown = errors.New("skylite: the outcome of the request is unknown")
ErrOutcomeUnknown is matched, with errors.Is, by the *Error of a request on a write channel whose outcome the driver could not learn: it may or may not have taken effect, a commit included. Do not run it again without checking. The error also matches the context's error when the caller's context ended, and ErrTransactionLost when the request was a statement inside a transaction. It never matches driver.ErrBadConn.
var ErrReadOnlyUnsupported = fmt.Errorf("skylite: read-only transactions are not supported by this server version: %w", errors.ErrUnsupported)
ErrReadOnlyUnsupported is BeginTx's error for a read-only transaction: the server serves no read-only channels yet. It wraps errors.ErrUnsupported.
ErrResultUnavailable is matched by the *Error of a request whose response was lost and whose rows the server could not keep for its replay. Committed reports whether it was an autocommit write, which committed; inside a transaction, the statement's effects are in the transaction, which stays open.
var ErrTransactionLost = errors.New("skylite: the transaction was lost with its channel and will not commit")
ErrTransactionLost is matched by the *Error of every statement and the commit of a transaction whose channel was lost or abandoned: nothing is sent for them, the transaction never commits, and its Rollback succeeds.
Functions ¶
This section is empty.
Types ¶
type Code ¶
Code is an ADR 0005 error code. The list is closed in protocol version 1; an Error may still carry a code outside it from a newer server.
type Config ¶
type Config struct {
// Endpoint is the server's base URL: https://host[:port][/path]. It may
// not carry user information, a query, a fragment, an empty host, or a
// "." or ".." path segment.
Endpoint string
// Database is the database name: 1 to 63 characters from a-z, 0-9 and
// -, starting with a letter or digit.
Database string
// Token supplies the bearer token of each request. Required.
Token TokenSource
// TLSConfig is the TLS configuration of the driver's own HTTP client;
// nil means the system roots. It cannot be combined with HTTPClient.
TLSConfig *tls.Config
// AllowPlaintext permits an http:// Endpoint. Without it only https://
// is accepted.
AllowPlaintext bool
// StatementTimeout, when set, lowers the server's statement time limit
// for each query: from 1 ms to 2^31-1 ms. Zero leaves the server's
// limit. The caller's context bounds the request on the client side.
StatementTimeout time.Duration
// ResponseHeaderTimeout bounds the wait for a response's headers on the
// driver's own HTTP client; zero means 60 s, above the server's default
// statement time limit of 30 s. It cannot be combined with HTTPClient.
ResponseHeaderTimeout time.Duration
// HTTPClient replaces the driver's HTTP client. The driver uses a copy
// whose redirect policy refuses every redirect, and never closes its
// connections.
HTTPClient *http.Client
}
Config configures a Connector.
type Connector ¶
type Connector struct {
// contains filtered or unexported fields
}
Connector opens connections to one database. It holds the read-your-writes watermark of ADR 0005 decision 5, shared by every connection of the sql.DB that uses it.
func NewConnector ¶
NewConnector validates cfg and returns a Connector. It sends nothing. Its errors never hold the endpoint or anything else from cfg.
Example ¶
The example compiles with the package's tests, so the documented usage stays valid; it has no output, so it does not run.
package main
import (
"context"
"database/sql"
"fmt"
"log"
"os"
"strings"
skylite "github.com/RandomCodeSpace/skylite/driver"
)
func main() {
c, err := skylite.NewConnector(skylite.Config{
Endpoint: "https://skylite.example.internal",
Database: "library",
Token: skylite.TokenFunc(func(context.Context) (string, error) {
// A projected service account token, read again on every request.
b, err := os.ReadFile("/var/run/secrets/tokens/skylite")
return strings.TrimSpace(string(b)), err
}),
})
if err != nil {
log.Fatal(err)
}
db := sql.OpenDB(c)
defer func() { _ = db.Close() }() // also closes c
var title string
if err := db.QueryRowContext(context.Background(), "SELECT title FROM books WHERE id > ?", 10).Scan(&title); err != nil {
log.Fatal(err)
}
fmt.Println(title)
}
Output:
func (*Connector) Close ¶
Close waits for the cancels and closes of abandoned channels still running, each bounded by abandonWait, so that the server releases what those channels hold at once, and closes the idle connections of the driver's own HTTP client, never those of a Config.HTTPClient. sql.DB.Close calls it. An HTTP/2 connection a query still uses then closes after its idle timeout, 90 seconds by default. An abandon that starts while Close waits sends nothing. Close must not be called from the token source: an abandon waiting on it holds a slot, so Close would never return.
type Driver ¶
type Driver struct{}
Driver is the driver registered as "skylite". Its Open always fails, because a DSN cannot carry a token source; use NewConnector.
type Error ¶
type Error struct {
Code Code
NotExecuted bool // this delivery had no effect
Final bool // this answer is the request's settled outcome
Message string
RequestID string
ChannelID string // the write channel's identifier, not its token
Seq int64 // the request's sequence number on the channel
SQLiteCode int64
SQLiteExtendedCode int64
Txn int64
Epoch int64
Cause string // "maintenance" on a transaction_aborted that maintenance caused
// contains filtered or unexported fields
}
Error is an error the server answered, ADR 0005's error object, or the driver's error for an outcome it could not learn (ErrOutcomeUnknown), a lost transaction (ErrTransactionLost) or a lost result (ErrResultUnavailable), which errors.Is matches. Use errors.As to read it. Zero fields were absent; Code is empty when no server answer gave one.
func (*Error) Committed ¶
Committed reports, for an error matching ErrResultUnavailable, whether the request committed.