UNPKG

smc-hub

Version:

CoCalc: Backend webserver component

226 lines 10.4 kB
"use strict"; /* * This file is part of CoCalc: Copyright © 2020 Sagemath, Inc. * License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details */ var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); 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 }; } }; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.query = void 0; /* Does the queries to update changefeeds, deduplicating across both all changefeeds and a small interval of time. */ // set to false to completely disable for debugging/testing var THROTTLE = true; // 10ms when running unit tests, still throttle, but make it quick. // Otherwise, we default to 250ms, which is enough to be massively // useful, but also not noticed by user. var THROTTLE_MS = process.env.SMC_TEST ? 10 : 500; // THROTTLE_MS can be overridden the POSTGRES_THROTTLE_CHANGEFEED_MS // environment variable. if (process.env.POSTGRES_THROTTLE_CHANGEFEED_MS != null) { THROTTLE_MS = parseInt(process.env.POSTGRES_THROTTLE_CHANGEFEED_MS); } var events_1 = require("events"); var awaiting_1 = require("awaiting"); var async_utils_1 = require("smc-util/async-utils"); var misc_1 = require("smc-util/misc"); var _a = require("../postgres-base"), one_result = _a.one_result, all_results = _a.all_results; function key(obj) { return "query-" + JSON.stringify(obj); } var ThrottledTableQueue = /** @class */ (function (_super) { __extends(ThrottledTableQueue, _super); function ThrottledTableQueue(db, table, interval_ms) { var _this = _super.call(this) || this; _this.queue = {}; _this.state = "ready"; _this.db = db; _this.table = table; _this.interval_ms = interval_ms; // client listens for results of query -- if queries pile up, and // there are many tables, the default of 10 can easily be exceeded. _this.setMaxListeners(100); return _this; } ThrottledTableQueue.prototype.dbg = function (f) { return this.db._dbg("ThrottledTableQueue('" + this.table + "')." + f); }; ThrottledTableQueue.prototype.close = function () { if (this.state == "closed") { return; } if (this.process_timer != null) { clearTimeout(this.process_timer); } for (var k in this.queue) { this.emit(k, "closed"); } this.emit("closed"); this.removeAllListeners(); misc_1.close(this); this.state = "closed"; }; ThrottledTableQueue.prototype.enqueue = function (query) { if (this.state == "closed") { throw Error("trying to enqueue after close"); } var k = key(query); this.queue[k] = query; if (this.process_timer == null) { this.dbg("enqueue")("will process queue in " + this.interval_ms + "ms..."); this.process_timer = setTimeout(this.process_queue.bind(this), this.interval_ms); } return k; }; ThrottledTableQueue.prototype.process_queue = function () { return __awaiter(this, void 0, void 0, function () { var dbg, queue, _a, _b, _i, k, _c, select, where, result, err_1; return __generator(this, function (_d) { switch (_d.label) { case 0: dbg = this.dbg("process_queue"); delete this.process_timer; // it just fired queue = misc_1.copy(this.queue); this.queue = {}; _a = []; for (_b in queue) _a.push(_b); _i = 0; _d.label = 1; case 1: if (!(_i < _a.length)) return [3 /*break*/, 6]; k = _a[_i]; dbg(k); _c = queue[k], select = _c.select, where = _c.where; _d.label = 2; case 2: _d.trys.push([2, 4, , 5]); return [4 /*yield*/, awaiting_1.callback(one_query, this.db, select, this.table, where)]; case 3: result = _d.sent(); if (this.state == "closed") return [2 /*return*/]; dbg("success", k); this.emit(k, undefined, result); return [3 /*break*/, 5]; case 4: err_1 = _d.sent(); if (this.state == "closed") return [2 /*return*/]; dbg("fail", k); this.emit(k, err_1, undefined); return [3 /*break*/, 5]; case 5: _i++; return [3 /*break*/, 1]; case 6: return [2 /*return*/]; } }); }); }; return ThrottledTableQueue; }(events_1.EventEmitter)); var throttled_table_queues = {}; function throttled_table_queue(db, table, interval_ms) { if (throttled_table_queues[table] != null) { return throttled_table_queues[table]; } return (throttled_table_queues[table] = new ThrottledTableQueue(db, table, interval_ms)); } function query(opts) { return __awaiter(this, void 0, void 0, function () { var Table, k, _a, err, result; return __generator(this, function (_b) { switch (_b.label) { case 0: if (!(THROTTLE && opts.one)) return [3 /*break*/, 2]; Table = throttled_table_queue(opts.db, opts.table, THROTTLE_MS); k = Table.enqueue({ select: opts.select, where: opts.where }); return [4 /*yield*/, async_utils_1.once(Table, k)]; case 1: _a = __read.apply(void 0, [_b.sent(), 2]), err = _a[0], result = _a[1]; if (err != null) { throw err; } return [2 /*return*/, result]; case 2: return [4 /*yield*/, awaiting_1.callback(opts.one ? one_query : all_query, opts.db, opts.select, opts.table, opts.where)]; case 3: return [2 /*return*/, _b.sent()]; } }); }); } exports.query = query; function all_query(db, select, table, where, cb) { db._query({ select: select, table: table, where: where, cb: all_results(cb) }); } function one_query(db, select, table, where, cb) { db._query({ select: select, table: table, where: where, cb: one_result(cb) }); } //# sourceMappingURL=changefeed-query.js.map