hrana

package
v0.0.0-...-7d81877 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package hrana is a bounded client for the official libSQL Hrana 3 pipeline protocol. It is private to the dalgo2libsql adapter.

One call sends a stateless pipeline — an execute request and a close request, with no baton — and decodes every result before returning any value, so a server that answers a later request with an error can never look like a successful read. Nothing here exposes a baton, a raw endpoint URL, credentials, or a request body to a caller's error chain.

Index

Constants

View Source
const (
	DefaultMaxRequestBytes   = 1 << 20  // 1 MiB
	DefaultMaxResponseBytes  = 4 << 20  // 4 MiB
	DefaultMaxOperationBytes = 16 << 20 // 16 MiB
	DefaultMaxOperationReqs  = 32
	DefaultMaxRows           = 10_000
	DefaultMaxOperationRows  = 50_000
	DefaultMaxColumns        = 256
	DefaultMaxValueBytes     = 1 << 20 // 1 MiB
	DefaultMaxArgs           = 256
	DefaultMaxStatementBytes = 1 << 20

	AbsoluteMaxRequestBytes   = 64 << 20
	AbsoluteMaxResponseBytes  = 64 << 20
	AbsoluteMaxOperationBytes = 64 << 20
	AbsoluteMaxOperationReqs  = 256
	AbsoluteMaxRows           = 100_000
	AbsoluteMaxOperationRows  = 500_000
	AbsoluteMaxColumns        = 2_048
	AbsoluteMaxValueBytes     = 16 << 20
	AbsoluteMaxArgs           = 4_096
	AbsoluteMaxStatementBytes = 16 << 20
)

Default and absolute limits. The defaults are deliberately modest: an existing-table read adapter should not buffer an unbounded result.

View Source
const Version = "hrana3"

Version identifies the protocol this client speaks.

Variables

View Source
var ErrLimit = errors.New("hrana: limit exceeded")

ErrLimit is wrapped by every bound failure.

View Source
var ErrProtocol = errors.New("hrana: protocol violation")

ErrProtocol is wrapped by every malformed or unexpected response.

Functions

func WithBudget

func WithBudget(ctx context.Context, b *Budget) context.Context

WithBudget returns a context carrying b. A nil b is a no-op.

Types

type Budget

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

Budget is the cumulative accounting of one DALgo operation. The facade attaches one to the context of every read, so the metadata probes the SQL compiler makes and the data statement that follows share one bound.

func BudgetFrom

func BudgetFrom(ctx context.Context) *Budget

BudgetFrom returns the budget carried by ctx, or nil.

func NewBudget

func NewBudget(l Limits) *Budget

NewBudget returns a budget for one operation. Zero limits select the package defaults; NewDB normalizes them once, and this keeps a directly constructed budget from meaning "zero bytes allowed".

func (*Budget) AddRequest

func (b *Budget) AddRequest() error

AddRequest records one pipeline call.

func (*Budget) AddResponse

func (b *Budget) AddResponse(bytes int64, rows int) error

AddResponse records response bytes and rows of one decoded result.

type Client

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

Client is one bounded Hrana 3 pipeline endpoint.

func New

func New(cfg Config) (*Client, error)

New returns a client for one endpoint. It performs no I/O.

func (*Client) Close

func (c *Client) Close() error

Close releases idle connections of a transport this client owns. It never closes a transport the caller supplied, and it is idempotent.

func (*Client) Execute

func (c *Client) Execute(ctx context.Context, statement string, args []any) (*Result, error)

Execute sends one statement in a stateless pipeline and returns its complete result. The close request is validated like the execute request; a failure in either is an error, never a partial success.

func (*Client) Origin

func (c *Client) Origin() string

Origin returns the configured scheme://host[:port] without credentials.

type Column

type Column struct {
	Name     string
	DeclType string
}

Column is one result column. DeclType is SQLite's declared type; it is empty for an expression that has none.

type Config

type Config struct {
	// Endpoint is the normalized base URL: https://host[/path]. The client
	// appends /v3/pipeline.
	Endpoint *url.URL
	// HTTPClient is the client to use. New copies the value and, when its
	// transport is an *http.Transport, clones that too; the caller's client
	// and transport are never mutated. A nil client uses a new default.
	HTTPClient *http.Client
	// Headers supplies per-request headers, credentials included.
	Headers HeaderProvider
	// Limits bounds the client and its operations.
	Limits Limits
}

Config is the input of New.

type Error

type Error struct {
	Op     string
	Status int
	Code   string
	Detail string
}

Error is a protocol, transport or limit failure. Its text is safe to return to a caller: it never contains the endpoint, credentials, SQL text, bound values, a response body, or an underlying transport error.

func (*Error) Error

func (e *Error) Error() string

type HeaderProvider

type HeaderProvider func(ctx context.Context) (map[string]string, error)

HeaderProvider returns headers for one request. It is called with the request context, which carries the operation's deadline and the per-request cap, so a provider that honours its context cannot outlive the operation.

type Limits

type Limits struct {
	// MaxRequestBytes bounds one encoded request body.
	MaxRequestBytes int64
	// MaxResponseBytes bounds one encoded response body.
	MaxResponseBytes int64
	// MaxOperationBytes bounds the response bytes of one operation,
	// including every internal metadata query it makes.
	MaxOperationBytes int64
	// MaxOperationRequests bounds the pipeline calls of one operation.
	MaxOperationRequests int
	// MaxRows bounds the rows of one execute result.
	MaxRows int
	// MaxOperationRows bounds the rows decoded across one operation.
	MaxOperationRows int
	// MaxColumns bounds the columns of one execute result.
	MaxColumns int
	// MaxValueBytes bounds one text or blob cell.
	MaxValueBytes int
	// MaxArgs bounds the bound arguments of one statement.
	MaxArgs int
	// MaxStatementBytes bounds one SQL statement string.
	MaxStatementBytes int
	// Timeout bounds one HTTP request.
	Timeout int64 // nanoseconds; kept as int64 so Limits stays comparable
}

Limits bounds one operation. A zero field selects the package default; values above the corresponding absolute maximum are rejected by NormalizeLimits.

func NormalizeLimits

func NormalizeLimits(l Limits) (Limits, error)

NormalizeLimits returns l with package defaults filled in. It rejects a negative value and a value above the corresponding hard maximum.

type Result

type Result struct {
	Columns  []Column
	Rows     [][]Value
	Affected int64
}

Result is one fully decoded execute result.

type Value

type Value struct {
	Kind  ValueKind
	Int   int64
	Float float64
	Text  string
	Blob  []byte
}

Value is one decoded cell.

type ValueKind

type ValueKind uint8

ValueKind identifies the Hrana storage class of one cell.

const (
	Null ValueKind = iota
	Integer
	Float
	Text
	Blob
)

Storage classes.

Jump to

Keyboard shortcuts

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