StarpTech / apollo-datasource-http

Optimized JSON HTTP Data Source for Apollo Server
MIT License
73 stars 32 forks source link

Connection pool cache #54

Open javi11 opened 2 years ago

javi11 commented 2 years ago

First of all, nice work with the library ;).

I encountered a case where I have a lot of open subscriptions and for each subscriptor I have a new Datasources instance. So in this case there is a connection pool for subscriptor with some memory usage let's say 2 mb. That memory is multiplying for every subscriptor. Summarising the connection pool is not reused for same hosts with the implementation that is currently putted on place.

Do you think it will make sense to do a connection pool cache?

In my service I end up doing something like this:

const poolCache = new Map<string, Pool>()

class HTTPDataSource extends ApolloHTTPDataSource<Context> {
  public globalOptions?: HTTPDataSourceOptions
  constructor(baseUrl: string, options: HTTPDataSourceOptions = {}) {
    if (!options.pool) {
      options.pool = poolCache.get(baseUrl)
    }
    if (!options.pool || options.pool.closed || options.pool.destroyed) {
      options.pool = new Pool(baseUrl, options?.clientOptions)
      poolCache.set(baseUrl, options.pool)
    }
    super(baseUrl, options)
  }