Sorry, not sure I'm following. A "new worker" is assigned for each request.
Sorry, not sure I'm following. A "new worker" is assigned for each request.
FinalizationRegistry created message in the log once during my initial request and after the connection count reaches around 250 to 300.I see FinalizationRegistry created message in the log once during my initial requestAlso just an FYI that this is expected. The Workers runtime creates one isolate per metal/server (*currently) and TCP connections are sticky so you'll be routed to the same metal for a good bit after you make the initial request. The global scope is only run once at isolate startup so you'll only see that message once.
.end(), but GC happens later.If I create a new drizzle instance with a huge schema for each Connection they are going to be kept in memory too.Sure, but the GC collector will be run more often if you're near the memory limit, so theoretically you'd see those connections being cleaned up more frequently if they were an issue. But I expect they're super lightweight anyway if the connection itself is closed.
SHOW TABLES query onlySHOW TABLES; query the max number of connection objects I see is around 360. If I multiply that by the 256 kB retained size I get around 92 MB. If I add the default heap size of the app which is around 36 MB I get 128 MB. Is this a coincidence?FinalizationRegistry but I think that's a platform limitation.workerd will run a GC sweep and clean things up.FinalizationRegistry created is probably because you just get routed to a different server and a new isolate is spun up.Socket within mysql2 PromiseConnection.connection.stream prevents it from being cleared.ssl_ecdh_curve=prime256v1 is supported. Assuming that's the one you mean, yes it works.
sql.end())waitUntil to do so, or for anything else in your script?import { createConnection, type Connection } from "mysql2/promise";
const registry = new FinalizationRegistry((value) => {
console.log(`"${value}" was GC'd`);
});
console.log("FinalizationRegistry created");
const connections: WeakRef<Connection>[] = [];
export default defineEventHandler(async (event) => {
const { env } = event.context.cloudflare;
const connection = await createConnection({
host: env.HYPERDRIVE.host,
user: env.HYPERDRIVE.user,
password: env.HYPERDRIVE.password,
database: env.HYPERDRIVE.database,
port: env.HYPERDRIVE.port,
disableEval: true
});
try {
registry.register(connection, "MySQL2 connection");
connections.push(new WeakRef(connection));
await connection.query("SELECT * FROM `my_table`;");
event.waitUntil(connection.end());
return Response.json({
connections: connections.filter((ref) => ref.deref() !== undefined).length
});
} catch (e) {
console.error(e);
}
});event.waitUntil(
connection.end().then(() => {
(connection as any).connection.stream.destroy();
})
);