Skip to content

Memory leak when creating multiple instances of api client #657

@misha-erm

Description

@misha-erm

Hello,

Recently we spotted memory leak in our Fibery-Hubspot integration and I suspect that sdk is causing it

Image

After looking at heapsnapshots I see there is a huge amount of functions created via Bottleneck

here are urls to the files. Second snapshot was taken few days after the first one

https://qm5o798ggr.ufs.sh/f/hmIrD5vM1qJoIZx1dOE0S938yOiXZVPJxczgKor5b47WmEjt

https://qm5o798ggr.ufs.sh/f/hmIrD5vM1qJooZYpe9LP02MwkEuIciFQtDZzCBOpofxAWjR5

We create new class instance for every client so rate limit calculation is isolated per workspace

Here is the code I use for client initialisation

import {Account} from '@fibery/connector';
import {Client, DEFAULT_LIMITER_OPTIONS} from '@hubspot/api-client';
import {LRUCache} from 'lru-cache';

const clientsPerAccount = new LRUCache<string, Client>({
  max: 300,
});

export const anonHubspotClient = new Client();

export const getHubspotClient = (account: Account) => {
  let client = clientsPerAccount.get(account.name);

  if (client) {
    if (client.config.accessToken !== account.accessToken) {
      client.setAccessToken(account.accessToken);
    }
  } else {
    client = new Client({
      accessToken: account.accessToken,
      // For OAuth apps, each HubSpot account that installs your app is limited to 100 requests every 10 seconds (TEN_SECONDLY_ROLLING)
      // https://developers.hubspot.com/docs/api/usage-details#rate-limits
      limiterOptions: {
        ...DEFAULT_LIMITER_OPTIONS,
        reservoir: 99,
        reservoirRefreshAmount: 99,
        reservoirRefreshInterval: 10000,
      },
      numberOfApiCallRetries: 4,
    });

    clientsPerAccount.set(account.name, client);
  }

  return client;
};

Thanks in advance for looking at it 🙇🏻 Please let me know if you need more details

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions