-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ec2): try to dispose SSM session state #5616
## Problem The remote connection through VSCode does not terminate the SSM session on close consistently. Note: We do not get an event when the remote window is closed. ## Solution Leverage two best-effort approaches: - only allow a single connection from the toolkit to any given EC2 Instance. If a customer attempts to open another remote window in an EC2 instance, we can use that as a sign to terminate the old session. - on toolkit shutdown (deactivate), remote any sessions that are still running. ## Implementation Details - Implement `Ec2RemoteEnvManager` to manage the remote environments. Behaves like a map of instance ids to sessions ids that most importantly maintains the invariant that any deleted item has its session terminated. - Refactor `packages/core/src/awsService/ec2/commands.ts` and `packages/core/src/awsService/ec2/activation.ts` to allow for state tracking in `EC2ConnectionManager`. This change also gives us an opportunity to improve the testing infrastructure for this code. --- Co-authored-by: JadenSimon <[email protected]> Co-authored-by: Justin M. Keyes <[email protected]> Co-authored-by: Weinstock <[email protected]>
- Loading branch information
1 parent
7dabb7c
commit 142761e
Showing
17 changed files
with
372 additions
and
61 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/*! | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { getLogger } from '../../shared' | ||
import { Ec2Connecter } from './model' | ||
|
||
export class Ec2ConnecterMap extends Map<string, Ec2Connecter> { | ||
private static warnSize: number = 25 | ||
|
||
public getOrInit(regionCode: string) { | ||
return this.has(regionCode) ? this.get(regionCode)! : this.initManager(regionCode) | ||
} | ||
|
||
private initManager(regionCode: string): Ec2Connecter { | ||
if (this.size >= Ec2ConnecterMap.warnSize) { | ||
getLogger().warn( | ||
`Connection manager exceeded threshold of ${Ec2ConnecterMap.warnSize} with ${this.size} active connections` | ||
) | ||
} | ||
const newConnectionManager = new Ec2Connecter(regionCode) | ||
this.set(regionCode, newConnectionManager) | ||
return newConnectionManager | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/*! | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { EC2, SSM } from 'aws-sdk' | ||
import { SsmClient } from '../../shared/clients/ssmClient' | ||
import { Disposable } from 'vscode' | ||
|
||
export class Ec2SessionTracker extends Map<EC2.InstanceId, SSM.SessionId> implements Disposable { | ||
public constructor( | ||
readonly regionCode: string, | ||
protected ssmClient: SsmClient | ||
) { | ||
super() | ||
} | ||
|
||
public async addSession(instanceId: EC2.InstanceId, sessionId: SSM.SessionId): Promise<void> { | ||
if (this.isConnectedTo(instanceId)) { | ||
const existingSessionId = this.get(instanceId)! | ||
await this.ssmClient.terminateSessionFromId(existingSessionId) | ||
this.set(instanceId, sessionId) | ||
} else { | ||
this.set(instanceId, sessionId) | ||
} | ||
} | ||
|
||
private async disconnectEnv(instanceId: EC2.InstanceId): Promise<void> { | ||
await this.ssmClient.terminateSessionFromId(this.get(instanceId)!) | ||
this.delete(instanceId) | ||
} | ||
|
||
public async dispose(): Promise<void> { | ||
this.forEach(async (_sessionId, instanceId) => await this.disconnectEnv(instanceId)) | ||
} | ||
|
||
public isConnectedTo(instanceId: EC2.InstanceId): boolean { | ||
return this.has(instanceId) | ||
} | ||
} |
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.