Skip to content

Commit

Permalink
Use Postgres as cache/message queue backend
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Sep 22, 2024
1 parent 208386c commit 1084bda
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
Binary file modified bun.lockb
Binary file not shown.
3 changes: 1 addition & 2 deletions docs/src/content/docs/install/env.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ The URL of the PostgreSQL database, e.g.,

The URL of the Redis server, e.g., `redis://localhost/0`.

If not set, Hollo will use an in-memory cache and in-process job queue, which
means that the cache and job queue will be lost when the server restarts.
If not set, Hollo will use PostgreSQL for caching and message queueing.

### `HOME_URL` <Badge text="Optional" />

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
"dependencies": {
"@aws-sdk/client-s3": "^3.577.0",
"@aws-sdk/credential-providers": "^3.577.0",
"@fedify/fedify": "1.0.0-dev.388",
"@fedify/fedify": "^1.0.0-dev.410",
"@fedify/markdown-it-hashtag": "0.2.0",
"@fedify/markdown-it-mention": "^0.1.1",
"@fedify/redis": "^0.1.1",
"@fedify/postgres": "^0.1.0-dev.3",
"@fedify/redis": "^0.2.0-dev.10",
"@hono/zod-validator": "^0.2.1",
"@js-temporal/polyfill": "^0.4.4",
"@logtape/logtape": "^0.4.2",
Expand Down
6 changes: 3 additions & 3 deletions src/db.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getLogger } from "@logtape/logtape";
import type { Logger } from "drizzle-orm/logger";
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import createPostgres from "postgres";
import * as schema from "./schema";

// biome-ignore lint/complexity/useLiteralKeys: tsc rants about this (TS4111)
Expand Down Expand Up @@ -56,7 +56,7 @@ class LogTapeLogger implements Logger {
}
}

const client = postgres(databaseUrl, { connect_timeout: 5 });
export const db = drizzle(client, { schema, logger: new LogTapeLogger() });
export const postgres = createPostgres(databaseUrl, { connect_timeout: 5 });
export const db = drizzle(postgres, { schema, logger: new LogTapeLogger() });

export default db;
12 changes: 6 additions & 6 deletions src/federation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
Image,
type KvStore,
Like,
MemoryKvStore,
type MessageQueue,
Note,
PropertyValue,
Expand All @@ -26,6 +25,7 @@ import {
importJwk,
isActor,
} from "@fedify/fedify";
import { PostgresKvStore, PostgresMessageQueue } from "@fedify/postgres";
import { RedisKvStore, RedisMessageQueue } from "@fedify/redis";
import { getLogger } from "@logtape/logtape";
import { parse } from "@std/semver";
Expand All @@ -40,7 +40,7 @@ import {
like,
} from "drizzle-orm";
import metadata from "../../package.json" with { type: "json" };
import db from "../db";
import { db, postgres } from "../db";
import { createRedis, getRedisUrl } from "../redis";
import {
type NewLike,
Expand Down Expand Up @@ -69,13 +69,13 @@ import {
const logger = getLogger(["hollo", "federation"]);

let kv: KvStore;
let queue: MessageQueue | undefined;
let queue: MessageQueue;

if (getRedisUrl() == null) {
kv = new MemoryKvStore();
queue = undefined;
kv = new PostgresKvStore(postgres);
queue = new PostgresMessageQueue(postgres);
logger.warn(
"No REDIS_URL is defined, using in-memory store and in-process queue.",
"No REDIS_URL is defined, using PostgresKvStore and PostgresMessageQueue.",
);
} else {
kv = new RedisKvStore(createRedis());
Expand Down

0 comments on commit 1084bda

Please sign in to comment.