Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/Deferred.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { TimeoutError } from './TimeoutError';

interface DeferredOptions {
errorMessage?: string;
}
/**
* Deferred Implementation
*
Expand All @@ -10,8 +13,10 @@ export class Deferred<T> {
protected _resolve: (value: T) => void;
protected _reject: (error: Error) => void;
protected _timeout: NodeJS.Timer;
private options: DeferredOptions;

constructor() {
constructor(options: DeferredOptions = {}) {
this.options = options;
this._promise = new Promise((resolve, reject) => {
this._reject = reject;
this._resolve = resolve;
Expand All @@ -23,7 +28,9 @@ export class Deferred<T> {

this._timeout = setTimeout(() => {
callback();
this.reject(new TimeoutError('Operation timeout'));
this.reject(
new TimeoutError(this.options.errorMessage ?? 'Operation timeout'),
);
}, timeoutInMillis);
}

Expand Down
4 changes: 3 additions & 1 deletion src/Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,9 @@ export class Pool<RawResource> {
);
}

const deferred = new Deferred<RawResource>();
const deferred = new Deferred<RawResource>({
errorMessage: 'Pool acquire operation timeout',
});
deferred.registerTimeout(this.acquireTimeoutMillis, () => {
// timeout triggered, promise will be rejected
// remove this object from pending list
Expand Down
2 changes: 1 addition & 1 deletion test/integration/pool-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ tap.test('pool expands only to max limit', (t) => {
.then((obj) => {
return pool.acquire().catch((e) => {
t.ok(e instanceof TimeoutError);
t.ok(e.message === 'Operation timeout');
t.ok(e.message === 'Pool acquire operation timeout');
pool.release(obj);
t.end();
});
Expand Down