Skip to content

Commit 4827168

Browse files
authored
Merge pull request #6445 from AnalyticalGraphicsInc/http2-throttling
Allow RequestScheduler to throttle white-listed servers differently.
2 parents 0df9363 + ac36dd4 commit 4827168

File tree

2 files changed

+66
-25
lines changed

2 files changed

+66
-25
lines changed

Source/Core/RequestScheduler.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ define([
22
'../ThirdParty/Uri',
33
'../ThirdParty/when',
44
'./Check',
5+
'./defaultValue',
56
'./defined',
67
'./defineProperties',
78
'./Event',
@@ -13,6 +14,7 @@ define([
1314
Uri,
1415
when,
1516
Check,
17+
defaultValue,
1618
defined,
1719
defineProperties,
1820
Event,
@@ -67,12 +69,21 @@ define([
6769
RequestScheduler.maximumRequests = 50;
6870

6971
/**
70-
* The maximum number of simultaneous active requests per server. Un-throttled requests do not observe this limit.
72+
* The maximum number of simultaneous active requests per server. Un-throttled requests or servers specifically
73+
* listed in requestsByServer do not observe this limit.
7174
* @type {Number}
7275
* @default 6
7376
*/
7477
RequestScheduler.maximumRequestsPerServer = 6;
7578

79+
/**
80+
* A per serverKey list of overrides to use for throttling instead of maximumRequestsPerServer
81+
*/
82+
RequestScheduler.requestsByServer = {
83+
'api.cesium.com:443': 18,
84+
'assets.cesium.com:443': 18
85+
};
86+
7687
/**
7788
* Specifies if the request scheduler should throttle incoming requests, or let the browser queue requests under its control.
7889
* @type {Boolean}
@@ -146,7 +157,8 @@ define([
146157
}
147158

148159
function serverHasOpenSlots(serverKey) {
149-
return numberOfActiveRequestsByServer[serverKey] < RequestScheduler.maximumRequestsPerServer;
160+
var maxRequests = defaultValue(RequestScheduler.requestsByServer[serverKey], RequestScheduler.maximumRequestsPerServer);
161+
return numberOfActiveRequestsByServer[serverKey] < maxRequests;
150162
}
151163

152164
function issueRequest(request) {

Specs/Core/RequestSchedulerSpec.js

+52-23
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,25 @@ defineSuite([
1313
var originalMaximumRequests;
1414
var originalMaximumRequestsPerServer;
1515
var originalPriorityHeapLength;
16+
var originalRequestsByServer;
1617

1718
beforeAll(function() {
1819
originalMaximumRequests = RequestScheduler.maximumRequests;
1920
originalMaximumRequestsPerServer = RequestScheduler.maximumRequestsPerServer;
2021
originalPriorityHeapLength = RequestScheduler.priorityHeapLength;
22+
originalRequestsByServer = RequestScheduler.requestsByServer;
2123
});
2224

2325
beforeEach(function() {
2426
RequestScheduler.clearForSpecs();
27+
RequestScheduler.requestsByServer = {};
2528
});
2629

2730
afterEach(function() {
2831
RequestScheduler.maximumRequests = originalMaximumRequests;
2932
RequestScheduler.maximumRequestsPerServer = originalMaximumRequestsPerServer;
3033
RequestScheduler.priorityHeapLength = originalPriorityHeapLength;
34+
RequestScheduler.requestsByServer = originalRequestsByServer;
3135
});
3236

3337
it('request throws when request is undefined', function() {
@@ -61,13 +65,13 @@ defineSuite([
6165
});
6266

6367
it('getServer with https', function() {
64-
var server = RequestScheduler.getServerKey('https://foo.com/1');
65-
expect(server).toEqual('foo.com:443');
68+
var server = RequestScheduler.getServerKey('https://test.invalid/1');
69+
expect(server).toEqual('test.invalid:443');
6670
});
6771

6872
it('getServer with http', function() {
69-
var server = RequestScheduler.getServerKey('http://foo.com/1');
70-
expect(server).toEqual('foo.com:80');
73+
var server = RequestScheduler.getServerKey('http://test.invalid/1');
74+
expect(server).toEqual('test.invalid:80');
7175
});
7276

7377
it('honors maximumRequests', function() {
@@ -84,7 +88,7 @@ defineSuite([
8488

8589
function createRequest() {
8690
return new Request({
87-
url : 'http://foo.com/1',
91+
url : 'http://test.invalid/1',
8892
requestFunction : requestFunction,
8993
throttle : true
9094
});
@@ -147,7 +151,7 @@ defineSuite([
147151
return deferred.promise;
148152
}
149153

150-
var url = 'http://foo.com/1';
154+
var url = 'http://test.invalid/1';
151155
var server = RequestScheduler.getServerKey(url);
152156

153157
function createRequest() {
@@ -216,7 +220,7 @@ defineSuite([
216220

217221
function createRequest(priority) {
218222
var request = new Request({
219-
url : 'http://foo.com/1',
223+
url : 'http://test.invalid/1',
220224
requestFunction : requestFunction,
221225
throttle : true,
222226
priority : priority
@@ -302,7 +306,7 @@ defineSuite([
302306
});
303307

304308
it('request goes through immediately when throttle is false', function() {
305-
var url = 'https://foo.com/1';
309+
var url = 'https://test.invalid/1';
306310
testImmediateRequest(url, false);
307311
});
308312

@@ -317,7 +321,7 @@ defineSuite([
317321

318322
var request = new Request({
319323
throttle : true,
320-
url : 'https://foo.com/1',
324+
url : 'https://test.invalid/1',
321325
requestFunction : requestFunction
322326
});
323327
expect(request.state).toBe(RequestState.UNISSUED);
@@ -342,7 +346,7 @@ defineSuite([
342346

343347
var request = new Request({
344348
throttle : true,
345-
url : 'https://foo.com/1',
349+
url : 'https://test.invalid/1',
346350
requestFunction : requestFunction
347351
});
348352

@@ -373,7 +377,7 @@ defineSuite([
373377

374378
var request = new Request({
375379
throttle : true,
376-
url : 'https://foo.com/1',
380+
url : 'https://test.invalid/1',
377381
requestFunction : requestFunction,
378382
cancelFunction : cancelFunction
379383
});
@@ -409,7 +413,7 @@ defineSuite([
409413
}
410414

411415
var request = new Request({
412-
url : 'https://foo.com/1',
416+
url : 'https://test.invalid/1',
413417
requestFunction : requestFunction
414418
});
415419

@@ -442,7 +446,7 @@ defineSuite([
442446
function createRequest(priority) {
443447
return new Request({
444448
throttle : true,
445-
url : 'https://foo.com/1',
449+
url : 'https://test.invalid/1',
446450
requestFunction : getRequestFunction(priority),
447451
priority : priority
448452
});
@@ -477,7 +481,7 @@ defineSuite([
477481
function createRequest(priority) {
478482
return new Request({
479483
throttle : true,
480-
url : 'https://foo.com/1',
484+
url : 'https://test.invalid/1',
481485
requestFunction : requestFunction,
482486
priorityFunction : getPriorityFunction(priority)
483487
});
@@ -529,7 +533,7 @@ defineSuite([
529533
function createRequest(priority) {
530534
return new Request({
531535
throttle : true,
532-
url : 'https://foo.com/1',
536+
url : 'https://test.invalid/1',
533537
requestFunction : requestFunction,
534538
priority : priority
535539
});
@@ -566,7 +570,7 @@ defineSuite([
566570

567571
function createRequest(throttle) {
568572
return new Request({
569-
url : 'http://foo.com/1',
573+
url : 'http://test.invalid/1',
570574
requestFunction : requestFunction,
571575
throttle : throttle
572576
});
@@ -604,7 +608,7 @@ defineSuite([
604608

605609
function createRequest(throttleByServer) {
606610
return new Request({
607-
url : 'http://foo.com/1',
611+
url : 'http://test.invalid/1',
608612
requestFunction : requestFunction,
609613
throttleByServer : throttleByServer
610614
});
@@ -637,7 +641,7 @@ defineSuite([
637641
RequestScheduler.throttleRequests = true;
638642
var request = new Request({
639643
throttle : true,
640-
url : 'https://foo.com/1',
644+
url : 'https://test.invalid/1',
641645
requestFunction : requestFunction
642646
});
643647
var promise = RequestScheduler.request(request);
@@ -646,7 +650,7 @@ defineSuite([
646650
RequestScheduler.throttleRequests = false;
647651
request = new Request({
648652
throttle : true,
649-
url : 'https://foo.com/1',
653+
url : 'https://test.invalid/1',
650654
requestFunction : requestFunction
651655
});
652656
promise = RequestScheduler.request(request);
@@ -669,7 +673,7 @@ defineSuite([
669673

670674
function createRequest() {
671675
return new Request({
672-
url : 'https://foo.com/1',
676+
url : 'https://test.invalid/1',
673677
requestFunction : requestFunction
674678
});
675679
}
@@ -708,7 +712,7 @@ defineSuite([
708712
}
709713

710714
var request = new Request({
711-
url : 'https://foo.com/1',
715+
url : 'https://test.invalid/1',
712716
requestFunction : requestFunction
713717
});
714718

@@ -806,7 +810,7 @@ defineSuite([
806810
}
807811

808812
var request = new Request({
809-
url : 'https://foo.com/1',
813+
url : 'https://test.invalid/1',
810814
requestFunction : requestFunction
811815
});
812816

@@ -839,7 +843,7 @@ defineSuite([
839843
}
840844

841845
var requestToCancel = new Request({
842-
url : 'https://foo.com/1',
846+
url : 'https://test.invalid/1',
843847
requestFunction : requestCancelFunction
844848
});
845849

@@ -854,4 +858,29 @@ defineSuite([
854858
cancelDeferred.resolve();
855859
removeListenerCallback();
856860
});
861+
862+
it('RequestScheduler.requestsByServer allows for custom maximum requests', function() {
863+
var promise;
864+
865+
RequestScheduler.requestsByServer['test.invalid:80'] = 23;
866+
867+
for (var i = 0; i < 23; i++) {
868+
promise = RequestScheduler.request(new Request({
869+
url: 'http://test.invalid/1',
870+
throttle: true,
871+
throttleByServer: true,
872+
requestFunction: function() { return when.defer(); }
873+
}));
874+
RequestScheduler.update();
875+
expect(promise).toBeDefined();
876+
}
877+
878+
promise = RequestScheduler.request(new Request({
879+
url: 'http://test.invalid/1',
880+
throttle: true,
881+
throttleByServer: true,
882+
requestFunction: function() { return when.defer(); }
883+
}));
884+
expect(promise).toBeUndefined();
885+
});
857886
});

0 commit comments

Comments
 (0)