Skip to content

Commit

Permalink
chore: neon dialect (#1502)
Browse files Browse the repository at this point in the history
  • Loading branch information
davenewza authored Jun 24, 2024
1 parent a23b591 commit 3321ac9
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 20 deletions.
5 changes: 4 additions & 1 deletion packages/functions-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"vitest": "^0.34.6"
},
"dependencies": {
"@neondatabase/serverless": "^0.9.3",
"@opentelemetry/api": "^1.7.0",
"@opentelemetry/exporter-trace-otlp-proto": "^0.46.0",
"@opentelemetry/resources": "^1.19.0",
Expand All @@ -28,7 +29,9 @@
"json-rpc-2.0": "^1.7.0",
"ksuid": "^3.0.0",
"kysely": "^0.25.0",
"kysely-neon": "^1.3.0",
"pg": "^8.11.3",
"traceparent": "^1.0.0"
"traceparent": "^1.0.0",
"ws": "^8.17.1"
}
}
99 changes: 99 additions & 0 deletions packages/functions-runtime/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 28 additions & 19 deletions packages/functions-runtime/src/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const { AuditContextPlugin } = require("./auditing");
const pg = require("pg");
const { PROTO_ACTION_TYPES } = require("./consts");
const { withSpan } = require("./tracing");
import { NeonDialect } from "kysely-neon";
import ws from 'ws';

// withDatabase is responsible for setting the correct database client in our AsyncLocalStorage
// so that the the code in a custom function uses the correct client.
Expand Down Expand Up @@ -148,27 +150,34 @@ function getDialect() {
return parseFloat(val);
});

return new PostgresDialect({
pool: new InstrumentedPool({
Client: InstrumentedClient,
// Increased idle time before closing a connection in the local pool (from 10s default).
// Establising a new connection on (almost) every functions query can be expensive, so this
// will reduce having to open connections as regularly. https://node-postgres.com/apis/pool
//
// NOTE: We should consider setting this to 0 (i.e. never pool locally) and open and close
// connections with each invocation. This is because the freeze/thaw nature of lambdas can cause problems
// with long-lived connections - see https://github.com/brianc/node-postgres/issues/2718
// Once we're "fully regional" this should not be a performance problem anymore.
//
// Although I doubt we will run into these freeze/thaw issues if idleTimeoutMillis is always shorter than the
// time is takes for a lambda to freeze (which is not a constant, but could be as short as several minutes,
// https://www.pluralsight.com/resources/blog/cloud/how-long-does-aws-lambda-keep-your-idle-functions-around-before-a-cold-start)
idleTimeoutMillis: 0,
connectionTimeoutMillis: 0,
connectionString: mustEnv("KEEL_DB_CONN"),
}),
return new NeonDialect({
connectionString: "postgresql://neondb_owner:[email protected]/neondb?sslmode=require",//mustEnv("KEEL_DB_CONN"),
webSocketConstructor: ws,
});

// return new PostgresDialect({
// connectisonString: mustEnv("KEEL_DB_CONN"),
// client: new InstrumentedClient,
// // pool: new InstrumentedPool({
// // Client: InstrumentedClient,
// // // Increased idle time before closing a connection in the local pool (from 10s default).
// // // Establising a new connection on (almost) every functions query can be expensive, so this
// // // will reduce having to open connections as regularly. https://node-postgres.com/apis/pool
// // //
// // // NOTE: We should consider setting this to 0 (i.e. never pool locally) and open and close
// // // connections with each invocation. This is because the freeze/thaw nature of lambdas can cause problems
// // // with long-lived connections - see https://github.com/brianc/node-postgres/issues/2718
// // // Once we're "fully regional" this should not be a performance problem anymore.
// // //
// // // Although I doubt we will run into these freeze/thaw issues if idleTimeoutMillis is always shorter than the
// // // time is takes for a lambda to freeze (which is not a constant, but could be as short as several minutes,
// // // https://www.pluralsight.com/resources/blog/cloud/how-long-does-aws-lambda-keep-your-idle-functions-around-before-a-cold-start)
// // idleTimeoutMillis: 0,
// // connectionTimeoutMillis: 0,
// // connectionString: mustEnv("KEEL_DB_CONN"),
// // }),
// });

default:
throw Error("unexpected KEEL_DB_CONN_TYPE: " + dbConnType);
}
Expand Down

0 comments on commit 3321ac9

Please sign in to comment.