MCPcopy
hub / github.com/mongodb/node-mongodb-native / withLock

Function withLock

src/cmap/auth/mongodb_oidc/callback_workflow.ts:156–176  ·  view source on GitHub ↗

* Ensure the callback is only executed one at a time and throttles the calls * to every 100ms.

(callback: OIDCCallbackFunction)

Source from the content-addressed store, hash-verified

154 * to every 100ms.
155 */
156 protected withLock(callback: OIDCCallbackFunction): OIDCCallbackFunction {
157 let lock: Promise<any> = Promise.resolve();
158 return async (params: OIDCCallbackParams): Promise<OIDCResponse> => {
159 // We do this to ensure that we would never return the result of the
160 // previous lock, only the current callback's value would get returned.
161 await lock;
162 lock = lock
163
164 .catch(() => null)
165
166 .then(async () => {
167 const difference = Date.now() - this.lastExecutionTime;
168 if (difference <= THROTTLE_MS) {
169 await setTimeout(THROTTLE_MS - difference, { signal: params.timeoutContext });
170 }
171 this.lastExecutionTime = Date.now();
172 return await callback(params);
173 });
174 return await lock;
175 };
176 }
177}
178
179/**

Callers

nothing calls this directly

Calls 1

callbackFunction · 0.50

Tested by

no test coverage detected