Skip to content

Commit

Permalink
UBERF-6242: More proper manage mongo connections
Browse files Browse the repository at this point in the history
Signed-off-by: Andrey Sobolev <[email protected]>
  • Loading branch information
haiodo committed Apr 1, 2024
1 parent 75a3d84 commit b9c9771
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 37 deletions.
34 changes: 4 additions & 30 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,5 @@
# Contribution checklist
**Pull Request Requirements:**

## Brief description

## Checklist

* [ ] - UI test added to added/changed functionality?
* [ ] - Screenshot is added to PR if applicable ?
* [ ] - Does a local formatting is applied (rush format)
* [ ] - Does a local svelte-check is applied (rush svelte-check)
* [ ] - Does a local UI tests are executed [UI Testing](../tests/readme.md)
* [ ] - Does the code work? Check whether function and logic are correct.
* [ ] - Does Changelog.md is updated with changes?
* [ ] - Does the translations are up to date?
* [ ] - Does it well tested?
* [ ] - Tested for Chrome.
* [ ] - Tested for Safari.
* [ ] - Go through the changed code looking for typos, TODOs, commented LOCs, debugging pieces of code, etc.
* [ ] - Rebase your branch onto master and upstream branch
* [ ] - Is there any redundant or duplicate code?
* [ ] - Are required links are linked to PR?
* [ ] - Does new code is well documented ?

## Related issues

A list of closed updated issues

## Contributor requirements

* [ ] - Sign-off is provided. [DCO](https://github.com/apps/dco)
* [ ] - GPG Signature is provided. [GPG](https://docs.github.com/en/authentication/managing-commit-signature-verification/signing-commits)
- Provide a brief description of the changeset.
- Include a screenshots if applicable
- Ensure that the changeset adheres to the [DCO guidelines](https://github.com/apps/dco).
2 changes: 1 addition & 1 deletion server/mongo/src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ abstract class MongoAdapterBase implements DbAdapter {
}

async close (): Promise<void> {
await this.client.close()
this.client.close()
}

private translateQuery<T extends Doc>(clazz: Ref<Class<T>>, query: DocumentQuery<T>): Filter<Document> {
Expand Down
21 changes: 15 additions & 6 deletions server/mongo/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ process.on('exit', () => {
*/
export async function shutdown (): Promise<void> {
for (const c of connections.values()) {
await c.close(true)
c.close(true)
}
connections.clear()
}
Expand All @@ -39,7 +39,10 @@ export class MongoClientReference {
count: number
client: MongoClient | Promise<MongoClient>

constructor (client: MongoClient | Promise<MongoClient>) {
constructor (
client: MongoClient | Promise<MongoClient>,
readonly onclose: () => void
) {
this.count = 1
this.client = client
}
Expand All @@ -51,13 +54,16 @@ export class MongoClientReference {
return this.client
}

async close (force: boolean = false): Promise<void> {
close (force: boolean = false): void {
this.count--
if (this.count === 0 || force) {
if (force) {
this.count = 0
}
await (await this.client).close()
this.onclose()
void (async () => {
await (await this.client).close()
})()
}
}

Expand All @@ -76,14 +82,17 @@ export function getMongoClient (uri: string, options?: MongoClientOptions): Mong
let existing = connections.get(key)

// If not created or closed
if (existing === undefined || existing.count === 0) {
if (existing === undefined) {
existing = new MongoClientReference(
MongoClient.connect(uri, {
...options,
enableUtf8Validation: false,
maxConnecting: 1024,
...extraOptions
})
}),
() => {
connections.delete(key)
}
)
connections.set(key, existing)
} else {
Expand Down

0 comments on commit b9c9771

Please sign in to comment.