Skip to content

Commit

Permalink
Just use use and done and done't do timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed Jan 21, 2024
1 parent 039d188 commit 1afdf49
Showing 1 changed file with 3 additions and 34 deletions.
37 changes: 3 additions & 34 deletions read.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ export function addReadMethods(LMDBStore, {
cursor = new Cursor(db, txnAddress || 0);
}
cursorAddress = cursor.address;
txn_handle = txn.use(2000);
if (txn.use) txn.use(); // track transaction so we always use the same one
if (snapshot === false) {
cursorRenewId = renewId; // use shared read transaction
txn.renewingRefCount = (txn.renewingRefCount || 0) + 1; // need to know how many are renewing cursors
Expand Down Expand Up @@ -496,7 +496,7 @@ export function addReadMethods(LMDBStore, {
iterable.onDone()
if (cursorRenewId)
txn.renewingRefCount--;
txn_handle.done();
txn.done?.();
if (txn.refCount <= 0 && txn.notCurrent) {
cursor.close();
} else {
Expand Down Expand Up @@ -832,39 +832,8 @@ Txn.prototype.done = function() {
} else if (this.refCount < 0)
throw new Error('Can not finish a transaction more times than it was used');
}
Txn.prototype.use = function(timeout) {
Txn.prototype.use = function() {
this.refCount = (this.refCount || 0) + 1;
if (timeout) {
let timed_out;
let handle;
(async () => {
let timer
await new Promise(async (resolve, reject) => {
timer = setTimeout(() => {
timed_out = true;
resolve();
}, timeout).unref();
handle = {
done: () => {
// clear our timeout timer
clearTimeout(timer);
this.done();
resolve();
}
}
});
if (timed_out) {
try {
throw new Error('Transaction took too long');
} catch(error) {
// this should print with a proper stack trace
console.error(error);
}
}
})();
return handle;
}
return this;
}


Expand Down

0 comments on commit 1afdf49

Please sign in to comment.