Skip to content

Commit

Permalink
Limit db connections to one per db server & db username (#889) (#928)
Browse files Browse the repository at this point in the history
  • Loading branch information
gzdunek committed Jun 27, 2022
1 parent 42e29e0 commit c2033b8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,16 @@ export function useDatabases() {
targetName: db.name,
targetUser: dbUser,
});
documentsService.add(doc);
documentsService.open(doc.uri);

const connectionToReuse =
appContext.connectionTracker.findConnectionByDocument(doc);

if (connectionToReuse) {
appContext.connectionTracker.activateItem(connectionToReuse.id);
} else {
documentsService.add(doc);
documentsService.open(doc.uri);
}
}

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ limitations under the License.
import { useStore } from 'shared/libs/stores';
import { ImmutableStore } from '../immutableStore';
import { ClustersService } from 'teleterm/ui/services/clusters';
import { WorkspacesService } from 'teleterm/ui/services/workspacesService';
import {
Document,
WorkspacesService,
} from 'teleterm/ui/services/workspacesService';
import { StatePersistenceService } from 'teleterm/ui/services/statePersistence';
import { TrackedConnectionOperationsFactory } from './trackedConnectionOperationsFactory';
import {
Expand Down Expand Up @@ -84,6 +87,19 @@ export class ConnectionTrackerService extends ImmutableStore<ConnectionTrackerSt
activate();
}

findConnectionByDocument(document: Document): TrackedConnection {
switch (document.kind) {
case 'doc.terminal_tsh_node':
return this.state.connections.find(
getServerConnectionByDocument(document)
);
case 'doc.gateway':
return this.state.connections.find(
getGatewayConnectionByDocument(document)
);
}
}

setState(
nextState: (
draftState: ConnectionTrackerState
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function getGatewayConnectionByDocument(document: DocumentGateway) {
return (i: TrackedGatewayConnection) =>
i.kind === 'connection.gateway' &&
i.targetUri === document.targetUri &&
i.port === document.port;
i.targetUser === document.targetUser;
}

export function getServerConnectionByDocument(document: DocumentTshNode) {
Expand All @@ -25,7 +25,7 @@ export function getGatewayDocumentByConnection(
return (i: DocumentGateway) =>
i.kind === 'doc.gateway' &&
i.targetUri === connection.targetUri &&
i.port === connection.port;
i.targetUser === connection.targetUser;
}

export function getServerDocumentByConnection(
Expand Down

0 comments on commit c2033b8

Please sign in to comment.