UNPKG

node-beanstalk

Version:

The most comprehensive beanstalk client for nodejs

221 lines 10 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Pool = void 0; var const_1 = require("./const"); var PoolClient_1 = require("./PoolClient"); var LinkedList_1 = require("./util/LinkedList"); var PoolError_1 = require("./error/PoolError"); var Pool = /** @class */ (function () { function Pool(options) { if (options === void 0) { options = {}; } var _this = this; this._clients = []; this._idleClients = new LinkedList_1.LinkedList(); this._pendingQueue = new LinkedList_1.LinkedList(); this._state = 'live'; this.handleClientRelease = function (client) { if (_this._state !== 'live') return; var pending = _this._pendingQueue.unshift(); if (pending) { pending.resolve(client); } else { _this._idleClients.push(client); } }; this._opt = __assign(__assign({}, const_1.DEFAULT_POOL_OPTIONS), options); } Object.defineProperty(Pool.prototype, "capacity", { /** * Total capacity of the pool. */ get: function () { return this._opt.capacity; }, enumerable: false, configurable: true }); Object.defineProperty(Pool.prototype, "idleCount", { /** * Amount of clients which are not reserved and currently idle in the pool. */ get: function () { return this._idleClients.size; }, enumerable: false, configurable: true }); Object.defineProperty(Pool.prototype, "waitingCount", { /** * Total amount of queued client requests when all clients are reserved. It is helpful to monitor * this number to see if you need to adjust the size of the pool. */ get: function () { return this._pendingQueue.size; }, enumerable: false, configurable: true }); /** * Current pool state. */ Pool.prototype.getState = function () { return this._state; }; /** * Reserve a client from the pool. * * If the pool is full and all clients are currently reserved, this will wait in a FIFO queue * until a client becomes available by it being released back to the pool. * * If there are idle clients in the pool it will be returned. * * If the pool is not full a new client will be created and connected. */ Pool.prototype.connect = function () { var _a; return __awaiter(this, void 0, void 0, function () { var client, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: if (this._state !== 'live') { throw new PoolError_1.PoolError("Unable to gain client, pool is not live: ".concat(this._state)); } if (!(this._clients.length < this._opt.capacity)) return [3 /*break*/, 2]; client = new PoolClient_1.PoolClient(this._opt.clientOptions); this._clients.push(client); return [4 /*yield*/, client.connect()]; case 1: _c.sent(); return [3 /*break*/, 6]; case 2: if (!((_a = this._idleClients.unshift()) !== null && _a !== void 0)) return [3 /*break*/, 3]; _b = _a; return [3 /*break*/, 5]; case 3: return [4 /*yield*/, this.createPendingPromise()]; case 4: _b = (_c.sent()); _c.label = 5; case 5: client = _b; _c.label = 6; case 6: client.once('release', this.handleClientRelease); return [2 /*return*/, client]; } }); }); }; /** * Disconnect all clients from server after all pending requests performed. * All currently reserved clients will not be returned to the pool and ended in-place after all * pending requests performed. * * If {force} set to truthy value - clients pending requests will not be awaited. */ Pool.prototype.disconnect = function (force) { if (force === void 0) { force = false; } return __awaiter(this, void 0, void 0, function () { var _i, _a, reject; return __generator(this, function (_b) { switch (_b.label) { case 0: if (this._state !== 'live') { throw new PoolError_1.PoolError("Unable to disconnect pool that is not live, current state: ".concat(this._state)); } if (!this._pendingQueue.size) return [3 /*break*/, 2]; if (!!force) return [3 /*break*/, 2]; // in case non-forced disconnect - we wait in queue return [4 /*yield*/, this.createPendingPromise()]; case 1: // in case non-forced disconnect - we wait in queue _b.sent(); _b.label = 2; case 2: this._state = 'disconnecting'; // reject all pending queue // eslint-disable-next-line no-restricted-syntax for (_i = 0, _a = this._pendingQueue.truncate(); _i < _a.length; _i++) { reject = _a[_i].reject; reject(new PoolError_1.PoolError('Unable to gain client, pool is disconnecting.')); } this._idleClients.truncate(); // disconnect all existing clients return [4 /*yield*/, Promise.allSettled(this._clients.splice(0, this._clients.length).map(function (client) { return client.disconnect(force); }))]; case 3: // disconnect all existing clients _b.sent(); this._state = 'disconnected'; return [2 /*return*/]; } }); }); }; /** * Restore pool from disconnected state. */ Pool.prototype.restore = function () { if (this._state !== 'disconnected') { throw new PoolError_1.PoolError("Unable to restore pool that was not disconnected, current state: ".concat(this._state)); } this._state = 'live'; }; Pool.prototype.createPendingPromise = function () { var _this = this; return new Promise(function (resolve, reject) { _this._pendingQueue.push({ resolve: resolve, reject: reject }); }); }; return Pool; }()); exports.Pool = Pool; //# sourceMappingURL=Pool.js.map