vole
Site
Clone
Read only, no credentials:
https://rodent.software/vole.git
Write over SSH with a registered key:
git clone ssh://git@rodent.software/vole.git
Self-hosted forge that lives in your Git repo.
|
8f6548a6
Keenan Nemetz
3d ago
|
1 | // Package sqlstore opens Vole's SQLite files with one policy. |
| 2 | // | |
| 3 | // SQLite allows a single writer per database file, and every process that | |
| 4 | // touches instance.db — the server, the push hook, sshd's key lookup command — | |
| 5 | // shares that one slot. A handle that disagrees with the others about locking | |
| 6 | // does not merely run slower: a deferred transaction that reads and then writes | |
| 7 | // fails with SQLITE_BUSY the instant another writer commits in between, and the | |
| 8 | // busy timeout does not apply to that failure. Keeping the settings in one place | |
| 9 | // is what makes every writer queue instead of colliding. | |
| 10 | package sqlstore | |
| 11 | ||
| 12 | import ( | |
| 13 | "database/sql" | |
| 14 | "errors" | |
| 15 | "time" | |
| 16 | ||
| 17 | sqlite "modernc.org/sqlite" | |
| 18 | sqlite3 "modernc.org/sqlite/lib" | |
| 19 | ) | |
| 20 | ||
| 21 | // writeParams are the settings every connection that may write takes. | |
| 22 | // | |
| 23 | // - journal_mode=WAL lets readers work while a writer holds the lock, so a | |
| 24 | // page read does not wait for a commit. | |
| 25 | // - synchronous=NORMAL costs one fsync per checkpoint instead of one per | |
| 26 | // commit. A power loss can drop the newest commits but cannot corrupt the | |
| 27 | // file, and the instance is backed up with VACUUM INTO, so the durability | |
| 28 | // that matters is the copy rather than the last transaction. | |
| 29 | // - txlock=immediate takes the write lock when the transaction begins. A | |
| 30 | // deferred transaction that starts by reading cannot upgrade after another | |
| 31 | // writer commits; SQLite answers SQLITE_BUSY immediately and never calls | |
| 32 | // the busy handler, so the timeout buys nothing. | |
| 33 | // - busy_timeout makes a second process wait for the lock instead of failing | |
| 34 | // the request it is serving. | |
| 35 | const writeParams = "?_busy_timeout=5000&_journal_mode=WAL&_synchronous=NORMAL&_txlock=immediate" | |
| 36 | ||
| 37 | // readParams are for handles that only read. journal_mode is a persistent | |
| 38 | // property of the file, so a reader does not set it: that keeps a reader from | |
| 39 | // needing the write lock even once. query_only makes an accidental write on a | |
| 40 | // read path fail where it is written instead of somewhere later. | |
| 41 | const readParams = "?_busy_timeout=5000&_query_only=1" | |
| 42 | ||
| 43 | // Options are the settings a caller chooses per database. Everything else is | |
| 44 | // the policy above and is not a caller's decision. | |
| 45 | type Options struct { | |
| 46 | // MaxConns bounds the pool. A larger pool does not buy write concurrency: | |
| 47 | // SQLite takes one writer per file, so the extra connections only contend | |
| 48 | // for the same lock. | |
| 49 | MaxConns int | |
| 50 | // ReadOnly opens a handle that may only read. It skips the journal-mode | |
| 51 | // pragma and turns on query_only. | |
| 52 | ReadOnly bool | |
| 53 | // ForeignKeys turns on foreign-key enforcement. SQLite leaves it off per | |
| 54 | // connection, so a schema that declares references has to ask for it. | |
| 55 | ForeignKeys bool | |
| 56 | } | |
| 57 | ||
| 58 | // Open opens one database file with the instance's write policy. A path that | |
| 59 | // does not exist yet is created by SQLite on the first statement, which is how a | |
| 60 | // fresh instance gets its tables. | |
| 61 | func Open(path string, opt Options) (*sql.DB, error) { | |
| 62 | params := writeParams | |
| 63 | if opt.ReadOnly { | |
| 64 | params = readParams | |
| 65 | } | |
| 66 | if opt.ForeignKeys { | |
| 67 | params += "&_foreign_keys=1" | |
| 68 | } | |
| 69 | return open(path, params, opt.MaxConns) | |
| 70 | } | |
| 71 | ||
| 72 | func open(path, params string, maxConns int) (*sql.DB, error) { | |
| 73 | d, err := sql.Open("sqlite", path+params) | |
| 74 | if err != nil { | |
| 75 | return nil, err | |
| 76 | } | |
| 77 | if maxConns <= 0 { | |
| 78 | maxConns = 2 | |
| 79 | } | |
| 80 | d.SetMaxOpenConns(maxConns) | |
| 81 | return d, nil | |
| 82 | } | |
| 83 | ||
| 84 | // RetryBusy runs fn again when SQLite answers that another connection holds the | |
| 85 | // lock. The busy timeout already covers waiting, so this is for the answers that | |
| 86 | // arrive immediately: a lock released and retaken between two statements, or a | |
| 87 | // checkpoint that outlasted the timeout. | |
| 88 | // | |
| 89 | // fn must only touch the database. A function that also appends to the audit log | |
| 90 | // or writes a file must not be retried, because the second attempt would record | |
| 91 | // the same decision twice. | |
| 92 | func RetryBusy(fn func() error) error { | |
| 93 | const attempts = 3 | |
| 94 | delay := 20 * time.Millisecond | |
| 95 | var err error | |
| 96 | for attempt := 0; attempt < attempts; attempt++ { | |
| 97 | if err = fn(); err == nil || !IsBusy(err) { | |
| 98 | return err | |
| 99 | } | |
| 100 | if attempt < attempts-1 { | |
| 101 | time.Sleep(delay) | |
| 102 | delay *= 5 | |
| 103 | } | |
| 104 | } | |
| 105 | return err | |
| 106 | } | |
| 107 | ||
| 108 | // IsBusy reports whether err is SQLite refusing because another connection holds | |
| 109 | // the lock. Extended result codes carry the base code in the low byte, so the | |
| 110 | // mask covers BUSY_SNAPSHOT as well as BUSY. | |
| 111 | func IsBusy(err error) bool { | |
| 112 | var e *sqlite.Error | |
| 113 | if !errors.As(err, &e) { | |
| 114 | return false | |
| 115 | } | |
| 116 | switch e.Code() & 0xff { | |
| 117 | case sqlite3.SQLITE_BUSY, sqlite3.SQLITE_LOCKED: | |
| 118 | return true | |
| 119 | } | |
| 120 | return false | |
| 121 | } |