我使用Ember数据与具有速率限制的REST交互。
对于如何将Ember数据请求节流为每秒X请求,有什么建议吗?
发布于 2018-03-18 14:30:32
首先,如果您想处理所有AJAX请求,我强烈建议使用ember-ajax。这为您提供了一个点来修改所有AJAX请求。下一个是关于节流。我在评论中建议使用ember-concurrency,所以让我详细说明一下。ember-concurrency为您提供了一个timeout方法,该方法返回在一定时间后将解决的承诺。这样可以方便地进行节流:
export default AjaxService.extend({
ajaxThrottle: task(function * (cb) {
cb();
// maxConcurrency with timeout 1000 means at maximum 3 requests per second.
yield timeout(1000);
}).maxConcurrency(3).enqueue(),
async request() {
await new Promise(cb => ajaxThrottle.perform(cb));
return this._super(...arguments); // this should make the actual request
},
});发布于 2018-03-13 05:25:30
经过一些努力,我设法让承诺节流阀在我的ApplicationController中工作。限制为每秒4个请求:
import PromiseThrottle from 'dispatcher/application/promise-throttle';
import { Promise } from 'rsvp';
export default DS.RESTAdapter.extend(DataAdapterMixin, {
init() {
this.set('promiseThrottle', new PromiseThrottle({
requestsPerSecond: 4,
promiseImplementation: Promise
}));
},
ajax: function (url, type, options) {
return this.get('promiseThrottle').add(this._super.bind(this, url, type, options));
}
...
});https://stackoverflow.com/questions/49182415
复制相似问题