-
Notifications
You must be signed in to change notification settings - Fork 558
improvement(client-container-loader): tighten Signal expectations #25522
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -21,12 +21,39 @@ import { | |
| } from "./protocol/index.js"; | ||
|
|
||
| // ADO: #1986: Start using enum from protocol-base. | ||
| export enum SignalType { | ||
| ClientJoin = "join", // same value as MessageType.ClientJoin, | ||
| ClientLeave = "leave", // same value as MessageType.ClientLeave, | ||
| Clear = "clear", // used only by client for synthetic signals | ||
| export const SignalType = { | ||
| ClientJoin: "join", // same value as MessageType.ClientJoin, | ||
| ClientLeave: "leave", // same value as MessageType.ClientLeave, | ||
| Clear: "clear", // used only by client for synthetic signals | ||
| } as const; | ||
|
|
||
| interface SystemSignalContent { | ||
| type: (typeof SignalType)[keyof typeof SignalType]; | ||
| content?: unknown; | ||
| } | ||
|
|
||
| interface InboundSystemSignal<TSignalContent extends SystemSignalContent> | ||
| extends ISignalMessage<{ type: never; content: TSignalContent }> { | ||
| // eslint-disable-next-line @rushstack/no-new-null -- `null` is used in JSON protocol to indicate system message | ||
| readonly clientId: null; | ||
| } | ||
|
|
||
| type ClientJoinSignal = InboundSystemSignal<{ | ||
| type: typeof SignalType.ClientJoin; | ||
| content: ISignalClient; | ||
| }>; | ||
|
|
||
| type ClientLeaveSignal = InboundSystemSignal<{ | ||
| type: typeof SignalType.ClientLeave; | ||
| content: string; // clientId of leaving client | ||
| }>; | ||
|
|
||
| type ClearClientsSignal = InboundSystemSignal<{ | ||
| type: typeof SignalType.Clear; | ||
| }>; | ||
|
|
||
| type AudienceSignal = ClientJoinSignal | ClientLeaveSignal | ClearClientsSignal; | ||
|
|
||
| /** | ||
| * Function to be used for creating a protocol handler. | ||
| * @legacy @beta | ||
|
|
@@ -47,7 +74,34 @@ export interface IProtocolHandler extends IBaseProtocolHandler { | |
| processSignal(message: ISignalMessage); | ||
| } | ||
|
|
||
| export class ProtocolHandler extends ProtocolOpHandler implements IProtocolHandler { | ||
| /** | ||
| * More specific version of {@link IProtocolHandler} with narrower call | ||
| * constraints for processSignal. | ||
| */ | ||
| export interface ProtocolHandlerInternal extends IProtocolHandler { | ||
| /** | ||
| * Process the audience related signal. | ||
| * @privateRemarks Internally only AudienceSignal messages need handled | ||
|
||
| */ | ||
| processSignal(message: AudienceSignal): void; | ||
| } | ||
|
|
||
| /** | ||
| * Function to be used for creating a protocol handler. | ||
| * | ||
| * @remarks This is the same are {@link ProtocolHandlerBuilder} but | ||
| * returns the {@link ProtocolHandlerInternal} which has narrower | ||
| * expectations for `processSignal`. | ||
| */ | ||
| export type InternalProtocolHandlerBuilder = ( | ||
| attributes: IDocumentAttributes, | ||
| snapshot: IQuorumSnapshot, | ||
| // TODO: use a real type (breaking change) | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| sendProposal: (key: string, value: any) => number, | ||
| ) => ProtocolHandlerInternal; | ||
|
|
||
| export class ProtocolHandler extends ProtocolOpHandler implements ProtocolHandlerInternal { | ||
| constructor( | ||
| attributes: IDocumentAttributes, | ||
| quorumSnapshot: IQuorumSnapshot, | ||
|
|
@@ -104,8 +158,8 @@ export class ProtocolHandler extends ProtocolOpHandler implements IProtocolHandl | |
| return super.processMessage(message, local); | ||
| } | ||
|
|
||
| public processSignal(message: ISignalMessage): void { | ||
| const innerContent = message.content as { content: unknown; type: string }; | ||
| public processSignal(message: AudienceSignal): void { | ||
| const innerContent = message.content; | ||
| switch (innerContent.type) { | ||
| case SignalType.Clear: { | ||
| const members = this.audience.getMembers(); | ||
|
|
@@ -117,15 +171,15 @@ export class ProtocolHandler extends ProtocolOpHandler implements IProtocolHandl | |
| break; | ||
| } | ||
| case SignalType.ClientJoin: { | ||
| const newClient = innerContent.content as ISignalClient; | ||
| const newClient = innerContent.content; | ||
| // Ignore write clients - quorum will control such clients. | ||
| if (newClient.client.mode === "read") { | ||
| this.audience.addMember(newClient.clientId, newClient.client); | ||
| } | ||
| break; | ||
| } | ||
| case SignalType.ClientLeave: { | ||
| const leftClientId = innerContent.content as string; | ||
| const leftClientId = innerContent.content; | ||
| // Ignore write clients - quorum will control such clients. | ||
| if (this.audience.getMember(leftClientId)?.mode === "read") { | ||
| this.audience.removeMember(leftClientId); | ||
|
|
@@ -144,7 +198,9 @@ export class ProtocolHandler extends ProtocolOpHandler implements IProtocolHandl | |
| * The protocol handler should strictly handle only ClientJoin, ClientLeave | ||
| * and Clear signal types. | ||
| */ | ||
| export function protocolHandlerShouldProcessSignal(message: ISignalMessage): boolean { | ||
| export function protocolHandlerShouldProcessSignal( | ||
| message: ISignalMessage, | ||
| ): message is AudienceSignal { | ||
| // Signal originates from server | ||
| if (message.clientId === null) { | ||
| const innerContent = message.content as { content: unknown; type: string }; | ||
|
|
||
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.
Use string literals for assert error messages instead of generic descriptions. Replace 'Unexpected type in ISignalMessage' and 'Non-string content in ISignalMessage' with more descriptive string literals.