Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to delete a resource from the pool? #241

Open
PertapaCode opened this issue Oct 18, 2018 · 2 comments
Open

How to delete a resource from the pool? #241

PertapaCode opened this issue Oct 18, 2018 · 2 comments
Assignees

Comments

@PertapaCode
Copy link

I'm using node-pool to handle multiple socket connections, when the connection goes down I need to be able to delete the socket resource from the pool, but looks like node-pool lacks this API.

I know pool.destroy will destroy the resource but this just works for previously borrowed resources which is not the case.

Is there any way to delete a resource from the pool that has not been previously borrowed?

@sandfox sandfox self-assigned this Oct 18, 2018
@sandfox
Copy link
Collaborator

sandfox commented Oct 18, 2018

The only way out-of-the-box I think to do with would be to create a custom evictor that periodically checks the status of each connection in the pool and deletes anything that fails whatever check you specify.

As long as you create an Evictor class that has an instance method of evictthat accepts (poolConfig, resourceToCheck, availableObjectsCount) and returns a boolean, false to keep the resource and true to destroy the resource, it should be ok.

see https://github.com/coopernurse/node-pool/blob/master/lib/DefaultEvictor.js

const { Pool, Deque, PriorityQueue } = require("generic-pool")

class MyEvictor {
  evict: (config, resource, availableObjectsCount) {
    return resource.conn_status != "alive"
  }
}

const createPool = (factory, config) => {
  return new Pool(MyEvictor, Deque, PriorityQueue, factory, config);
}

// usual generic-pool style usage

const myPool = createPool({/* factory object */, /* config */}

The evictor is however limited to synchronously testing functions which may be too restrictive for you?

@fbretel-cn
Copy link

@PertapaCode not sure it applies to your use case, but I could solve mine using in the factory's validate():

const validator = instance => Promise.resolve(true)

const factory = {
  create: () => puppeteer.launch({...puppeteerLaunchArgs}).then(instance => {
    instance.disconnectionCount = 0;
    instance.on('disconnected', async () => {
      console.error(`Browser instance disconnected: ${instance.wsEndpoint()}`);
      instance.disconnectionCount += 1;
    });
    return instance;
  }),
  destroy: (instance) => {
    instance.close();
  },
  validate: (instance) => {
    return validator(instance)
      .then(valid => Promise.resolve(valid && instance.disconnectionCount === 0));
  },
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants