Add request throttling to the abstract stats tracker

This is the best of three options

1. Users of stats trackers control polling (old method)
2. Stat tracker is stateful and has start/stop methods (like logging)
3. Stat trackers blindly throttle requests

This is the best option because it means N number of concurrent users of
a stats tracker can request polling without inundating the tracker with
redundant frames (or the network with redundant requests), but they also
don't have to coordinate amongst themselves to determine what state a
tracker should be in.
This commit is contained in:
Michael Lange 2018-09-17 15:58:28 -07:00
parent ae161d75bc
commit 8900d5c138
2 changed files with 12 additions and 5 deletions

View file

@ -79,8 +79,8 @@ export default Component.extend({
poller: task(function*() { poller: task(function*() {
do { do {
yield this.get('tracker').poll(); yield this.get('tracker.poll').perform();
yield timeout(2000); yield timeout(10);
} while (!Ember.testing); } while (!Ember.testing);
}), }),

View file

@ -1,5 +1,6 @@
import Mixin from '@ember/object/mixin'; import Mixin from '@ember/object/mixin';
import { assert } from '@ember/debug'; import { assert } from '@ember/debug';
import { task, timeout } from 'ember-concurrency';
export default Mixin.create({ export default Mixin.create({
url: '', url: '',
@ -16,12 +17,18 @@ export default Mixin.create({
); );
}, },
poll() { // Uses EC as a form of debounce to prevent multiple
// references to the same tracker from flooding the tracker,
// but also avoiding the issue where different places where the
// same tracker is used needs to coordinate.
poll: task(function*() {
const url = this.get('url'); const url = this.get('url');
assert('Url must be defined', url); assert('Url must be defined', url);
return this.get('fetch')(url) yield this.get('fetch')(url)
.then(res => res.json()) .then(res => res.json())
.then(frame => this.append(frame)); .then(frame => this.append(frame));
},
yield timeout(2000);
}).drop(),
}); });