-
Notifications
You must be signed in to change notification settings - Fork 396
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
WIP: feat: send data by chunk in websocket #3988
Open
bytemain
wants to merge
4
commits into
main
Choose a base branch
from
feat/chunk-ws-payload
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.
Open
Changes from 2 commits
Commits
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
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
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
24 changes: 17 additions & 7 deletions
24
packages/connection/src/common/connection/drivers/ws-websocket.ts
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 |
---|---|---|
@@ -1,24 +1,34 @@ | ||
import { IDisposable } from '@opensumi/ide-core-common'; | ||
|
||
import { chunkSize } from '../../constants'; | ||
|
||
import { BaseConnection } from './base'; | ||
import { LengthFieldBasedFrameDecoder } from './frame-decoder'; | ||
|
||
import type WebSocket from 'ws'; | ||
|
||
export class WSWebSocketConnection extends BaseConnection<Uint8Array> { | ||
protected decoder = new LengthFieldBasedFrameDecoder(); | ||
|
||
constructor(public socket: WebSocket) { | ||
super(); | ||
this.socket.on('message', (data: Buffer) => { | ||
this.decoder.push(data); | ||
}); | ||
bytemain marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
send(data: Uint8Array): void { | ||
this.socket.send(data); | ||
const handle = LengthFieldBasedFrameDecoder.construct(data).dumpAndOwn(); | ||
const packet = handle.get(); | ||
for (let i = 0; i < packet.byteLength; i += chunkSize) { | ||
this.socket.send(packet.subarray(i, i + chunkSize)); | ||
} | ||
|
||
handle.dispose(); | ||
Comment on lines
+21
to
+27
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.
新的 为了进一步优化性能,您可以考虑以下建议:
示例实现: send(data: Uint8Array): void {
const handle = LengthFieldBasedFrameDecoder.construct(data).dumpAndOwn();
const packet = handle.get();
const sendChunk = (start: number) => {
while (start < packet.byteLength && this.socket.bufferedAmount < 1024 * 1024) { // 1MB buffer threshold
const end = Math.min(start + chunkSize, packet.byteLength);
this.socket.send(packet.subarray(start, end));
start = end;
}
if (start < packet.byteLength) {
setTimeout(() => sendChunk(start), 0);
} else {
handle.dispose();
}
};
sendChunk(0);
} 这个实现使用了递归的方式来控制发送速率,避免一次性将所有数据推入缓冲区。 |
||
} | ||
|
||
onMessage(cb: (data: Uint8Array) => void): IDisposable { | ||
this.socket.on('message', cb); | ||
return { | ||
dispose: () => { | ||
this.socket.off('message', cb); | ||
}, | ||
}; | ||
return this.decoder.onData(cb); | ||
} | ||
onceClose(cb: () => void): IDisposable { | ||
this.socket.once('close', cb); | ||
|
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 |
---|---|---|
@@ -1 +1,6 @@ | ||
export const METHOD_NOT_REGISTERED = '$$METHOD_NOT_REGISTERED'; | ||
|
||
/** | ||
* 分片大小, 8MB | ||
*/ | ||
export const chunkSize = 8 * 1024 * 1024; |
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.
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.
监听器管理方式的重大变更
这个变更将之前基于
Emitter
的多监听器方法改为了单一监听器方法。虽然这简化了事件处理机制,但也限制了类只能同时处理一个监听器。考虑以下几点:
建议考虑以下改进:
removeListener
方法,使 API 更加完整和直观。这样的实现既保持了简单性,又提供了更大的灵活性。