-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/workers optimised #256
Open
ivanovSPvirtru
wants to merge
9
commits into
main
Choose a base branch
from
feature/workers_unoptimised
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+275
−47
Open
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1b6ccaf
No pause between download
ivanovSPvirtru 03e9a73
working but 20s for decrypt
ivanovSPvirtru 4334c09
worker optimised
ivanovSPvirtru 591a62e
Merge branch 'main' into feature/workers_unoptimised
ivanovSPvirtru c9f28a7
worker optimised and bug fixed
ivanovSPvirtru a10b559
Optimized worker decrypt
ivanovSPvirtru 223e9a0
🤖 🎨 Autoformat
ivanovSPvirtru 96b88fb
test fixed
ivanovSPvirtru bdc344b
test added
ivanovSPvirtru File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import('./dist/web/tests/mocha/setup.js').catch(e => console.error(e)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { TdfDecryptError } from '../../../src/errors.js'; | ||
|
||
const maxWorkers = navigator?.hardwareConcurrency || 4; // save fallback number | ||
|
||
interface DecryptData { | ||
key: CryptoKey; | ||
encryptedPayload: ArrayBuffer; | ||
algo: AesCbcParams | AesGcmParams; | ||
} | ||
|
||
const workerScript = ` | ||
self.onmessage = async (event) => { | ||
const { key, encryptedPayload, algo } = event.data; | ||
|
||
try { | ||
const decryptedData = await crypto.subtle.decrypt(algo, key, encryptedPayload); | ||
self.postMessage({ success: true, data: decryptedData }); | ||
} catch (error) { | ||
self.postMessage({ success: false, error: error.message }); | ||
} | ||
}; | ||
`; | ||
|
||
const workerBlob = new Blob([workerScript], { type: 'application/javascript' }); | ||
const workerUrl = URL.createObjectURL(workerBlob); | ||
const workersArray: Worker[] = Array.from({ length: maxWorkers }, () => new Worker(workerUrl)); | ||
|
||
interface WorkersQueue { | ||
freeWorkers: Worker[]; | ||
resolvers: ((worker: Worker) => void)[]; | ||
push: (worker: Worker) => void; | ||
pop: () => Promise<Worker>; | ||
} | ||
|
||
const workersQueue: WorkersQueue = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add some test coverage for this? |
||
freeWorkers: [...workersArray], | ||
resolvers: [], | ||
|
||
push(worker: Worker) { | ||
const resolve = this.resolvers.shift(); | ||
if (typeof resolve === 'function') { | ||
resolve(worker); | ||
} else { | ||
this.freeWorkers.push(worker); | ||
} | ||
}, | ||
|
||
pop(): Promise<Worker> { | ||
return new Promise((resolve) => { | ||
const worker = this.freeWorkers.shift(); | ||
if (worker instanceof Worker) { | ||
resolve(worker); | ||
} else { | ||
this.resolvers.push(resolve); | ||
} | ||
}); | ||
}, | ||
}; | ||
|
||
export async function decrypt(data: DecryptData): Promise<ArrayBuffer> { | ||
const worker: Worker = await workersQueue.pop(); | ||
return await new Promise((resolve, reject) => { | ||
worker.onmessage = (event) => { | ||
const { success, data, error } = event.data; | ||
workersQueue.push(worker); | ||
if (success) { | ||
resolve(data as ArrayBuffer); | ||
} else { | ||
reject(new TdfDecryptError(error)); | ||
} | ||
}; | ||
|
||
worker.postMessage(data); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally we'd be doing this at build time, not runtime. Do we have any methods for doing this? I can't find a good way to do this without doing a two-pass build.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code gets executate in the instance when we import entry point of sdk to any frontend app. Because this file eventually gets imported from entry point further down the nodes. And imports are synchronous. ANd code above too.
I dont think it could be initiated earlier that that in js world tbh
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally the bundler will do all this work for you can you can treat this like a dynamic import almost. It looks like both vitejs and webpack 5 support this:
https://vitejs.dev/guide/features.html#web-workers
https://webpack.js.org/guides/web-workers/
I guess the question is are they smart enough to work through all the intermediate translations we do. Most notably, virtru-sdk uses webpack5 to bundle, and then secure-lib.js loads that (webpack 5 again) as does secure-share (webpack 5 as well), but our only test library here uses vite.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yes i tried it and i had constant problems on a app side. Ive tuned it on Virtru-sdk side but after webpack on secure share it had constant errors. Spend a wile on that.
Thats why i decided to go with this aproach. Its makes sdk independent of bundlers issues and doesnt slow down performance, since its a singleton instance that initializes workers as soon as sdk imported it. So when youll start download stuff its already be there.