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

Fix messagingSession infinite reconnect loop on error and also add backwards compatibility with AWS JS SDK V2 for getMessagingSessionEndpoint #2444

Merged
merged 1 commit into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- `MessagingSession` reconnect loop did not break on error past reconnect deadline. Infinite reconnect loop was caused due to `firstConnectionAttemptTimestamp` not being set as `startedConnectionAttempt` was not invoked. Check https://github.com/aws/amazon-chime-sdk-js/issues/2372 for details.
- `MessagingSession` `getMessagingSessionEndpoint` call is now backwards compatible with AWS JS SDK v2.
- Use a default "playback" `latencyHint` when creating the `AudioContext` on Windows. Also adds a `setDefaultLatencyHint` API to `DefaultDeviceController` to allow for overriding.

## [3.7.0] - 2022-07-05
Expand Down
24 changes: 22 additions & 2 deletions src/messagingsession/DefaultMessagingSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,32 @@ export default class DefaultMessagingSession implements MessagingSession {

private async startConnecting(reconnecting: boolean): Promise<void> {
let endpointUrl = this.configuration.endpointUrl;

// Moving this reconnect logic can potentially result into an infinite reconnect loop on errors.
// Check https://github.com/aws/amazon-chime-sdk-js/issues/2372 for details.
if (!reconnecting) {
this.reconnectController.reset();
}
if (this.reconnectController.hasStartedConnectionAttempt()) {
this.reconnectController.startedConnectionAttempt(false);
} else {
this.reconnectController.startedConnectionAttempt(true);
}
// reconnect needs to re-resolve endpoint url, which will also refresh credentials on client if they are expired
if (reconnecting || endpointUrl === undefined) {
try {
if (this.configuration.chimeClient.getMessagingSessionEndpoint instanceof Function) {
endpointUrl = (await this.configuration.chimeClient.getMessagingSessionEndpoint())
.Endpoint.Url;
const response = await this.configuration.chimeClient.getMessagingSessionEndpoint();
// Check for aws sdk v3 with v2 style compatibility first
if (response.Endpoint?.Url) {
endpointUrl = response.Endpoint.Url;
} else {
// Make aws sdk v2 call
const endpoint = await this.configuration.chimeClient
.getMessagingSessionEndpoint()
.promise();
endpointUrl = endpoint.Endpoint.Url;
}
} else {
endpointUrl = (
await this.configuration.chimeClient.send(new GetMessagingSessionEndpointCommand({}))
Expand Down
49 changes: 44 additions & 5 deletions test/messagingsession/DefaultMessagingSession.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('DefaultMessagingSession', () => {
},
};

const v2ChimeClient = {
const v3ChimeClientV2style = {
config: {
region: 'us-east-1',
credentials: {
Expand All @@ -85,6 +85,30 @@ describe('DefaultMessagingSession', () => {
},
};

const v2ChimeClient = {
config: {
region: 'us-east-1',
credentials: {
accessKeyId: 'accessKey',
secretAccessKey: 'secretKey',
sessionToken: 'sessionToken',
},
},
getMessagingSessionEndpoint: function () {
return {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
promise: async function (): Promise<any> {
getMessSessionCnt++;
return {
Endpoint: {
Url: ENDPOINT_URL,
},
};
},
};
},
};

class TestSigV4 implements SigV4 {
lastSignedQueryParams: Map<string, string[]>;

Expand Down Expand Up @@ -170,6 +194,21 @@ describe('DefaultMessagingSession', () => {
});
});

it('Can start with v3 client with v2 style', done => {
configuration.chimeClient = v3ChimeClientV2style;
messagingSession.addObserver({
messagingSessionDidStart(): void {
expect(getMessSessionCnt).to.be.eq(1);
done();
},
});
messagingSession.start().then(() => {
new TimeoutScheduler(10).start(() => {
webSocket.send(SESSION_SUBSCRIBED_MSG);
});
});
});

it('Can start with v2 client', done => {
configuration.chimeClient = v2ChimeClient;
messagingSession.addObserver({
Expand Down Expand Up @@ -299,21 +338,21 @@ describe('DefaultMessagingSession', () => {
});

it('can reconnect with failures on getMessagingSession', done => {
configuration.chimeClient = v2ChimeClient;
configuration.chimeClient = v3ChimeClientV2style;
let didStartCount = 0;
let didStartConnecting = 0;
const savedClientBehavior = v2ChimeClient.getMessagingSessionEndpoint;
const savedClientBehavior = v3ChimeClientV2style.getMessagingSessionEndpoint;
messagingSession.addObserver({
messagingSessionDidStartConnecting(reconnecting: boolean): void {
didStartConnecting++;
if (!reconnecting) {
webSocket.addEventListener('open', () => {
webSocket.close(1006);
v2ChimeClient.getMessagingSessionEndpoint = function () {
v3ChimeClientV2style.getMessagingSessionEndpoint = function () {
throw 'some error';
};
setTimeout(function () {
v2ChimeClient.getMessagingSessionEndpoint = savedClientBehavior;
v3ChimeClientV2style.getMessagingSessionEndpoint = savedClientBehavior;
}, 100);
});
} else {
Expand Down