Skip to content

Commit 0f0716c

Browse files
committed
Inprocess Litestream primary.
1 parent 0286e50 commit 0f0716c

File tree

4 files changed

+51
-13
lines changed

4 files changed

+51
-13
lines changed

litestream/README.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
1-
# Litestream lightweight read-replicas
1+
# Litestream in-process replication and lightweight read-replicas
22

3-
This package implements the **EXPERIMENTAL** `"litestream"` SQLite VFS
4-
that offers Litestream [lightweight read-replicas](https://fly.io/blog/litestream-revamped/#lightweight-read-replicas).
3+
This package adds **EXPERIMENTAL** support for in-process [Litestream](https://litestream.io/).
54

6-
See the [example](vfs_test.go) for how to use.
5+
## Lightweight read-replicas
6+
7+
The `"litestream"` SQLite VFS implements Litestream
8+
[lightweight read-replicas](https://fly.io/blog/litestream-revamped/#lightweight-read-replicas).
9+
10+
See the [example](example_test.go) for how to use.
711

812
To improve performance,
913
increase `PollInterval` (and `MinLevel`) as much as you can,
1014
and set [`PRAGMA cache_size=N`](https://www.sqlite.org/pragma.html#pragma_cache_size)
11-
(or use `_pragma=cache_size(N)`).
15+
(or use `_pragma=cache_size(N)`).
16+
17+
## In-process replication
18+
19+
For disaster recovery, it is probably best if you run Litestream as a separate background process,
20+
as recommended by the [tutorial](https://litestream.io/getting-started/).
21+
22+
However, running Litestream as a background process requires
23+
compatible locking and cross-process shared memory WAL
24+
(see our [support matrix](https://github.com/ncruces/go-sqlite3/wiki/Support-matrix)).
25+
26+
If your OS lacks locking or shared memory support,
27+
you can use `NewPrimary` with the `sqlite3_dotlk` build tag to setup in-process replication.

litestream/api.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package litestream
33

44
import (
5+
"context"
56
"log/slog"
67
"sync"
78
"time"
@@ -76,3 +77,23 @@ func RemoveReplica(name string) {
7677
defer liteMtx.Unlock()
7778
delete(liteDBs, name)
7879
}
80+
81+
// NewPrimary creates a new primary that replicates through a client.
82+
// If restore is not nil, the database is first restored.
83+
func NewPrimary(ctx context.Context, path string, client litestream.ReplicaClient, restore *litestream.RestoreOptions) (*litestream.DB, error) {
84+
lsdb := litestream.NewDB(path)
85+
lsdb.Replica = litestream.NewReplicaWithClient(lsdb, client)
86+
87+
if restore != nil {
88+
err := lsdb.Replica.Restore(ctx, *restore)
89+
if err != nil {
90+
return nil, err
91+
}
92+
}
93+
94+
err := lsdb.Open()
95+
if err != nil {
96+
return nil, err
97+
}
98+
return lsdb, nil
99+
}

litestream/go.mod

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/ncruces/go-sqlite3 v0.30.1
88
github.com/ncruces/wbt v0.2.0
99
github.com/superfly/ltx v0.5.0
10+
golang.org/x/sync v0.18.0
1011
)
1112

1213
// github.com/ncruces/go-sqlite3
@@ -28,7 +29,7 @@ require (
2829
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
2930
github.com/prometheus/client_golang v1.23.2 // indirect
3031
github.com/prometheus/client_model v0.6.2 // indirect
31-
github.com/prometheus/common v0.67.3 // indirect
32+
github.com/prometheus/common v0.67.4 // indirect
3233
github.com/prometheus/procfs v0.19.2 // indirect
3334
github.com/psanford/sqlite3vfs v0.0.0-20240315230605-24e1d98cf361 // indirect
3435
go.yaml.in/yaml/v2 v2.4.3 // indirect
@@ -60,4 +61,4 @@ require (
6061
github.com/aws/smithy-go v1.22.5 // indirect
6162
)
6263

63-
replace modernc.org/sqlite => github.com/ncruces/go-sqlite3/litestream/modernc v0.0.0-20251109124432-99b097de3b79
64+
replace modernc.org/sqlite => github.com/ncruces/go-sqlite3/litestream/modernc v0.30.1

litestream/go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
115115
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
116116
github.com/ncruces/go-sqlite3 v0.30.1 h1:pHC3YsyRdJv4pCMB4MO1Q2BXw/CAa+Hoj7GSaKtVk+g=
117117
github.com/ncruces/go-sqlite3 v0.30.1/go.mod h1:UVsWrQaq1qkcal5/vT5lOJnZCVlR5rsThKdwidjFsKc=
118-
github.com/ncruces/go-sqlite3/litestream/modernc v0.0.0-20251109124432-99b097de3b79 h1:evpQceUV2vRbOe84U/QhBBchfqFERRHTx1JOadFFMLE=
119-
github.com/ncruces/go-sqlite3/litestream/modernc v0.0.0-20251109124432-99b097de3b79/go.mod h1:GSM2gXEOb9HIFFtsl0IUtnpvpDmVi7Kbp8z5GzwA0Tw=
118+
github.com/ncruces/go-sqlite3/litestream/modernc v0.30.1 h1:3SNAOrm+qmLprkZybcvBrVNHyt0QYHljUGxmOXnL+K0=
119+
github.com/ncruces/go-sqlite3/litestream/modernc v0.30.1/go.mod h1:GSM2gXEOb9HIFFtsl0IUtnpvpDmVi7Kbp8z5GzwA0Tw=
120120
github.com/ncruces/julianday v1.0.0 h1:fH0OKwa7NWvniGQtxdJRxAgkBMolni2BjDHaWTxqt7M=
121121
github.com/ncruces/julianday v1.0.0/go.mod h1:Dusn2KvZrrovOMJuOt0TNXL6tB7U2E8kvza5fFc9G7g=
122122
github.com/ncruces/wbt v0.2.0 h1:Q9zlKOBSZc7Yy/R2cGa35g6RKUUE3BjNIW3tfGC4F04=
@@ -133,8 +133,8 @@ github.com/prometheus/client_golang v1.23.2 h1:Je96obch5RDVy3FDMndoUsjAhG5Edi49h
133133
github.com/prometheus/client_golang v1.23.2/go.mod h1:Tb1a6LWHB3/SPIzCoaDXI4I8UHKeFTEQ1YCr+0Gyqmg=
134134
github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk=
135135
github.com/prometheus/client_model v0.6.2/go.mod h1:y3m2F6Gdpfy6Ut/GBsUqTWZqCUvMVzSfMLjcu6wAwpE=
136-
github.com/prometheus/common v0.67.3 h1:shd26MlnwTw5jksTDhC7rTQIteBxy+ZZDr3t7F2xN2Q=
137-
github.com/prometheus/common v0.67.3/go.mod h1:gP0fq6YjjNCLssJCQp0yk4M8W6ikLURwkdd/YKtTbyI=
136+
github.com/prometheus/common v0.67.4 h1:yR3NqWO1/UyO1w2PhUvXlGQs/PtFmoveVO0KZ4+Lvsc=
137+
github.com/prometheus/common v0.67.4/go.mod h1:gP0fq6YjjNCLssJCQp0yk4M8W6ikLURwkdd/YKtTbyI=
138138
github.com/prometheus/procfs v0.19.2 h1:zUMhqEW66Ex7OXIiDkll3tl9a1ZdilUOd/F6ZXw4Vws=
139139
github.com/prometheus/procfs v0.19.2/go.mod h1:M0aotyiemPhBCM0z5w87kL22CxfcH05ZpYlu+b4J7mw=
140140
github.com/psanford/sqlite3vfs v0.0.0-20240315230605-24e1d98cf361 h1:vAKifIJuYY306ZJSrwDgKonWcJGELijdaenABqbV03E=
@@ -169,8 +169,8 @@ golang.org/x/net v0.47.0 h1:Mx+4dIFzqraBXUugkia1OOvlD6LemFo1ALMHjrXDOhY=
169169
golang.org/x/net v0.47.0/go.mod h1:/jNxtkgq5yWUGYkaZGqo27cfGZ1c5Nen03aYrrKpVRU=
170170
golang.org/x/oauth2 v0.32.0 h1:jsCblLleRMDrxMN29H3z/k1KliIvpLgCkE6R8FXXNgY=
171171
golang.org/x/oauth2 v0.32.0/go.mod h1:lzm5WQJQwKZ3nwavOZ3IS5Aulzxi68dUSgRHujetwEA=
172-
golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug=
173-
golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
172+
golang.org/x/sync v0.18.0 h1:kr88TuHDroi+UVf+0hZnirlk8o8T+4MrK6mr60WkH/I=
173+
golang.org/x/sync v0.18.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
174174
golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc=
175175
golang.org/x/sys v0.38.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
176176
golang.org/x/text v0.31.0 h1:aC8ghyu4JhP8VojJ2lEHBnochRno1sgL6nEi9WGFGMM=

0 commit comments

Comments
 (0)