douhuangzhi0707 2018-03-02 16:56
浏览 103

一次处理多个GET请求

I'm writing this API which is a backend for slack bot application which will be used by many users. In one of the API endpoints in my api I'm calling an external API to get some data so I can massage it in my API to send it to the BOT/User. But that external API call I'm making is very expensive (~5 / ~10 secs). I've noticed that when I hit my API endpoint two or more times at the same time sometimes I don't get any data. Only one of the calls becomes successful. Is there any way that I can fix this? I've been looking into worker queue but not sure if its overkill for this.

I'm using mux to do my routing.

  • 写回答

1条回答 默认 最新

  • drblhw5731 2018-03-02 17:00
    关注

    If you want to handle requests more quickly than the external API does, you'll need to separate your outbound calls from the incoming requests and do some sort of caching. For example, you can:

    • Run a worker that calls your external API and stores the results in a cache
    • Have you request handler pull from the cache
    • If the data is recent, return it
    • If the data is too old, wait for the cache to be refreshed, and use the new data

    If this is a 24/7 busy service, the worker can just refresh the cache on a repeating schedule with whatever frequency is necessary; or, you can have the request handler queue a message to the cache worker signalling that the cache needs to be refreshed so that when you're not getting requests, it won't keep hitting the external API.

    评论

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线