@thzero/library_server_service_grpc 0.19.1 → 0.19.2

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.
package/client.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as grpc from '@grpc/grpc-js';
2
- import { Mutex as asyncMutex } from 'async-mutex';
3
2
 
4
3
  import LibraryCommonUtility from '@thzero/library_common/utility/index.js';
4
+ import LibraryMomentUtility from '@thzero/library_common/utility/moment.js';
5
5
 
6
6
  import LibraryServerConstants from '@thzero/library_server/constants.js';
7
7
 
@@ -11,11 +11,16 @@ class BaseClientGrpcService extends Service {
11
11
  constructor() {
12
12
  super();
13
13
 
14
- this._mutex = new asyncMutex();
15
-
16
14
  this._serviceDiscoveryResources = null;
17
15
 
16
+ // key -> discovered host. A failed discovery is remembered by time so a
17
+ // backend that is down is asked about once per retry window rather than on
18
+ // every call, and a discovery in flight is shared by the calls that arrive
19
+ // while it runs.
18
20
  this._hosts = new Map();
21
+ this._hostsFailed = new Map();
22
+ this._hostsPending = new Map();
23
+ this._hostsRetryMs = 5 * 1000;
19
24
  }
20
25
 
21
26
  async init(injector) {
@@ -92,30 +97,42 @@ class BaseClientGrpcService extends Service {
92
97
  if (!enabled)
93
98
  return host;
94
99
 
95
- let discovered = this._hosts.get(key);
100
+ const discovered = this._hosts.get(key);
96
101
  if (discovered)
97
102
  return discovered;
98
103
 
99
- const release = await this._mutex.acquire();
100
- try {
101
- discovered = this._hosts.get(key);
102
- if (discovered)
103
- return discovered;
104
-
105
- this._enforceNotNull('BaseClientGrpcService', '_host', config.discoverable.name, 'discoveryName', correlationId);
106
-
107
- const response = await this._serviceDiscoveryResources.getService(correlationId, config.discoverable.name);
108
- if (this._hasFailed(response))
109
- return null;
104
+ // A failure used to cache nothing, so every call after it took the mutex
105
+ // and asked discovery again, one at a time.
106
+ const failed = this._hostsFailed.get(key);
107
+ if (failed && ((LibraryMomentUtility.getTimestamp() - failed) < this._hostsRetryMs))
108
+ return null;
109
+
110
+ // One discovery per key at a time. Calls for the same key share it; calls
111
+ // for other keys are not held up by it, which the single mutex did.
112
+ let pending = this._hostsPending.get(key);
113
+ if (!pending) {
114
+ pending = this._hostDiscover(correlationId, host, config, key)
115
+ .finally(() => {
116
+ this._hostsPending.delete(key);
117
+ });
118
+ this._hostsPending.set(key, pending);
119
+ }
120
+ return await pending;
121
+ }
110
122
 
111
- host = await this._hostFromResource(correlationId, host, response.results);
123
+ async _hostDiscover(correlationId, host, config, key) {
124
+ this._enforceNotNull('BaseClientGrpcService', '_host', config.discoverable.name, 'discoveryName', correlationId);
112
125
 
113
- this._hosts.set(key, host);
114
- }
115
- finally {
116
- release();
126
+ const response = await this._serviceDiscoveryResources.getService(correlationId, config.discoverable.name);
127
+ if (this._hasFailed(response)) {
128
+ this._hostsFailed.set(key, LibraryMomentUtility.getTimestamp());
129
+ return null;
117
130
  }
118
131
 
132
+ host = await this._hostFromResource(correlationId, host, response.results);
133
+
134
+ this._hosts.set(key, host);
135
+ this._hostsFailed.delete(key);
119
136
  return host;
120
137
  }
121
138
 
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@thzero/library_server_service_grpc",
3
3
  "type": "module",
4
- "version": "0.19.1",
4
+ "version": "0.19.2",
5
5
  "version_major": 0,
6
6
  "version_minor": 19,
7
- "version_patch": 1,
8
- "version_date": "09/15/2026",
7
+ "version_patch": 2,
8
+ "version_date": "09/23/2026",
9
9
  "description": "",
10
10
  "author": "thZero",
11
11
  "author_url": "https://www.thzero.com",
@@ -2,8 +2,13 @@ import assert from 'node:assert/strict';
2
2
  import { beforeEach, describe, it } from 'node:test';
3
3
 
4
4
  import '@thzero/library_common/utility/string.js';
5
+ import LibraryMomentUtility from '@thzero/library_common/utility/moment.js';
5
6
  import BaseClientGrpcService from '../client.js';
6
7
 
8
+ // dayjs.utc() only exists once the plugins are registered; the app does this at
9
+ // boot, so anything calling getTimestamp() outside a booted app must do it too.
10
+ LibraryMomentUtility.initDateTime();
11
+
7
12
  const inject = (target, name, value) => {
8
13
  Object.defineProperty(target, name, { value, writable: true, configurable: true });
9
14
  return target;
@@ -95,6 +100,69 @@ describe('_hostFromConfig', () => {
95
100
  assert.equal(await service._hostFromConfig('cid', host(), 'key', null), null);
96
101
  });
97
102
 
103
+ // Regression: a failure cached nothing, so every call after it took the mutex
104
+ // and asked discovery again, one at a time.
105
+ it('does not ask discovery again for a failed key within the retry window', async () => {
106
+ let lookups = 0;
107
+ inject(service, '_config', { getBackend: () => ({ baseUrl: 'http://backend:1', discoverable: { name: 'svc' } }) });
108
+ service._serviceDiscoveryResources = { getService: async () => { lookups++; return { success: false }; } };
109
+ assert.equal(await service._hostFromConfig('cid', host(), 'key', null), null);
110
+ assert.equal(await service._hostFromConfig('cid', host(), 'key', null), null);
111
+ assert.equal(await service._hostFromConfig('cid', host(), 'key', null), null);
112
+ assert.equal(lookups, 1);
113
+ });
114
+
115
+ it('asks again once the retry window has passed, and forgets the failure on success', async () => {
116
+ let lookups = 0;
117
+ let succeed = false;
118
+ inject(service, '_config', { getBackend: () => ({ baseUrl: 'http://backend:1', discoverable: { name: 'svc' } }) });
119
+ service._serviceDiscoveryResources = {
120
+ getService: async () => { lookups++; return succeed ? { success: true, results: { address: 'up', grpc: { port: 1 } } } : { success: false }; }
121
+ };
122
+ service._hostsRetryMs = 0;
123
+ assert.equal(await service._hostFromConfig('cid', host(), 'key', null), null);
124
+ succeed = true;
125
+ const result = await service._hostFromConfig('cid', host(), 'key', null);
126
+ assert.equal(result.url, 'up:1');
127
+ assert.equal(lookups, 2);
128
+ assert.equal(service._hostsFailed.has('key'), false);
129
+ });
130
+
131
+ it('shares one discovery between concurrent calls for the same key', async () => {
132
+ let lookups = 0;
133
+ let release;
134
+ const gate = new Promise((resolve) => { release = resolve; });
135
+ inject(service, '_config', { getBackend: () => ({ baseUrl: 'http://backend:1', discoverable: { name: 'svc' } }) });
136
+ service._serviceDiscoveryResources = {
137
+ getService: async () => { lookups++; await gate; return { success: true, results: { address: 'discovered', grpc: { port: 9000 } } }; }
138
+ };
139
+ const pending = Promise.all([ service._hostFromConfig('a', host(), 'key', null), service._hostFromConfig('b', host(), 'key', null) ]);
140
+ release();
141
+ const [ first, second ] = await pending;
142
+ assert.equal(lookups, 1);
143
+ assert.equal(second, first);
144
+ assert.equal(service._hostsPending.size, 0);
145
+ });
146
+
147
+ // The single mutex serialized every key behind whichever was in flight.
148
+ it('does not hold one key\'s discovery behind another\'s', async () => {
149
+ let release;
150
+ const gate = new Promise((resolve) => { release = resolve; });
151
+ inject(service, '_config', { getBackend: (correlationId, key) => ({ baseUrl: 'http://backend:1', discoverable: { name: key } }) });
152
+ service._serviceDiscoveryResources = {
153
+ getService: async (correlationId, name) => {
154
+ if (name === 'slow')
155
+ await gate;
156
+ return { success: true, results: { address: name, grpc: { port: 1 } } };
157
+ }
158
+ };
159
+ const slow = service._hostFromConfig('cid', host(), 'slow', null);
160
+ const fast = await service._hostFromConfig('cid', host(), 'fast', null);
161
+ assert.equal(fast.url, 'fast:1', 'resolved while the slow one is still in flight');
162
+ release();
163
+ assert.equal((await slow).url, 'slow:1');
164
+ });
165
+
98
166
  it('rejects a config without a base url', async () => {
99
167
  inject(service, '_config', { getBackend: () => ({}) });
100
168
  await assert.rejects(() => service._hostFromConfig('cid', host(), 'key', null), /config.baseUrl is empty/);