Skip to content

Commit

Permalink
fix: avoid accumulating open connections by closing services after 10m
Browse files Browse the repository at this point in the history
  • Loading branch information
avermeil committed Apr 28, 2023
1 parent 4c537ca commit 2cb0b84
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"author": "Opteo",
"license": "MIT",
"dependencies": {
"@isaacs/ttlcache": "^1.2.2",
"google-ads-node": "^11.0.0",
"google-auth-library": "^7.1.0",
"google-gax": "^3.6.0",
Expand Down
22 changes: 18 additions & 4 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
import { getFieldMask, toSnakeCase } from "./utils";
import { googleAdsVersion } from "./version";
import { Hooks } from "./hooks";
import TTLCache from "@isaacs/ttlcache";

// Make sure to update this version number when upgrading
export const FAILURE_KEY = `google.ads.googleads.${googleAdsVersion}.errors.googleadsfailure-bin`;
Expand All @@ -28,11 +29,19 @@ export interface CallHeaders {
"linked-customer-id"?: string;
}

const cache = new TTLCache({
max: 1000,
ttl: 10 * 60 * 1000, // 10 minutes
dispose: async (service: any) => {
// Close connections when they are removed from the cache
await service.close();
},
});

export class Service {
protected readonly clientOptions: ClientOptions;
protected readonly customerOptions: CustomerOptions;
protected readonly hooks: Hooks;
private serviceCache!: Record<ServiceName, AllServices>;

constructor(
clientOptions: ClientOptions,
Expand Down Expand Up @@ -83,18 +92,23 @@ export class Service {
}

protected loadService<T = AllServices>(service: ServiceName): T {
if (this.serviceCache[service]) {
return this.serviceCache[service] as unknown as T;
const serviceCacheKey = `${service}_${this.customerOptions.refresh_token}`;

if (cache.has(serviceCacheKey)) {
return cache.get(serviceCacheKey) as unknown as T;
}

// eslint-disable-next-line @typescript-eslint/no-var-requires
const { [service]: protoService } = require("google-ads-node");
if (typeof protoService === "undefined") {
throw new Error(`Service "${service}" could not be found`);
}

const client = new protoService({
sslCreds: this.getCredentials(),
});
this.serviceCache[service] = client;

cache.set(serviceCacheKey, client);
return client as unknown as T;
}

Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,11 @@
resolved "https://registry.yarnpkg.com/@humanwhocodes/object-schema/-/object-schema-1.2.1.tgz#b520529ec21d8e5945a1851dfd1c32e94e39ff45"
integrity sha512-ZnQMnLV4e7hDlUvw8H+U8ASL02SS2Gn6+9Ac3wGGLIe7+je2AeAOxPY+izIPJDfFDb7eDjev0Us8MO1iFRN8hA==

"@isaacs/ttlcache@^1.2.2":
version "1.2.2"
resolved "https://registry.yarnpkg.com/@isaacs/ttlcache/-/ttlcache-1.2.2.tgz#2c3bc9cbfdbf999046c58a22fd17c0b8e4ffba19"
integrity sha512-UvS9r/yugpqc/uLRdmxfFkaPGfjvVQPjHn3cGFWiT5K1H8o0t3MsRUAB5xs//4dNEmSSi6WpMqVpMjKbsVv82g==

"@istanbuljs/load-nyc-config@^1.0.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz#fd3db1d59ecf7cf121e80650bb86712f9b55eced"
Expand Down

0 comments on commit 2cb0b84

Please sign in to comment.