【问题标题】:Race condition in ngrx effects with switchmap resulting slow performance带有 switchmap 的 Ngrx 效果中的竞争条件导致性能下降
【发布时间】:2019-11-07 16:45:23
【问题描述】:

我有一个使用 Injected Angular 服务发出 GET 请求的简单效果。

该请求返回 JSON 响应(10 000 行)。

  @Effect()
  effectExample$ = this.actions$
    .pipe(
      ofType(actions.ONE_EXAMPLE_REST_API_ACTION),
      switchMap((action: actions.OneExampleRestApiAction) => {
        console.time('timer');
        return this.service
          .getJsonResponse()
          .pipe(
            map((response) => {
              console.timeEnd('timer');
              return new actions.OneExampleRestApiActionSuccess(response);
            })
          );
      })
    );

如您所见,我添加了 console.time('计时器'); console.timeEnd('timer');

发生了什么?

在控制台中,我得到该计时器大约需要 15 到 20 秒。它会有所不同,而如果我使用 POSTMAN 调用 getJsonResponse() 调用的相同请求,则请求总是最多需要 2-3 秒。

getJsonResponse() {
return this.http
      .get(`http://example.com/getjson`);
}

执行地图需要 15 - 20 秒的原因是什么?

return new actions.OneExampleRestApiActionSuccess(response);

可能是因为响应是 10000 行的 JSON 对象,所以邮递员可以更快地处理它还是?我现在真的不知道什么是阻止者: .getJsonResponse()返回新的操作。OneExampleRestApiActionSuccess(response);

因为如果我使用邮递员,服务器只需要 2-3 秒即可返回响应。

同样在上述请求的网络选项卡中,它显示执行时间为 2 秒,但显然 Angular 或 Effect 需要 15 - 20 秒才能到达 return new actions.OneExampleRestApiActionSuccess(response);

【问题讨论】:

    标签: angular rxjs ngrx


    【解决方案1】:

    由于 switchMap,您遇到了竞争条件。这会导致您在响应时间中看到的延迟。 这是您参考的好post

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-22
      • 1970-01-01
      • 1970-01-01
      • 2015-04-18
      • 1970-01-01
      相关资源
      最近更新 更多