sqliteha

package module
v0.18.1 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 14 Imported by: 2 modules, 1 package

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.

README

go-sqlite-ha

Go database/sql driver based on CGO FREE modernc.org/sqlite, providing high availability for SQLite databases.

Features

  • Built on the robust foundation of modernc.org/sqlite.
  • High availability support for SQLite databases.
  • Replication: Synchronize data across nodes using NATS.
  • Customize the replication strategy
  • Leaderless clusters: Read/Write from/to any node. Last-writer wins by default, but you can customize conflict resolutions by implementing ChangeSetInterceptor.
  • Embedded or External NATS: Choose between an embedded NATS server or an external one for replication.
  • Easy to integrate with existing Go projects.

Installation

go get github.com/litesql/go-sqlite-ha

Usage

package main

import (
    "database/sql"
    _ "github.com/litesql/go-sqlite-ha"
)

func main() {
    db, err := sql.Open("sqlite-ha", "file:example.db?_journal=WAL&_timeout=5000&replicationURL=nats://broker:4222&name=node0")
    if err != nil {
        panic(err)
    }
    defer db.Close()

    // Use db to interact with your database
}
Using Connector
package main

import (
    "database/sql"
    sqliteha "github.com/litesql/go-sqlite-ha"
)

func main() {
    c, err := sqliteha.NewConnector("file:example.db?_journal=WAL&_timeout=5000",
		ha.WithName("node1"),
		ha.WithEmbeddedNatsConfig(&ha.EmbeddedNatsConfig{
			Port: 4222,
		}))
	if err != nil {
		panic(err)
	}
	defer c.Close()

	db := sql.OpenDB(c)
	defer db.Close()

    // Use db to interact with your database
}

Configuration

See options available

Observability

go-sqlite-ha adds OpenTelemetry metrics and traces for SQL operations, transaction operations (including two-phase commit stages), and backups. It does not record SQL text or parameter values. Metrics are go_sqlite_ha.db.operations, go_sqlite_ha.db.operation.duration, and go_sqlite_ha.backup.size. They use bounded operation and route attributes (local, local_2pc, two_phase_commit, leader_grpc, proxied_db, or cross_shard) plus success/error/cancelled result. Spans use the go_sqlite_ha.db.* names and go_sqlite_ha.backup.

The driver uses the global OpenTelemetry meter and tracer providers. Configure the global providers before opening the connector. To enable the go-ha cluster and replication telemetry with those same providers, pass ha.WithOpenTelemetry(nil, nil) or otel=true when creating the connector. The /healthz, /readyz, and /metrics handlers provided by go-ha remain the cluster-level observability endpoints. The driver's metrics are emitted through OpenTelemetry; they are not added to go-ha's standalone Prometheus /metrics registry. To scrape them with Prometheus, configure an OpenTelemetry Prometheus exporter in the application.

Resilience tests

The integration resilience tests start an in-process JetStream server and use temporary SQLite databases; they do not require external NATS or Docker:

go test ./... -run '^TestResilience' -count=1

They cover leader failover, NATS server interruption and reconnection, duplicate publication deduplication, node restart, and replay of changesets published while a subscriber is offline.

Projects using go-sqlite-ha

  • HA: Highly available leaderless SQLite cluster with HTTP and PostgreSQL Wire Protocol
  • PocketBase HA: Highly available leaderless PocketBase cluster

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrTimedOut = ha.ErrTimedOut

Functions

func Backup

func Backup(ctx context.Context, db *sql.DB, w io.Writer) error

func NewBackupFn added in v0.18.1

func NewBackupFn(opts ...BackupOption) ha.BackupFn

func NewConnector

func NewConnector(name string, opts ...ha.Option) (*ha.Connector, error)

func NewHistoryModule added in v0.10.1

func NewHistoryModule(connector *ha.Connector) vtab.Module

Types

type BackupOption added in v0.18.1

type BackupOption func(*backupConfig)

func WithBackupDestVFS added in v0.18.1

func WithBackupDestVFS(vfsName string) BackupOption

func WithBackupOutputCopy added in v0.18.1

func WithBackupOutputCopy(fn func(dst io.Writer, src io.Reader) (int64, error)) BackupOption

func WithBackupPagesPerStep added in v0.18.1

func WithBackupPagesPerStep(pages int32) BackupOption

func WithBackupStepPause added in v0.18.1

func WithBackupStepPause(d time.Duration) BackupOption

type Conn

type Conn = ha.Conn

type ConnHooksRegister

type ConnHooksRegister interface {
	RegisterPreUpdateHook(sqlite.PreUpdateHookFn)
	RegisterCommitHook(sqlite.CommitHookFn)
	RegisterRollbackHook(sqlite.RollbackHookFn)
}

type Driver

type Driver struct {
	ConnectionHook sqlite.ConnectionHookFn
	Options        []ha.Option
	// contains filtered or unexported fields
}

func (*Driver) Open

func (d *Driver) Open(name string) (driver.Conn, error)

func (*Driver) OpenConnector

func (d *Driver) OpenConnector(name string) (driver.Connector, error)

type ProxiedQuerierExecer added in v0.11.8

type ProxiedQuerierExecer = ha.ProxiedQuerierExecer

type SQLiteConn

type SQLiteConn interface {
	driver.Conn
	driver.ConnBeginTx
	driver.ConnPrepareContext
	sqlite.ExecQuerierContext
	sqlite.HookRegisterer
	NewBackup(string) (*sqlite.Backup, error)
	Serialize() ([]byte, error)
	Deserialize([]byte) error
}

Directories

Path Synopsis
_examples
2pc/peer1 command
2pc/peer2 command
node1 command
node2 command
shards command

Jump to

Keyboard shortcuts

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