Skip to content

Commit 34cc9fc

Browse files
robstaxrstachof
andauthored
fix(u2c): make v1 handle negative host priorities (#4621)
Co-authored-by: rstachof <[email protected]>
1 parent 4820468 commit 34cc9fc

File tree

2 files changed

+119
-1
lines changed

2 files changed

+119
-1
lines changed

packages/@webex/webex-core/src/lib/services/service-url.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ const ServiceUrl = AmpState.extend({
6060
? this.hosts.filter((host) => host.id === clusterId)
6161
: this.hosts.filter((host) => host.homeCluster);
6262

63+
// Filter out hosts with non-positive priorities (including -1)
64+
filteredHosts = filteredHosts.filter((host) => host.priority > 0);
65+
66+
// If no valid hosts remain after filtering, return the default URL
67+
if (filteredHosts.length === 0) {
68+
return this.defaultUrl;
69+
}
70+
6371
const aliveHosts = filteredHosts.filter((host) => !host.failed);
6472

6573
filteredHosts =
@@ -76,7 +84,7 @@ const ServiceUrl = AmpState.extend({
7684
filteredHosts.reduce(
7785
(previous, current) =>
7886
previous.priority > current.priority || !previous.homeCluster ? current : previous,
79-
{}
87+
filteredHosts[0]
8088
).host
8189
);
8290
},

packages/@webex/webex-core/test/unit/spec/services/service-url.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,116 @@ describe('webex-core', () => {
151151

152152
assert.isTrue(homeClusterUrls.every((host) => !host.failed));
153153
});
154+
155+
describe('when hosts have negative priorities', () => {
156+
it('should return defaultUrl when all hosts have negative priorities', () => {
157+
const negativeServiceUrl = new ServiceUrl({
158+
defaultUrl: 'https://default.example.com/api/v1',
159+
hosts: [
160+
{
161+
host: 'example-host-neg1.com',
162+
priority: -1,
163+
ttl: -1,
164+
id: '1',
165+
homeCluster: true,
166+
},
167+
{
168+
host: 'example-host-neg2.com',
169+
priority: -1,
170+
ttl: -1,
171+
id: '2',
172+
homeCluster: true,
173+
},
174+
],
175+
name: 'negative-priority-test',
176+
});
177+
178+
assert.equal(
179+
negativeServiceUrl._getPriorityHostUrl(),
180+
'https://default.example.com/api/v1'
181+
);
182+
});
183+
184+
it('should return defaultUrl when all hosts have zero priority', () => {
185+
const zeroServiceUrl = new ServiceUrl({
186+
defaultUrl: 'https://default.example.com/api/v1',
187+
hosts: [
188+
{
189+
host: 'example-host-zero.com',
190+
priority: 0,
191+
ttl: -1,
192+
id: '1',
193+
homeCluster: true,
194+
},
195+
],
196+
name: 'zero-priority-test',
197+
});
198+
199+
assert.equal(zeroServiceUrl._getPriorityHostUrl(), 'https://default.example.com/api/v1');
200+
});
201+
202+
it('should ignore hosts with negative priorities and return valid host', () => {
203+
const mixedServiceUrl = new ServiceUrl({
204+
defaultUrl: 'https://default.example.com/api/v1',
205+
hosts: [
206+
{
207+
host: 'example-host-neg.com',
208+
priority: -1,
209+
ttl: -1,
210+
id: '1',
211+
homeCluster: true,
212+
},
213+
{
214+
host: 'example-host-valid.com',
215+
priority: 5,
216+
ttl: -1,
217+
id: '2',
218+
homeCluster: true,
219+
},
220+
],
221+
name: 'mixed-priority-test',
222+
});
223+
224+
const result = mixedServiceUrl._getPriorityHostUrl();
225+
226+
assert.include(result, 'example-host-valid.com');
227+
assert.notInclude(result, 'example-host-neg.com');
228+
});
229+
230+
it('should select lowest positive priority host when mixed with negative priorities', () => {
231+
const mixedServiceUrl = new ServiceUrl({
232+
defaultUrl: 'https://default.example.com/api/v1',
233+
hosts: [
234+
{
235+
host: 'example-host-neg.com',
236+
priority: -1,
237+
ttl: -1,
238+
id: '1',
239+
homeCluster: true,
240+
},
241+
{
242+
host: 'example-host-p5.com',
243+
priority: 5,
244+
ttl: -1,
245+
id: '2',
246+
homeCluster: true,
247+
},
248+
{
249+
host: 'example-host-p2.com',
250+
priority: 2,
251+
ttl: -1,
252+
id: '3',
253+
homeCluster: true,
254+
},
255+
],
256+
name: 'mixed-priority-test',
257+
});
258+
259+
const result = mixedServiceUrl._getPriorityHostUrl();
260+
261+
assert.include(result, 'example-host-p2.com');
262+
});
263+
});
154264
});
155265

156266
describe('#failHost()', () => {

0 commit comments

Comments
 (0)