@routr/registry 2.0.5-alpha.21 → 2.0.5-alpha.23

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.
@@ -18,10 +18,6 @@ export declare const schema: {
18
18
  description: string;
19
19
  type: string;
20
20
  properties: {
21
- bindAddr: {
22
- description: string;
23
- type: string;
24
- };
25
21
  requesterAddr: {
26
22
  description: string;
27
23
  type: string;
@@ -39,10 +39,6 @@ exports.schema = {
39
39
  description: "Operations spec for Registry",
40
40
  type: "object",
41
41
  properties: {
42
- bindAddr: {
43
- description: "Ipv4 interface to accept request on",
44
- type: "string"
45
- },
46
42
  requesterAddr: {
47
43
  description: "Address of service to send requests to",
48
44
  type: "string"
@@ -3,7 +3,7 @@ import { IRegistryStore, RegistrationEntry } from "./types";
3
3
  * In-memory store for the registry service.
4
4
  */
5
5
  export default class MemoryStore implements IRegistryStore {
6
- private collections;
6
+ private map;
7
7
  private cleanupCount;
8
8
  private maxCyclesBeforeCleanup;
9
9
  /**
@@ -1,5 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ const notExpired = (entry) => {
4
+ const timeElapsed = (Date.now() - entry.timeOfEntry) / 1000;
5
+ return timeElapsed <= entry.retentionTimeInSeconds;
6
+ };
3
7
  const MAX_CYCLES_BEFORE_CLEANUP = 10000;
4
8
  /**
5
9
  * In-memory store for the registry service.
@@ -11,40 +15,44 @@ class MemoryStore {
11
15
  * @param {number} maxCyclesBeforeCleanup - Maximum number of cycles before cleanup
12
16
  */
13
17
  constructor(maxCyclesBeforeCleanup = MAX_CYCLES_BEFORE_CLEANUP) {
14
- this.collections = new Map();
18
+ this.map = new Map();
15
19
  this.maxCyclesBeforeCleanup = maxCyclesBeforeCleanup;
16
20
  }
17
21
  /** @inheritdoc */
18
22
  put(key, entry) {
19
- this.collections.set(key, entry);
23
+ this.map.set(key, entry);
24
+ // Cleanup every so often to avoid memory build up
25
+ this.cleanupCount++;
26
+ if (this.cleanupCount >= this.maxCyclesBeforeCleanup) {
27
+ this.cleanupCount = 0;
28
+ this.cleanup();
29
+ }
20
30
  return;
21
31
  }
22
32
  /** @inheritdoc */
23
33
  list() {
24
- return Promise.resolve(this.collections.values ? Array.from(this.collections.values()) : []);
34
+ return Promise.resolve(this.map.values ? Array.from(this.map.values()).filter(notExpired) : []);
25
35
  }
26
36
  /** @inheritdoc */
27
37
  get(key) {
28
- // Cleanup every so often to avoid memory build up
29
- this.cleanupCount++;
30
- if (this.cleanupCount >= this.maxCyclesBeforeCleanup) {
31
- this.cleanupCount = 0;
32
- this.cleanup();
38
+ const entry = this.map.get(key);
39
+ if (entry && notExpired(entry)) {
40
+ return Promise.resolve(entry);
33
41
  }
34
- return Promise.resolve(this.collections.get(key));
42
+ return null;
35
43
  }
36
44
  /** @inheritdoc */
37
45
  delete(key) {
38
- this.collections.delete(key);
46
+ this.map.delete(key);
39
47
  return;
40
48
  }
41
49
  /** @inheritdoc */
42
50
  cleanup() {
43
51
  // Remove all expired registration entries
44
- this.collections.forEach((entry, key) => {
45
- const timeElapsed = (Date.now() - entry.timeOfEntry) / 1000;
46
- if (timeElapsed > entry.retentionTimeInSeconds)
47
- this.collections.delete(key);
52
+ this.map.forEach((entry, key) => {
53
+ if (!notExpired(entry)) {
54
+ this.map.delete(key);
55
+ }
48
56
  });
49
57
  }
50
58
  }
package/dist/service.js CHANGED
@@ -38,8 +38,7 @@ const common_1 = require("@routr/common");
38
38
  const logger_1 = require("@fonoster/logger");
39
39
  const common_2 = require("@routr/common");
40
40
  const logger = (0, logger_1.getLogger)({ service: "registry", filePath: __filename });
41
- const DEFAULT_REGISTRATION_INTERVAL = 60 * 1000;
42
- const DEFAULT_RETENTION_TIME = 600 * 1000;
41
+ const DEFAULT_REGISTRATION_INTERVAL = 60;
43
42
  // TODO:
44
43
  // - We need to filter quarentine trunks from final list of trunks
45
44
  /**
@@ -49,6 +48,7 @@ const DEFAULT_RETENTION_TIME = 600 * 1000;
49
48
  */
50
49
  function registryService(config) {
51
50
  logger.info("starting registry service");
51
+ const registerInterval = config.registerInterval || DEFAULT_REGISTRATION_INTERVAL;
52
52
  const store = (0, utils_1.buildStore)(config);
53
53
  // Creates internval to send registration request every X seconds
54
54
  setInterval(() => __awaiter(this, void 0, void 0, function* () {
@@ -62,14 +62,18 @@ function registryService(config) {
62
62
  logger.error("failed to retrieve trunks from API", err);
63
63
  return;
64
64
  }
65
- const registryInvocations = resources.map((resource) => __awaiter(this, void 0, void 0, function* () {
65
+ // Create a list of trunks in the Store
66
+ const trunksInStore = (yield store.list()).map((r) => r.trunkRef);
67
+ const registryInvocations = resources
68
+ .filter((resources) => !trunksInStore.includes(resources.metadata.ref))
69
+ .map((resource) => __awaiter(this, void 0, void 0, function* () {
66
70
  const registrationRequestInput = (0, utils_1.registrationRequestInputFromTrunk)(yield (0, utils_1.convertResourceToTrunk)(dataAPI, resource), config);
67
71
  const request = (0, request_1.default)(registrationRequestInput);
68
72
  return (0, sender_1.sendRegisterMessage)(config.requesterAddr)(request);
69
73
  }));
70
74
  const results = yield Promise.allSettled(registryInvocations);
71
75
  results === null || results === void 0 ? void 0 : results.forEach((result) => __awaiter(this, void 0, void 0, function* () {
72
- var _a;
76
+ var _a, _b;
73
77
  logger.verbose("processing registration result", { result });
74
78
  if (result.status === "rejected") {
75
79
  logger.error("request rejected", result.reason);
@@ -80,7 +84,10 @@ function registryService(config) {
80
84
  return;
81
85
  }
82
86
  const message = result.value.message;
83
- const retentionTime = ((_a = message.expires) === null || _a === void 0 ? void 0 : _a.expires) || DEFAULT_RETENTION_TIME;
87
+ // Makes sure we send register before the last register expires
88
+ const retentionTime = (((_a = message.contact) === null || _a === void 0 ? void 0 : _a.expires) ||
89
+ ((_b = message.expires) === null || _b === void 0 ? void 0 : _b.expires) ||
90
+ types_1.DEFAULT_EXPIRES) - registerInterval;
84
91
  // TODO: Refactor to use ResponseType instead of string
85
92
  const status = message.responseType.toString() == "OK"
86
93
  ? types_1.RegistrationEntryStatus.REGISTERED
@@ -98,6 +105,6 @@ function registryService(config) {
98
105
  status
99
106
  });
100
107
  }));
101
- }), config.registerInterval * 1000 || DEFAULT_REGISTRATION_INTERVAL);
108
+ }), registerInterval * 1000);
102
109
  }
103
110
  exports.default = registryService;
package/dist/types.d.ts CHANGED
@@ -15,7 +15,6 @@ export interface EdgePort {
15
15
  region: string;
16
16
  }
17
17
  export interface RegistryConfig {
18
- bindAddr: string;
19
18
  requesterAddr: string;
20
19
  apiAddr: string;
21
20
  edgePorts: EdgePort[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@routr/registry",
3
- "version": "2.0.5-alpha.21",
3
+ "version": "2.0.5-alpha.23",
4
4
  "description": "Sends trunks registrations and maintains their bindings",
5
5
  "author": "Pedro Sanders <psanders@fonoster.com>",
6
6
  "homepage": "https://github.com/fonoster/routr#readme",
@@ -34,10 +34,11 @@
34
34
  "@opentelemetry/sdk-trace-base": "^1.0.4",
35
35
  "@opentelemetry/sdk-trace-node": "^1.0.4",
36
36
  "@opentelemetry/semantic-conventions": "^1.0.4",
37
- "@routr/common": "^2.0.5-alpha.21",
38
- "@routr/processor": "^2.0.5-alpha.21",
37
+ "@routr/common": "^2.0.5-alpha.23",
38
+ "@routr/processor": "^2.0.5-alpha.23",
39
39
  "ajv": "^6.12.6",
40
40
  "fp-ts": "^2.11.8",
41
+ "pb-util": "^1.0.3",
41
42
  "redis": "^4.0.4",
42
43
  "uniqid": "^5.4.0"
43
44
  },
@@ -51,5 +52,5 @@
51
52
  "devDependencies": {
52
53
  "@types/uniqid": "^5.3.2"
53
54
  },
54
- "gitHead": "904559d8d4694e16d63f553704345a987dc0d93a"
55
+ "gitHead": "318a9433328b89e7c50d0826ecff715ceb6f66dc"
55
56
  }