我有一个使用API网关的应用。用户登录后,将提供仅一个小时有效的令牌。
此网关与众不同的原因是,该令牌由所有用户共享,并且续约时间并非会话唯一。它不能在任何时间进行更新,但是设置的时间为每小时5分32秒。
到目前为止,这是我需要管理令牌更新的内容:
videoDecoder.onmessage = async function (e) {
const blob = e.data;
const bmp = await createImageBitmap( blob );
dCtx.drawImage(bmp 0, 0);
bmp.close();
};
但是,我一直遇到这样的问题:令牌更新时,我的API请求返回// This will get the Gateway Token
getToken = (baseUrl, httpOptions) => {
return this.http.post<oAuthResponse>(baseUrl + 'token', 'grant_type=client_credentials', httpOptions)
}
// this will be called when the token needs to be renewed
renewToken = (baseUrl, httpOptions) => {
this.getToken(baseUrl, httpOptions).subscribe( (res:any) => {
this.keepAlive(res.expires_in)
})
}
// this triggers the renewal at the set time
keepAlive = (t) => {
setTimeout(()=>{
this.setToken()
}, t*1000 )
}
。
我希望能够在调用401 error
或getToken()
的同时将其他API请求排队。因此,在返回此调用时发出的请求将等待完成。
我将如何去做?我当前的API请求是这样设置的:
renewToken()