@vercel/functions 3.11.2 → 3.11.3-snapshot-23cc3b13430e
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @vercel/functions
|
|
2
2
|
|
|
3
|
+
## 3.11.3
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 7e0ddee: Associate PostgreSQL pool cleanup with the request that checked out the connection. Keep cleanup holds independent per pool, preserve a live owner with a later deadline, and ignore untracked releases without replacing an existing hold. Repeated attachment to the same PostgreSQL pool is now idempotent.
|
|
8
|
+
|
|
3
9
|
## 3.11.2
|
|
4
10
|
|
|
5
11
|
### Patch Changes
|
|
@@ -1,8 +1,13 @@
|
|
|
1
|
+
type PgConnectCallback = (error: Error | undefined, client?: object, release?: (error?: Error | boolean) => void) => void;
|
|
1
2
|
interface PgPool {
|
|
2
3
|
options?: {
|
|
3
4
|
idleTimeoutMillis?: number | null;
|
|
4
5
|
};
|
|
5
|
-
on?: (event: 'release', listener: (
|
|
6
|
+
on?: (event: 'release', listener: (error: unknown, client?: object) => void) => void;
|
|
7
|
+
connect?: {
|
|
8
|
+
(): Promise<object>;
|
|
9
|
+
(callback: PgConnectCallback): void;
|
|
10
|
+
};
|
|
6
11
|
}
|
|
7
12
|
interface MySQLPool {
|
|
8
13
|
config?: {
|
package/db-connections/index.js
CHANGED
|
@@ -56,54 +56,94 @@ function getIdleTimeout(dbPool) {
|
|
|
56
56
|
}
|
|
57
57
|
return 1e4;
|
|
58
58
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
59
|
+
const fallbackDuration = 15 * 60 * 1e3;
|
|
60
|
+
function captureRequest() {
|
|
61
|
+
const context = (0, import_get_context.getContext)();
|
|
62
|
+
return {
|
|
63
|
+
waitUntil: context.waitUntil?.bind(context),
|
|
64
|
+
deadline: (0, import_deadline.getDeadline)()?.getTime() ?? Date.now() + fallbackDuration
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
const idleTimeouts = /* @__PURE__ */ new WeakMap();
|
|
68
|
+
const attachedPgPools = /* @__PURE__ */ new WeakSet();
|
|
69
|
+
function trackPgRequests(pgPool) {
|
|
70
|
+
if (typeof pgPool.connect !== "function")
|
|
71
|
+
return;
|
|
72
|
+
const requests = /* @__PURE__ */ new WeakMap();
|
|
73
|
+
const connect = pgPool.connect.bind(pgPool);
|
|
74
|
+
function connectWithContext(callback) {
|
|
75
|
+
const request = captureRequest();
|
|
76
|
+
if (callback) {
|
|
77
|
+
return connect(function(...args) {
|
|
78
|
+
const [error, client] = args;
|
|
79
|
+
if (!error && client)
|
|
80
|
+
requests.set(client, request);
|
|
81
|
+
callback.apply(this, args);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
return connect().then((client) => {
|
|
85
|
+
requests.set(client, request);
|
|
86
|
+
return client;
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
pgPool.connect = connectWithContext;
|
|
90
|
+
return requests;
|
|
91
|
+
}
|
|
92
|
+
function waitUntilIdleTimeout(dbPool, request = captureRequest()) {
|
|
64
93
|
if (!process.env.VERCEL_URL || // This is not set during builds where we don't need to wait for idle connections using the mechanism
|
|
65
94
|
!process.env.VERCEL_REGION) {
|
|
66
95
|
return;
|
|
67
96
|
}
|
|
68
|
-
if (
|
|
69
|
-
|
|
70
|
-
|
|
97
|
+
if (!request?.waitUntil) {
|
|
98
|
+
console.warn("Pool release event triggered outside of request scope.");
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
let { waitUntil, deadline } = request;
|
|
102
|
+
const previous = idleTimeouts.get(dbPool);
|
|
103
|
+
if (previous) {
|
|
104
|
+
if (previous.deadline > deadline) {
|
|
105
|
+
waitUntil = previous.waitUntil;
|
|
106
|
+
deadline = previous.deadline;
|
|
107
|
+
}
|
|
108
|
+
clearTimeout(previous.timeout);
|
|
109
|
+
previous.resolve();
|
|
71
110
|
}
|
|
72
|
-
const promise = new Promise((resolve) => {
|
|
73
|
-
idleTimeoutResolve = resolve;
|
|
74
|
-
});
|
|
75
|
-
const deadline = (0, import_deadline.getDeadline)();
|
|
76
|
-
const remainingDuration = deadline ? deadline.getTime() - Date.now() - 1e3 : maximumDuration;
|
|
77
111
|
const waitTime = Math.min(
|
|
78
112
|
getIdleTimeout(dbPool) + 100,
|
|
79
|
-
Math.max(100,
|
|
113
|
+
Math.max(100, deadline - Date.now() - 1e3)
|
|
80
114
|
);
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
115
|
+
const promise = new Promise((resolve) => {
|
|
116
|
+
const timeout = setTimeout(() => {
|
|
117
|
+
if (idleTimeouts.get(dbPool)?.timeout === timeout) {
|
|
118
|
+
idleTimeouts.delete(dbPool);
|
|
119
|
+
}
|
|
120
|
+
resolve();
|
|
121
|
+
if (DEBUG) {
|
|
122
|
+
console.log(
|
|
123
|
+
"Database pool idle timeout reached. Releasing connections."
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
}, waitTime);
|
|
127
|
+
idleTimeouts.set(dbPool, { timeout, resolve, waitUntil, deadline });
|
|
128
|
+
});
|
|
129
|
+
waitUntil(promise);
|
|
93
130
|
}
|
|
94
131
|
function attachDatabasePool(dbPool) {
|
|
95
|
-
if (idleTimeout) {
|
|
96
|
-
idleTimeoutResolve?.();
|
|
97
|
-
clearTimeout(idleTimeout);
|
|
98
|
-
}
|
|
99
132
|
if ("on" in dbPool && dbPool.on && "options" in dbPool && "idleTimeoutMillis" in dbPool.options) {
|
|
100
133
|
const pgPool = dbPool;
|
|
101
|
-
|
|
134
|
+
if (attachedPgPools.has(pgPool))
|
|
135
|
+
return;
|
|
136
|
+
const requests = trackPgRequests(pgPool);
|
|
137
|
+
pgPool.on("release", (_error, client) => {
|
|
102
138
|
if (DEBUG) {
|
|
103
139
|
console.log("Client released from pool");
|
|
104
140
|
}
|
|
105
|
-
|
|
141
|
+
const request = requests ? (client && requests.get(client)) ?? null : captureRequest();
|
|
142
|
+
if (client)
|
|
143
|
+
requests?.delete(client);
|
|
144
|
+
waitUntilIdleTimeout(dbPool, request);
|
|
106
145
|
});
|
|
146
|
+
attachedPgPools.add(pgPool);
|
|
107
147
|
return;
|
|
108
148
|
} else if ("on" in dbPool && dbPool.on && "config" in dbPool && dbPool.config && "connectionConfig" in dbPool.config) {
|
|
109
149
|
const mysqlPool = dbPool;
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
> **attachDatabasePool**(`dbPool`): `void`
|
|
8
8
|
|
|
9
|
-
Defined in: [packages/functions/src/db-connections/index.ts:
|
|
9
|
+
Defined in: [packages/functions/src/db-connections/index.ts:302](https://github.com/vercel/vercel/blob/main/packages/functions/src/db-connections/index.ts#L302)
|
|
10
10
|
|
|
11
11
|
**`Experimental`**
|
|
12
12
|
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
> `const` **experimental\_attachDatabasePool**: (`dbPool`) => `void` = `attachDatabasePool`
|
|
8
8
|
|
|
9
|
-
Defined in: [packages/functions/src/db-connections/index.ts:
|
|
9
|
+
Defined in: [packages/functions/src/db-connections/index.ts:404](https://github.com/vercel/vercel/blob/main/packages/functions/src/db-connections/index.ts#L404)
|
|
10
10
|
|
|
11
11
|
**`Experimental`**
|
|
12
12
|
|