API轮询和超时 [英] API polling and timeout

查看:105
本文介绍了API轮询和超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个轮询用例,其中:

I have a polling use-case where:

  1. 我想调用一个基于业务逻辑的API,该API可立即(1.5-2秒)返回数字(1-10)或错误(API中的网络问题/异常等).
  2. 如果API返回错误(API中的网络问题/异常等),那么我想取消订阅轮询和显示错误.
  3. 如果API返回成功,我想检查返回值并取消订阅(如果返回值为5)或继续进行轮询.
  4. 我想每5秒调用一次API.
  5. 我要将轮询的最大时间(超时/阈值)保持为3分钟.如果在这3分钟内我没有得到所需的响应(5号),则轮询应该会出错.

这是我目前实施的方式:

This is how I have implemented it currently:

this.trackSpoke$ = interval(5000)
                .pipe(
                    timeout(250000),
                    startWith(0),
                    switchMap(() =>
                        this.sharedService.pollForAPropertyValue(
                            "newuser"
                        )
                    )
                )
                .subscribe(
                    (data: SpokeProperty) => {
                        this.CheckSpokeStatus(data);
                    },
                    error => {
                        this.trackSpoke$.unsubscribe();
                        this.createSpokeState.CdhResponseTimedOut();
                    }
                );


private CheckSpokeStatus(data) {
        if (data.PropertyValue === "5") {
            this.trackSpoke$.unsubscribe();
            //display success   
        } else {
              //keep the polling going
        }
    }

但是,以上实现并未超时.

But, the above implementation is not timing out.

需要做些什么才能使它超时并能够实现所有提到的用例?

What needs to be done so that it times out and I am able to achieve all mentioned use-case?

推荐答案

首先使用 interval 进行API轮询是一种反模式,因为 interval 不会等待"您的http请求完成-可能触发多个请求(如果该请求需要5秒钟以上的时间才能完成).

First of all using interval for API polling is quite an anti-pattern because interval won't "wait" for your http request to finish - potentially triggering multiple requests (if the request takes more then 5s to complete).

我更喜欢使用 使用 repeatWhen delay (请参见下面的代码).

I prefer to use defer with repeatWhen and delay (see the code below).

timeout 不会触发,因为 interval 每隔5s滴答一次,以防止发生超时. defer / repeatWhen 组合也应该可以解决该问题.

The timeout is not triggering because the interval is ticking every 5s preventing the timeout from ever occurring. The defer/repeatWhen combo should also fix that.

考虑使用 takeWhile 为您取消订阅Observable.

Instead of manually unsubscribing consider using takeWhile to unsubscribe the Observable for you.

还不需要在错误处理程序中使用 this.trackSpoke $ .unsubscribe(); ,因为在发生错误时会自动取消订阅Observable.

Also using this.trackSpoke$.unsubscribe(); in the error handler is not needed because Observable is unsubscribed automatically in case of an error.

this.trackSpoke$ = defer(() => this.sharedService.pollForAPropertyValue("newuser"))
    .pipe(
        timeout(250000),
        repeatWhen(notifications => notifications.delay(5000)),
        takeWhile(data => this.CheckSpokeStatus(data)),
    )
    .subscribe(
        error => {
            this.createSpokeState.CdhResponseTimedOut();
        }
    );


private CheckSpokeStatus(data) {
    return data.PropertyValue !== "5";
}

这篇关于API轮询和超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