Code Monkey home page Code Monkey logo

pool's Introduction

Object Pool and Asyncronous Pool

Фабрики и пулы объектов

Асинхронный пул для worker thread pool в Node.js

See: 4-improved.js

Signature poolify(factory, min, norm, max)

  • factory - call to instantiate pool item
  • min - minimum amount to allocate new
  • norm - preallocated amount
  • max - maximum amount to limit pool

pool's People

Contributors

marylynjuana avatar tshemsedinov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

pool's Issues

Infinite loop with exclusive access

To reproduce

Pool from 9-exclusive.js up to b-timeout.js

const pool = new Pool();
pool.add({ item: 1 });
pool.add({ item: 2 });
let x1 = pool.capture();
const x2 = pool.capture();
pool.release(x1);
x1 = pool.capture();
pool.release(x1);
const bottom = pool.capture(); //infinite loop in next(1)

wrong release function in async example

  release(item) {
    const index = this.items.indexOf(item);
    if (index < 0) throw new Error('Pool: release unexpected item');
    if (this.free[index]) throw new Error('Pool: release not captured');
    this.free[index] = true;
    this.available++;
    if (this.queue.length > 0) {
      const { resolve, timer } = this.queue.shift();
      clearTimeout(timer);
      if (resolve) setTimeout(resolve, 0, item);
    }
  }

error - we should not free released item if we resolve it for user in queue

  release(item) {
    const index = this.items.indexOf(item);
    if (index < 0) throw new Error('Pool: release unexpected item');
    if (this.free[index]) throw new Error('Pool: release not captured');
    if (this.queue.length > 0) {
      const { resolve, timer } = this.queue.shift();
      clearTimeout(timer);
      if (resolve) setTimeout(resolve, 0, item);
    } else { 
      this.free[index] = true;
      this.available++;
    }
  }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.