@resolveio/server-lib 5.0.12 → 5.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,6 +7,8 @@ export declare class SubscriptionManager {
7
7
  private _wss;
8
8
  private _oplogQueue;
9
9
  private _subCache;
10
+ private _nodeCache;
11
+ private _cacheId;
10
12
  private _sendQueue;
11
13
  private _runningQueue;
12
14
  private serverConfig;
@@ -56,6 +56,7 @@ var billing_logged_in_users_collection_1 = require("../collections/billing-logge
56
56
  var moment = require("moment");
57
57
  var billing_logged_in_users_1 = require("../publications/billing-logged-in-users");
58
58
  var report_builder_reports_1 = require("../publications/report-builder-reports");
59
+ var NodeCache = require("node-cache");
59
60
  var SubscriptionManager = /** @class */ (function () {
60
61
  function SubscriptionManager(mainServer, wss, serverConfig) {
61
62
  var _this = this;
@@ -63,9 +64,11 @@ var SubscriptionManager = /** @class */ (function () {
63
64
  this._subscriptions = [];
64
65
  this._oplogQueue = [];
65
66
  this._subCache = [];
67
+ this._cacheId = 0;
66
68
  this._sendQueue = [];
67
69
  this._runningQueue = false;
68
70
  this._mainServer = mainServer;
71
+ this._nodeCache = new NodeCache({ stdTTL: 0, checkperiod: 120 });
69
72
  this.serverConfig = serverConfig;
70
73
  this._wss = wss;
71
74
  // Publications
@@ -87,6 +90,9 @@ var SubscriptionManager = /** @class */ (function () {
87
90
  support_tickets_1.loadSupportTicketPublications(this);
88
91
  this.tailOpLogSupport();
89
92
  }
93
+ setInterval(function () {
94
+ console.log(_this._nodeCache.getStats().vsize);
95
+ }, 60000);
90
96
  setInterval(function () {
91
97
  if (!_this._runningQueue) {
92
98
  if (_this._sendQueue.length) {
@@ -671,6 +677,7 @@ var SubscriptionManager = /** @class */ (function () {
671
677
  var that = this;
672
678
  for (var jj = this._subCache.length - 1; jj >= 0; jj--) {
673
679
  if (this._subCache[jj].collection === collection) {
680
+ this._nodeCache.del(this._subCache[jj].id);
674
681
  this._subCache.splice(jj, 1);
675
682
  }
676
683
  }
@@ -719,6 +726,7 @@ var SubscriptionManager = /** @class */ (function () {
719
726
  }).exec();
720
727
  removed = true;
721
728
  }
729
+ this_1._nodeCache.del(this_1._subCache.filter(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); })[0].id);
722
730
  this_1._subCache.splice(this_1._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }), 1);
723
731
  }
724
732
  if (!!removed) return [3 /*break*/, 2];
@@ -748,6 +756,7 @@ var SubscriptionManager = /** @class */ (function () {
748
756
  }).exec();
749
757
  removed = true;
750
758
  }
759
+ this_1._nodeCache.del(this_1._subCache.filter(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); })[0].id);
751
760
  this_1._subCache.splice(this_1._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }), 1);
752
761
  }
753
762
  if (!removed) {
@@ -792,6 +801,7 @@ var SubscriptionManager = /** @class */ (function () {
792
801
  }).exec();
793
802
  removed = true;
794
803
  }
804
+ this_1._nodeCache.del(this_1._subCache.filter(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); })[0].id);
795
805
  this_1._subCache.splice(this_1._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }), 1);
796
806
  }
797
807
  if (!removed) {
@@ -813,6 +823,7 @@ var SubscriptionManager = /** @class */ (function () {
813
823
  }).exec();
814
824
  removed = true;
815
825
  }
826
+ this_1._nodeCache.del(this_1._subCache.filter(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); })[0].id);
816
827
  this_1._subCache.splice(this_1._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }), 1);
817
828
  }
818
829
  if (!removed) {
@@ -931,7 +942,7 @@ var SubscriptionManager = /** @class */ (function () {
931
942
  serverRes = {
932
943
  messageId: messageId,
933
944
  hasError: false,
934
- data: this._subCache.filter(function (a) { return a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData); })[0].res
945
+ data: this._nodeCache.get(this._subCache.filter(function (a) { return a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData); })[0].id)
935
946
  };
936
947
  this.sendWS(ws, serverRes);
937
948
  }
