简体   繁体   English

axios 获取请求错误:请求失败,状态码为 504

[英]axios get request Error: Request failed with status code 504

here is my code that makes an Http Get request to an API end point from one of the services running on Amazon Fargate service.这是我的代码,它从 Amazon Fargate 服务上运行的服务之一向 API 端点发出 Http Get 请求。 The API is powered by Amazon API gateway and Lambda. API 由 Amazon API 网关和 Lambda 提供支持。 Also this is a private api used with in the VPC and I also have setup the apigateway VPC end point to facilitate the same.此外,这是在 VPC 中使用的私有 api,我还设置了 apigateway VPC 端点以方便相同的操作。 I have received this error only once.我只收到过一次这个错误。 All the subsequent calls made to the API were successful.对 API 的所有后续调用均成功。

My suspicion is that the lambda was not warm and that resulted a timeout.我怀疑 lambda 不热,导致超时。 I am going to try setting a timeout for the axios code.我将尝试为 axios 代码设置超时。 any suggestions welcome欢迎任何建议

async getItems(): Promise < any > {
  try {

    let url = `https://vpce-[id].execute-api.ap-southeast-2.vpce.amazonaws.com/prod/items`


    const response = await axios.get(url, {
      headers: {
        'Authorization': `Bearer ${token}`,
        'x-apigw-api-id': `[api-id]`
      }
    });

    return response.data;

  } catch(error) {
    console.log(error);
    throw error;
  }
}

Turns out my lambda is timing out after the 30 seconds configured time.结果我的 lambda 在 30 秒配置时间后超时。 I could increase the lambda timeout, but the configurable timeout for API gateway is 30 seconds.我可以增加 lambda 超时,但 API 网关的可配置超时为 30 秒。

It has only happened once and i believe that it's because lambda cold start.它只发生过一次,我相信这是因为 lambda 冷启动。 As a workaround, I am taking the retry approach.作为一种解决方法,我正在采用retry方法。 The API request will be retried 3 times. API 请求将重试 3 次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM