Repository navigation
feat(proxy): persist proxy logs to SQLite #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -1,14 +1,64 @@ | ||||||||
| /** | ||||||||
| * Proxy Logger — In-memory ring buffer for proxy events | ||||||||
| * Mirrors the call-log pattern used by RequestLoggerV2 | ||||||||
| * Proxy Logger — Hybrid in-memory + SQLite persistence | ||||||||
| * | ||||||||
| * Keeps a fast in-memory ring buffer for real-time dashboard AND | ||||||||
| * persists to SQLite so logs survive server restarts. | ||||||||
| * | ||||||||
| * Pattern follows callLogs.js (T-15 decomposition). | ||||||||
| */ | ||||||||
| import { v4 as uuidv4 } from "uuid"; | ||||||||
| import { getDbInstance, isCloud, isBuildPhase } from "./db/core.js"; | ||||||||
|
|
||||||||
| const shouldPersistToDisk = !isCloud && !isBuildPhase; | ||||||||
|
|
||||||||
| const MAX_ENTRIES = 500; | ||||||||
| const proxyLogs = []; | ||||||||
|
|
||||||||
| // ──────────────── Startup: hydrate from DB ──────────────── | ||||||||
|
|
||||||||
| function loadFromDb() { | ||||||||
| if (!shouldPersistToDisk) return; | ||||||||
| try { | ||||||||
| const db = getDbInstance(); | ||||||||
| const rows = db | ||||||||
| .prepare("SELECT * FROM proxy_logs ORDER BY timestamp DESC LIMIT ?") | ||||||||
| .all(MAX_ENTRIES); | ||||||||
|
|
||||||||
| for (const row of rows) { | ||||||||
| proxyLogs.push({ | ||||||||
| id: row.id, | ||||||||
| timestamp: row.timestamp, | ||||||||
| status: row.status || "success", | ||||||||
| proxy: row.proxy_host | ||||||||
| ? { type: row.proxy_type, host: row.proxy_host, port: row.proxy_port } | ||||||||
| : null, | ||||||||
| level: row.level || "direct", | ||||||||
| levelId: row.level_id || null, | ||||||||
| provider: row.provider || null, | ||||||||
| targetUrl: row.target_url || null, | ||||||||
| publicIp: row.public_ip || null, | ||||||||
| latencyMs: row.latency_ms || 0, | ||||||||
| error: row.error || null, | ||||||||
| connectionId: row.connection_id || null, | ||||||||
| comboId: row.combo_id || null, | ||||||||
| account: row.account || null, | ||||||||
| tlsFingerprint: row.tls_fingerprint === 1, | ||||||||
| }); | ||||||||
| } | ||||||||
|
|
||||||||
| if (proxyLogs.length > 0) { | ||||||||
| console.log(`[proxyLogger] Loaded ${proxyLogs.length} proxy logs from SQLite`); | ||||||||
| } | ||||||||
| } catch (err) { | ||||||||
| console.warn("[proxyLogger] Failed to load from DB:", err.message); | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| loadFromDb(); | ||||||||
|
|
||||||||
| // ──────────────── Log a proxy event ──────────────── | ||||||||
|
|
||||||||
| /** | ||||||||
| * Log a proxy event | ||||||||
| * @param {Object} entry | ||||||||
| * @param {"success"|"error"|"timeout"} entry.status | ||||||||
| * @param {Object} entry.proxy - { type, host, port } | ||||||||
|
|
@@ -21,6 +71,7 @@ const proxyLogs = []; | |||||||
| * @param {string} [entry.error] | ||||||||
| * @param {string} [entry.connectionId] | ||||||||
| * @param {string} [entry.comboId] | ||||||||
|
||||||||
| * @param {string} [entry.comboId] | |
| * @param {string} [entry.comboId] | |
| * @param {string} [entry.account] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation for trimming the database runs a COUNT(*) query and a DELETE query on every single log event. This can become a performance bottleneck, especially under high load, as COUNT(*) requires a full table scan. A more efficient approach would be to perform this cleanup operation periodically rather than on every write.
I suggest making the trimming logic probabilistic, for example, running it on average 1% of the time. This significantly reduces the overhead while still keeping the table size under control.
// Trim old entries periodically to avoid overhead on every write.
// This runs on average once every 100 calls.
if (Math.random() < 0.01) {
const count = db.prepare("SELECT COUNT(*) as cnt FROM proxy_logs").get()?.cnt || 0;
if (count > MAX_ENTRIES) {
db.prepare(
`DELETE FROM proxy_logs WHERE id IN (
SELECT id FROM proxy_logs ORDER BY timestamp ASC LIMIT ?
)`
).run(count - MAX_ENTRIES);
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
statusandlevelcolumns can beNULL, but the application logic inproxyLogger.jsalways provides default values ('success' and 'direct' respectively). This could mask issues where a log is saved without these values. To ensure data integrity, it's better to enforce this at the database level by making these columnsNOT NULL.