@@ -940,19 +951,23 @@ var SubscriptionManager = /** @class */ (function () {
940
951
  that._mainServer.getMethodManager().callMethodInternal('insertSubscriptionLog', type, subscription.publication, collection, JSON.stringify(subscription.subscriptionData));
941
952
  }
942
953
  (_a = that._publications[subscription.publication].function).call.apply(_a, [Object.assign({}, that, SubscriptionManager.prototype)].concat(subscription.subscriptionData)).then(function (res) {
943
- if (common_1.getBinarySize(JSON.stringify(_this._subCache)) > 500000000) {
944
- var neededSize = (common_1.getBinarySize(JSON.stringify(_this._subCache))) - 500000000;
954
+ var nodeCacheSize = _this._nodeCache.getStats().vsize;
955
+ if (nodeCacheSize > 500000000) {
945
956
  var neededSizeIndex = 0;
946
957
  for (var zz = 0; zz < _this._subCache.length; zz++) {
947
- neededSize -= (common_1.getBinarySize(JSON.stringify(_this._subCache[zz])));
948
- neededSizeIndex++;
949
- if (neededSize < 0) {
958
+ _this._nodeCache.del(_this._subCache[zz].id);
959
+ if (_this._nodeCache.getStats().vsize < 500000000) {
960
+ neededSizeIndex = zz;
950
961
  break;
951
962
  }
952
963
  }
964
+ // console.log('Cache1: ' + 'Too Big - ' + subscription.publication + ' - ' + getBinarySize(JSON.stringify(this._subCache)));
953
965
  _this._subCache.splice(0, neededSizeIndex);
954
966
  }
955
- _this._subCache.push({ collection: collection, publication: subscription.publication, subscriptionData: subscription.subscriptionData, res: res });
967
+ var cacheId = _this._cacheId++;
968
+ _this._nodeCache.set(cacheId, res);
969
+ _this._subCache.push({ collection: collection, publication: subscription.publication, subscriptionData: subscription.subscriptionData, id: cacheId });
970
+ // console.log('Cache1: ' + 'Create - ' + subscription.publication + ' - ' + getBinarySize(JSON.stringify(this._subCache)));
956
971
  var serverRes = {
957
972
  messageId: messageId,
958
973
  hasError: false,
@@ -1006,8 +1021,9 @@ var SubscriptionManager = /** @class */ (function () {
1006
1021
  var serverRes = {
1007
1022
  messageId: client.messageId,
1008
1023
  hasError: false,
1009
- data: _this._subCache.filter(function (a) { return a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData); })[0].res
1024
+ data: _this._nodeCache.get(_this._subCache.filter(function (a) { return a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData); })[0].id)
1010
1025
  };
1026
+ // console.log('Cache: ' + 'Found - ' + subscription.publication + ' - ' + getBinarySize(JSON.stringify(this._subCache)));
1011
1027
  _this.sendWS(ws, serverRes);
1012
1028
  }
1013
1029
  });
@@ -1018,19 +1034,23 @@ var SubscriptionManager = /** @class */ (function () {
1018
1034
  that._mainServer.getMethodManager().callMethodInternal('insertSubscriptionLog', type, subscription.publication, collection, JSON.stringify(subscription.subscriptionData));
1019
1035
  }
1020
1036
  (_a = that._publications[subscription.publication].function).call.apply(_a, [Object.assign({}, that, SubscriptionManager.prototype)].concat(subscription.subscriptionData)).then(function (res) {
1021
- if (common_1.getBinarySize(JSON.stringify(_this._subCache)) > 500000000) {
1022
- var neededSize = (common_1.getBinarySize(JSON.stringify(_this._subCache))) - 500000000;
1037
+ var nodeCacheSize = _this._nodeCache.getStats().vsize;
1038
+ if (nodeCacheSize > 500000000) {
1023
1039
  var neededSizeIndex = 0;
1024
1040
  for (var zz = 0; zz < _this._subCache.length; zz++) {
1025
- neededSize -= (common_1.getBinarySize(JSON.stringify(_this._subCache[zz])));
1026
- neededSizeIndex++;
1027
- if (neededSize < 0) {
1041
+ _this._nodeCache.del(_this._subCache[zz].id);
1042
+ if (_this._nodeCache.getStats().vsize < 500000000) {
1043
+ neededSizeIndex = zz;
1028
1044
  break;
1029
1045
  }
1030
1046
  }
1047
+ // console.log('Cache: ' + 'Too Big - ' + subscription.publication + ' - ' + getBinarySize(JSON.stringify(this._subCache)));
1031
1048
  _this._subCache.splice(0, neededSizeIndex);
1032
1049
  }
1033
- _this._subCache.push({ collection: collection, publication: subscription.publication, subscriptionData: subscription.subscriptionData, res: res });
1050
+ var cacheId = _this._cacheId++;
1051
+ _this._nodeCache.set(cacheId, res);
1052
+ _this._subCache.push({ collection: collection, publication: subscription.publication, subscriptionData: subscription.subscriptionData, id: cacheId });
1053
+ // console.log('Cache: ' + 'Create - ' + subscription.publication + ' - ' + getBinarySize(JSON.stringify(this._subCache)));
1034
1054
  subscription.clients.forEach(function (client) {
1035
1055
  that._wss.clients.forEach(function (ws) {
1036
1056
  if (ws['id_socket'] === client.id_socket) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@resolveio/server-lib",
3
- "version": "5.0.12",
3
+ "version": "5.0.13",
4
4
  "description": "",
5
5
  "main": "index.ts",
6
6
  "scripts": {
@@ -27,6 +27,7 @@
27
27
  "mongo-oplog": "2.1.4",
28
28
  "mongoose": "5.6.0",
29
29
  "node-google-timezone": "0.1.1",
30
+ "node-cache": "5.0.1",
30
31
  "nodemailer": "6.2.1",
31
32
  "passport": "0.4.0",
32
33
  "passport-local": "1.0.0",