Skip to content
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

worker code support Asynchronous injection #2016

Merged
merged 10 commits into from
Jul 25, 2023
Merged

Conversation

deyihu
Copy link
Collaborator

@deyihu deyihu commented Jul 12, 2023

fix #2015
fix #2017

@codecov-commenter
Copy link

codecov-commenter commented Jul 12, 2023

Codecov Report

Merging #2016 (7156cae) into master (7fe20b9) will decrease coverage by 0.01%.
The diff coverage is 84.37%.

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

@@            Coverage Diff             @@
##           master    #2016      +/-   ##
==========================================
- Coverage   83.97%   83.97%   -0.01%     
==========================================
  Files         164      164              
  Lines       17847    17907      +60     
==========================================
+ Hits        14987    15037      +50     
- Misses       2860     2870      +10     
Impacted Files Coverage Δ
src/core/worker/Actor.js 67.90% <77.27%> (+5.60%) ⬆️
src/core/worker/Worker.js 87.75% <82.75%> (-7.49%) ⬇️
src/core/Eventable.js 91.42% <100.00%> (+0.06%) ⬆️
src/core/worker/CoreWorkers.js 100.00% <100.00%> (ø)
src/core/worker/WorkerPool.js 82.45% <100.00%> (+0.63%) ⬆️

... and 2 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@@ -49,7 +50,9 @@ const Eventable = Base =>
if (l > 0) {
for (let i = 0; i < l; i++) {
if (handler === handlerChain[i].handler && handlerChain[i].context === context) {
console.warn(this, `find '${eventsOn}' handler:`, handler, ' The old listener function will be removed');
if (!Browser.isTest) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

方便测试而已,测试环境开了,就不用log,可以让ci跑的更快和输出更加干净

// Dynamic Create Adapter
//利用worker通信向每个workerPool里的每个worker注入新的code
//注意注入的代码在worker code里不是明文的,是个匿名函数挂到adapters,代码层面是看不到改段代码的
export function createAdapter(key, cb) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

利用worker把用户动态注入的code发送到worker里,然后再worker里动态生成adapter,并挂到adpters上

onmessage = function (msg) {
msg = msg.data;
//createAdapter
if (msg.messageType === 'createAdapter') {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果是动态注入worker

@@ -45,6 +47,19 @@ const EMPTY_BUFFERS = [];
export default class Actor {

constructor(workerKey) {
this._delayMessages = [];
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

延迟消息,如果一个worker是动态创建的,在这个过程中,用户可能发送消息


created() {
// handler delay messages
this._delayMessages.forEach(message => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

处理延迟消息

@@ -73,7 +98,11 @@ export default class Actor {
* @param {Function} cb - callback function when received message from worker thread
*/
broadcast(data, buffers, cb) {
cb = cb || function () {};
if (this.initializing) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

当worker动态创建时,拦截消息,并加入到延迟消息队列,等worker创建完成了,在发送到worker里

if (!workerPool) {
return;
}
const workers = workerPool.workers || [];
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

必须是所有的worker,即每个worker里都要注入改adapter

//当第一个Actor初始化完成释放了worker pool里的每个worker资源,后续的Actor的消息通信才会被执行
if (workersHasCreated() && !hasCreated) {
this.initializing = true;
console.log(`Dynamic Create Adapter for worker:${workerKey}`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改成 Injecting codes in worker with worker key: :${workerKey}
另外worker中的adapter存在三种状态: 没有初始化,正在初始化,完成初始化
这里只判断了没有初始化,还应该增加正在初始化的逻辑

@fuzhenn fuzhenn merged commit fbbcc4b into maptalks:master Jul 25, 2023
@deyihu deyihu deleted the 2015 branch July 25, 2023 09:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

路由懒加载导致worker注入出错 worker code support Asynchronous injection
3 participants