Skip to content

Commit

Permalink
OPFS read failure - do not retry
Browse files Browse the repository at this point in the history
  • Loading branch information
kevodwyer committed Dec 14, 2024
1 parent 49ba217 commit 0cb6573
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 27 deletions.
25 changes: 8 additions & 17 deletions assets/js/opfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,26 @@ onmessage = async (e) => {
} else if (message.action =="set") {
setOPFSKV(message.filename, message.value, message.directory, 0);
} else if(message.action =="get") {
getOPFSKV(message.filename, message.directory, 0);
getOPFSKV(message.filename, message.directory);
}
};
function getOPFSKV(filename, directory, retryCount) {
function getOPFSKV(filename, directory) {
getFileHandle(filename, directory).then( fileHandle => {
if (fileHandle != null) {
fileHandle.createSyncAccessHandle({mode: "read-only"}).then(accessHandle => {
const size = accessHandle.getSize();
if (size == 0) {
console.log("OPFS: attempt to read 0 byte data. hash:" + filename);
if (retryCount < 5) {
setTimeout(() => getOPFSKV(filename, directory, retryCount + 1),2000);
} else {
postMessage({filename: filename, contents: null});
}
console.log("OPFS attempt to read 0 byte data. filename:" + filename);
postMessage({filename: filename, contents: null, readFailure: true});
} else {
const dataView = new Int8Array(size);
accessHandle.read(dataView);
accessHandle.close();
postMessage({filename: filename, contents: dataView});
}
}).catch(e => {
if (retryCount < 5) {
setTimeout(() => getOPFSKV(filename, directory, retryCount + 1),2000);
} else {
console.log('getOPFSKV error: ' + e + " filename:" + filename);
postMessage({filename: filename, contents: null});
}
console.log('OPFS error: ' + e + " filename:" + filename);
postMessage({filename: filename, contents: null, readFailure: true});
});
}
}).catch(e => {
Expand All @@ -52,10 +44,9 @@ function setOPFSKV(filename, value, directory, retryCount) {
accessHandle.close();
//console.log('setOPFSKV closing:' + filename);
}).catch(e => {
console.log('setOPFSKV error: ' + e + " filename:" + filename);
if (retryCount < 5) {
setTimeout(() => setOPFSKV(filename, value, directory, retryCount + 1),500);
} else {
console.log('setOPFSKV error: ' + e + " filename:" + filename);
setTimeout(() => setOPFSKV(filename, value, directory, retryCount + 1), 300);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/views/Drive.vue
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ module.exports = {
launcherApp: null,
uploadProgressQueue: { entries:[]},
executingUploadProgressCommands: false,
progressBarUpdateFrequency: 50,
progressBarUpdateFrequency: 30,
zipAndDownloadFoldersCount: 0,
htmlAnchor: "",
previouslyOpenedApp: {path: '', filename: '', app: ''},
Expand Down
31 changes: 22 additions & 9 deletions vendor/priors/gwt.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,23 +392,36 @@ opfsWorker.postMessage({action: 'init'});
opfsWorker.onmessage = function(event) { // reads
let message = event.data;
const pendingFutures = pendingReads.get(message.filename);
pendingReads.delete(message.filename);
if (pendingFutures != null) {
pendingReads.delete(message.filename);
if (message.contents == null) {
pendingFutures.forEach(pendingFuture =>
if (message.readFailure) {
const pending = pendingWrites.get(message.filename);
if (pending != null) {
console.log('getOPFSKV found in cache. filename:' + message.filename);
pendingFutures.forEach(pendingFuture =>
pendingFuture.complete(peergos.client.JsUtil.optionalOf(convertToByteArray(pending)))
);
} else {
console.log('getOPFSKV NOT found in cache. filename:' + message.filename);
pendingFuture.complete(peergos.client.JsUtil.emptyOptional())
);
}
} else {
pendingFutures.forEach(pendingFuture =>
pendingFuture.complete(peergos.client.JsUtil.optionalOf(convertToByteArray(message.contents)))
);
if (message.contents == null) {
pendingFutures.forEach(pendingFuture =>
pendingFuture.complete(peergos.client.JsUtil.emptyOptional())
);
} else {
pendingFutures.forEach(pendingFuture =>
pendingFuture.complete(peergos.client.JsUtil.optionalOf(convertToByteArray(message.contents)))
);
}
}
}
};

function setOPFSKV(filename, value, directory) {
if (!pendingWrites.has(filename)) {
pendingWrites.set(filename, peergos.client.JsUtil.optionalOf(convertToByteArray(value)));
pendingWrites.set(filename, value);
opfsWorker.postMessage({action: 'set', filename: filename, value: value, directory: directory});
setTimeout(() => {
pendingWrites.delete(filename);
Expand All @@ -419,7 +432,7 @@ function getOPFSKV(filename, context, future) {
let directory = context.cacheStore;
const pending = pendingWrites.get(filename);
if (pending != null) {
future.complete(pending);
future.complete(peergos.client.JsUtil.optionalOf(convertToByteArray(pending)));
} else {
if (pendingReads.has(filename)) {
pendingReads.get(filename).push(future);
Expand Down

0 comments on commit 0cb6573

Please sign in to comment.