【问题标题】:100s of HTTP requests with delays inbetween each100 个 HTTP 请求,每个请求之间有延迟
【发布时间】:2019-02-13 21:14:17
【问题描述】:

我需要向门控 API 发出 100 次 http 请求,每秒 5 个请求。将 Angular 6 与 CLI 和 RXjs 一起使用。在 Node.js 服务器上,我知道如何使用 request-retry NPM 包来做到这一点。如何使用 Angular 6 做到这一点?

我知道如何发出一个请求,或者多少个请求,但是我如何插入一个时间延迟,以便一秒钟只发出 5 个请求?

getApps(luisAuthoringKey:string): Observable<any> {

  this.httpOptions.headers.set("Ocp-Apim-Subscription-Key",luisAuthoringKey);

  return this.http.get(this.endpoint + 'apps', this.httpOptions).pipe(
    map(this.extractData));
}

【问题讨论】:

标签: angular typescript http rxjs wait


【解决方案1】:

使用来自 rxjs 的 intervaltimer 运算符

带计时器

this.http.get(this.endpoint + 'apps', this.httpOptions).pipe(
  map(this.extractData),
  timer(200),
  catchError(e=>of(e)),
  repeat(100),
)

有间隔

interval(200).pipe(
  this.http.get(this.endpoint + 'apps', this.httpOptions),
  map(this.extractData),
  catchError(e=>of(e)),
  take(100),
)

【讨论】:

    【解决方案2】:

    我认为这里最好的选择是 bufferCount 运算符,然后是 delay 以确保最小 1s 延迟。

    from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
      .pipe(
        concatMap(v => of(v).pipe(delay(120))),
        bufferCount(5),
        concatMap(buffer => of(buffer).pipe(delay(1000)))
      )
      .subscribe(console.log);
    

    现场演示:https://stackblitz.com/edit/rxjs6-demo-k5wins?file=index.ts

    【讨论】:

      猜你喜欢
      • 2023-01-28
      • 1970-01-01
      • 2015-08-14
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 2013-02-09
      • 2017-08-12
      • 2014-03-27
      相关资源
      最近更新 更多