a tool for shared writing and social publishing
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

add performance logging to push

+18 -14
+18 -14
app/api/rpc/[command]/push.ts
··· 7 7 import type { Env } from "./route"; 8 8 import { cachedServerMutationContext } from "src/replicache/cachedServerMutationContext"; 9 9 import { drizzle } from "drizzle-orm/node-postgres"; 10 - import { Pool } from "pg"; 11 - import { attachDatabasePool } from "@vercel/functions"; 12 - import { DbPool } from "@vercel/functions/db-connections"; 10 + import { Lock } from "src/utils/lock"; 11 + import { pool } from "supabase/pool"; 13 12 14 13 const mutationV0Schema = z.object({ 15 14 id: z.number(), ··· 45 44 ]); 46 45 47 46 type PushRequestZ = z.infer<typeof pushRequestSchema>; 48 - 49 - const pool = new Pool({ 50 - idleTimeoutMillis: 5000, 51 - min: 1, 52 - connectionString: process.env.DB_URL, 53 - }); 54 - 55 - // Attach the pool to ensure idle connections close before suspension 56 - attachDatabasePool(pool as DbPool); 57 - 58 - import { Lock } from "src/utils/lock"; 59 47 60 48 let locks = new Map<string, Lock>(); 61 49 ··· 72 60 result: { error: "VersionNotSupported", versionType: "push" } as const, 73 61 }; 74 62 } 63 + let timeWaitingForLock: number; 64 + let timeWaitingForDbConnection: number; 65 + let timeProcessingMutations: number = 0; 75 66 67 + let start = performance.now(); 76 68 let client = await pool.connect(); 69 + timeWaitingForDbConnection = performance.now() - start; 70 + start = performance.now(); 77 71 const db = drizzle(client); 78 72 let channel = supabase.channel(`rootEntity:${rootEntity}`); 79 73 let lock = locks.get(token.id); ··· 82 76 locks.set(token.id, lock); 83 77 } 84 78 let release = await lock.lock(); 79 + timeWaitingForLock = performance.now() - start; 80 + start = performance.now(); 85 81 try { 86 82 await db.transaction(async (tx) => { 87 83 let clientGroup = await getClientGroup(tx, pushRequest.clientGroupID); ··· 135 131 }); 136 132 await flush(); 137 133 }); 134 + timeProcessingMutations = performance.now() - start; 138 135 139 136 await channel.send({ 140 137 type: "broadcast", ··· 142 139 payload: { message: "poke" }, 143 140 }); 144 141 } catch (e) { 142 + timeProcessingMutations = performance.now() - start; 145 143 console.log(e); 146 144 } finally { 145 + console.log( 146 + `Total Elapsed: ${timeProcessingMutations.toFixed(2)}ms 147 + Time Waiting for DB Connection: ${timeWaitingForDbConnection.toFixed(2)}ms 148 + Time Waiting For Lock: ${timeWaitingForLock.toFixed(2)}ms 149 + `, 150 + ); 147 151 client.release(); 148 152 release(); 149 153 supabase.removeChannel(channel);