Skip to content

Commit

Permalink
Add name and timeout params. (tldraw#5129)
Browse files Browse the repository at this point in the history
Improve `getPostgres` helper.:
- We can now pass in a name, which allows us to see the who is holding
an open connection in `pg_stat_activity`. See `application_name` column
below.
![CleanShot 2024-12-16 at 15 47
58@2x](https://github.com/user-attachments/assets/a9f62e5c-4198-478d-9020-12a74841c48e)
- `idleTimeout` option with a default of 30.

### Change type

- [ ] `bugfix`
- [x] `improvement`
- [ ] `feature`
- [ ] `api`
- [ ] `other`
  • Loading branch information
MitjaBezensek authored Dec 17, 2024
1 parent 0895148 commit de9694f
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions apps/dotcom/client/public/tla/locales-compiled/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"8ff3d0a705": "Rate limited",
"902b0d55fd": "Error",
"904a830405": "Rename",
"91e49bb0eb": "Copy to my files",
"9563b82670": "Uploading {count} .tldr {count, plural, one {file} other {files}}…",
"96165d6df5": "This month",
"9914a0ce04": "Light",
Expand Down
3 changes: 3 additions & 0 deletions apps/dotcom/client/public/tla/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@
"904a830405": {
"translation": "Rename"
},
"91e49bb0eb": {
"translation": "Copy to my files"
},
"9563b82670": {
"translation": "Uploading {count} .tldr {count, plural, one {file} other {files}}…"
},
Expand Down
4 changes: 2 additions & 2 deletions apps/dotcom/sync-worker/src/TLDrawDurableObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ export class TLDrawDurableObject extends DurableObject {
return this._fileRecordCache
}
try {
const postgres = getPostgres(this.env, { pooled: true })
const postgres = getPostgres(this.env, { pooled: true, name: 'TLDrawDurableObject' })
const fileRecord =
await postgres`SELECT * FROM public.file WHERE ID = ${this.documentInfo.slug}`
this._fileRecordCache = fileRecord[0] as TlaFile
Expand Down Expand Up @@ -588,7 +588,7 @@ export class TLDrawDurableObject extends DurableObject {

// Update the updatedAt timestamp in the database
if (this.documentInfo.isApp) {
const pg = getPostgres(this.env, { pooled: true })
const pg = getPostgres(this.env, { pooled: true, name: 'TLDrawDurableObject' })
await pg`UPDATE public.file SET "updatedAt" = ${new Date().getTime()} WHERE id = ${this.documentInfo.slug}`
await pg.end()
}
Expand Down
2 changes: 1 addition & 1 deletion apps/dotcom/sync-worker/src/TLPostgresReplicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ export class TLPostgresReplicator extends DurableObject<Environment> {
// preserve the promise so any awaiters do eventually get resolved
// TODO: set a timeout on the promise?
promise,
db: getPostgres(this.env, { pooled: false }),
db: getPostgres(this.env, { pooled: false, name: 'TLPostgresReplicator' }),
sequenceId: uniqueId(),
}
const subscription = await this.state.db.subscribe(
Expand Down
2 changes: 1 addition & 1 deletion apps/dotcom/sync-worker/src/TLUserDurableObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class TLUserDurableObject extends DurableObject<Environment> {
this.sentry = createSentry(ctx, env)
this.replicator = getReplicator(env)

this.db = getPostgres(env, { pooled: true })
this.db = getPostgres(env, { pooled: true, name: 'TLUserDurableObject' })
this.debug('created')
this.measure = env.MEASURE
}
Expand Down
9 changes: 8 additions & 1 deletion apps/dotcom/sync-worker/src/getPostgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { Environment } from './types'
/**
* `pooled` should be almost always be true.
*/
export function getPostgres(env: Environment, { pooled }: { pooled: boolean }) {
export function getPostgres(
env: Environment,
{ pooled, name, idleTimeout = 30 }: { pooled: boolean; name: string; idleTimeout?: number }
) {
return postgres(
pooled ? env.BOTCOM_POSTGRES_POOLED_CONNECTION_STRING : env.BOTCOM_POSTGRES_CONNECTION_STRING,
{
Expand All @@ -16,6 +19,10 @@ export function getPostgres(env: Environment, { pooled }: { pooled: boolean }) {
serialize: (value: number) => String(value), // Convert number to string
},
},
idle_timeout: idleTimeout,
connection: {
application_name: name,
},
}
)
}

0 comments on commit de9694f

Please sign in to comment.