Skip to content

Commit

Permalink
Merge pull request #1000 from OpenGeoscience/queue-initial-size
Browse files Browse the repository at this point in the history
Allow the fetch queue to have an initial size.
  • Loading branch information
manthey authored May 16, 2019
2 parents 9b71153 + 4cc98b4 commit fb6baf3
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 2 deletions.
21 changes: 20 additions & 1 deletion src/fetchQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var fetchQueue = function (options) {

options = options || {};
this._size = options.size || 6;
this._initialSize = options.initialSize || 0;
this._track = options.track || 600;
this._needed = options.needed || null;
this._batch = false;
Expand All @@ -54,6 +55,23 @@ var fetchQueue = function (options) {
get: function () { return m_this._size; },
set: function (n) {
m_this._size = n;
if (m_this._initialSize > 1 && n < m_this._initialSize) {
m_this._initialSize = n;
}
m_this.next_item();
}
});

/**
* Get/set the initial maximum concurrent deferred object size.
* @property {number} size The initial maximum number of deferred objects.
* `0` to use `size`.
* @name geo.fetchQueue#size
*/
Object.defineProperty(this, 'initialSize', {
get: function () { return m_this._initialSize; },
set: function (n) {
m_this._initialSize = n;
m_this.next_item();
}
});
Expand Down Expand Up @@ -119,6 +137,7 @@ var fetchQueue = function (options) {
if (m_this._processing > 0) {
m_this._processing -= 1;
}
m_this._initialSize = 0;
m_this.next_item();
}).promise(defer);
m_this.next_item();
Expand Down Expand Up @@ -217,7 +236,7 @@ var fetchQueue = function (options) {
}
}
}
while (m_this._processing < m_this._size && m_this._queue.length) {
while (m_this._processing < (m_this._initialSize || m_this._size) && m_this._queue.length) {
var defer = m_this._queue.shift();
if (defer.__fetchQueue) {
m_this._processing += 1;
Expand Down
42 changes: 41 additions & 1 deletion src/tileLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ var featureLayer = require('./featureLayer');
* zoom level.
* @property {number} [cacheSize=400] The maximum number of tiles to cache.
* The default is 200 if keepLower is false.
* @property {number} [queueSize=6] The queue size. Most browsers make at most
* 6 requests to any domain, so this should be no more than 6 times the
* number of subdomains used.
* @property {number} [initialQueueSize=0] The initial queue size. `0` to use
* the queue size. When querying a tile server that needs to load
* information before serving the first time, having an initial queue size of
* 1 can reduce the load on the tile server. After the initial queue of
* tiles are loaded, the `queueSize` is used for all additional queries
* unless the `initialQueueSize` is set again or the tile cache is reset.
* @property {boolean} [keepLower=true] When truthy, keep lower zoom level
* tiles when showing high zoom level tiles. This uses more memory but
* results in smoother transitions.
Expand Down Expand Up @@ -196,6 +205,8 @@ var tileLayer = function (arg) {
var s_init = this._init,
s_exit = this._exit,
s_visible = this.visible,
m_queueSize = arg.queueSize || 6,
m_initialQueueSize = arg.initialQueueSize || 0,
m_lastTileSet = [],
m_maxBounds = [],
m_exited,
Expand All @@ -221,7 +232,8 @@ var tileLayer = function (arg) {
// initialize the tile fetch queue
this._queue = fetchQueue({
// this should probably be 6 * subdomains.length if subdomains are used
size: 6,
size: m_queueSize,
initialSize: m_initialQueueSize,
// if track is the same as the cache size, then neither processing time
// nor memory will be wasted. Larger values will use more memory,
// smaller values will do needless computations.
Expand Down Expand Up @@ -263,6 +275,33 @@ var tileLayer = function (arg) {
return $.extend({}, m_this._activeTiles); // copy on output
}});

/**
* Get/set the queue size.
* @property {number} size The queue size.
* @name geo.tileLayer#queueSize
*/
Object.defineProperty(this, 'queueSize', {
get: function () { return m_queueSize; },
set: function (n) {
m_queueSize = n;
m_this._queue.size = n;
}
});

/**
* Get/set the initial queue size.
* @property {number} size The initial queue size. `0` to use the queue
* size.
* @name geo.tileLayer#queueSize
*/
Object.defineProperty(this, 'initialQueueSize', {
get: function () { return m_initialQueueSize; },
set: function (n) {
m_initialQueueSize = n || 0;
m_this._queue.initialSize = n || m_queueSize;
}
});

/**
* The number of tiles at the given zoom level. The default implementation
* just returns `Math.pow(2, z)`.
Expand Down Expand Up @@ -1027,6 +1066,7 @@ var tileLayer = function (arg) {
this.reset = function () {
m_this.clear();
m_this._cache.clear();
m_this._queue.initialSize = m_initialQueueSize;
return m_this;
};

Expand Down
40 changes: 40 additions & 0 deletions tests/cases/fetchQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,46 @@ describe('geo.core.fetchQueue', function () {
}, 0);
});

it('initial queue size', function (done) {
var q = geo.fetchQueue({size: 4, initialSize: 3}), dlist = [];

expect(q.size).toBe(4);
expect(q.initialSize).toBe(3);
q.size = 2;
expect(q.size).toBe(2);
expect(q.initialSize).toBe(2);
q.initialSize = 1;
expect(q.size).toBe(2);
expect(q.initialSize).toBe(1);

for (var i = 0; i < 5; i += 1) {
dlist.push(make_deferred());
// add items at end of queue
q.add(dlist[i], dlist[i].process, true);
}
expect(q.length).toBe(4);
expect(q.processing).toBe(1);

// increasing the size shouldn't do anything
q.size = 3;
expect(q.size).toBe(3);
expect(q.length).toBe(4);
expect(q.processing).toBe(1);

dlist[0].defer.resolve();
window.setTimeout(function () { // wait for next time slice
expect(q.length).toBe(1);
expect(q.processing).toBe(3);

dlist[1].defer.resolve();
window.setTimeout(function () { // wait for next time slice
expect(q.length).toBe(0);
expect(q.processing).toBe(3);
done();
}, 0);
}, 0);
});

it('queue removes and skips unneeded items', function (done) {
var q = geo.fetchQueue({
size: 2,
Expand Down
15 changes: 15 additions & 0 deletions tests/cases/tileLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,21 @@ describe('geo.tileLayer', function () {
layer._update();
expect(count).toBe(3);
});
it('queueSize', function () {
var m = map(), layer;
opts.map = m;
layer = geo.tileLayer(opts);
expect(layer.queueSize).toBe(6);
expect(layer.initialQueueSize).toBe(0);
expect(layer._queue.size).toBe(6);
expect(layer._queue.initialSize).toBe(0);
layer.queueSize = 4;
expect(layer.queueSize).toBe(4);
expect(layer._queue.size).toBe(4);
layer.initialQueueSize = 1;
expect(layer.initialQueueSize).toBe(1);
expect(layer._queue.initialSize).toBe(1);
});
});
describe('Public utility methods', function () {
describe('isValid', function () {
Expand Down

0 comments on commit fb6baf3

Please sign in to comment.