GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(server): stop fsyncing SQLite on every commit so slow disks no lo… · AdEx-Partners-DE/t3code@a74ce48

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps/server/src/persistence
    • Sqlite.test.ts
    • Sqlite.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎apps/server/src/persistence/Sqlite.test.ts‎

Copy file name to clipboardExpand all lines: apps/server/src/persistence/Sqlite.test.ts
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,21 @@ it.effect("collects planner statistics for an existing database on open", () =>
114114
);
115115
});
116116

117+
it.effect("only fsyncs the database file at WAL checkpoints", () => {
118+
const tempDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-sqlite-sync-"));
119+
const dbPath = NodePath.join(tempDir, "state.sqlite");
120+
121+
return Effect.gen(function* () {
122+
const sql = yield* SqlClient.SqlClient;
123+
const rows = yield* sql<{ readonly synchronous: number }>`PRAGMA synchronous`;
124+
// 1 is NORMAL.
125+
assert.equal(rows[0]?.synchronous, 1);
126+
}).pipe(
127+
Effect.provide(SqlitePersistence.layerFromPath(dbPath).pipe(Layer.provide(NodeServices.layer))),
128+
Effect.ensuring(Effect.sync(() => NodeFS.rmSync(tempDir, { recursive: true, force: true }))),
129+
);
130+
});
131+
117132
it.effect("applies busy_timeout in the shared persistence setup", () =>
118133
Effect.gen(function* () {
119134
const sql = yield* SqlClient.SqlClient;
Collapse file

‎apps/server/src/persistence/Sqlite.ts‎

Copy file name to clipboardExpand all lines: apps/server/src/persistence/Sqlite.ts
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ const layerSetup = Layer.effectDiscard(
1919
yield* sql`PRAGMA busy_timeout = 5000;`;
2020
yield* sql`PRAGMA foreign_keys = ON;`;
2121
yield* sql`PRAGMA journal_mode = WAL;`;
22+
// FULL fsyncs the WAL on every commit, on the main thread. Every provider
23+
// event is its own commit, so a slow or busy disk stalls the event loop
24+
// and clients drop. In WAL mode NORMAL only fsyncs at checkpoints. It
25+
// cannot corrupt the database and an app crash loses nothing; an OS crash
26+
// or power loss can roll back commits since the last checkpoint.
27+
yield* sql`PRAGMA synchronous = NORMAL;`;
2228
// PASSIVE checkpoints never shrink the -wal file, so it otherwise keeps its
2329
// largest size until the last connection closes.
2430
yield* sql.unsafe(`PRAGMA journal_size_limit = ${WAL_SIZE_LIMIT_BYTES};`);

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.