KOA API服务器 - 等待处理之前的请求,然后处理新请求

发布于 2025-01-26 21:31:36 字数 890 浏览 3 评论 0原文

我正在使用KOA建立一个API,该API使用另一个API处理一些信息。我的API来自客户端,我的API向另一个API提出了一些不同的请求。问题是,另一个API易碎且缓慢,因此为了确保数据完整性,我必须在开始新的过程之前检查是否处理过以前的传入请求。我的第一个想法是使用承诺和全球布尔值来检查是否进行了正在进行的处理并等待该过程完成。不知何故,这可以防止并发请求,但是即使在此过程中出现了3-4个请求,也只完成了第一个请求,就是这样。为什么其余的传入请求被遗忘了?

编辑:作为附带说明,我不需要响应带有处理的信息的传入请求。收到请求后,我可以立即发送响应。我需要使用第三方API进行操作。

到目前为止,我的解决方案: 入口点:

  router.get('/update', (ctx, next) => {
    ctx.body = 'Updating...';
    update();
    next();
  });

和更新功能:

let updateInProgress = false;

const update = async () => {
  const updateProcess = () => {
    return new Promise((resolve, reject) => {
      if (!updateInProgress) {
        return resolve();
      } else {
        setTimeout(updateProcess, 5000);
      }
    });
  };
  await updateProcess();
  updateInProgress = true;
  // Process the request
  updateInProgress = false
}

I'm building an API in Node with Koa which uses another API to process some information. A request comes in to my API from the client and my API does some different requests to another API. Problem is, the other API is fragile and slow so to guarantee data integrity, I have to check if there is no previous incoming request being processed, before starting a new process. My first idea was to use promises and a global boolean to check if theres an ongoing processing and await until the process has finished. Somehow this prevents concurrent requests but even if 3-4 requests come in during the process, only the first one is done and that is it. Why are the rest of the incoming requests forgotten ?

Edit: As a side note, I do not need to respond to the incoming request with processed information. I could send response right after the request is recieved. I need to do operations with the 3rd party API.

My solution so far:
The entry point:

  router.get('/update', (ctx, next) => {
    ctx.body = 'Updating...';
    update();
    next();
  });

And the update function:

let updateInProgress = false;

const update = async () => {
  const updateProcess = () => {
    return new Promise((resolve, reject) => {
      if (!updateInProgress) {
        return resolve();
      } else {
        setTimeout(updateProcess, 5000);
      }
    });
  };
  await updateProcess();
  updateInProgress = true;
  // Process the request
  updateInProgress = false
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丿*梦醉红颜 2025-02-02 21:31:36

好吧,我找到了一个工作解决方案,不确定它有多优雅...
我猜这个问题是,超时功能是创造了新的诺言,另一个是一个,另一个直到解决了一个诺言。这并不能解决第一个承诺艰难的诺言,并且代码被卡住了。解决方案是创建一个间隔,该间隔检查是否满足条件,然后解决承诺。如果有人聪明可以发表评论,我将不胜感激。

let updateInProgress = false;

const update = async () => {
  const updateProcess = () => {
    return new Promise((resolve, reject) => {
      if (!updateInProgress) {
        return resolve();
      } else {
        const processCheck = setInterval(() => {
          if (!updateInProgress) {
            clearInterval(processCheck);
            return resolve();
          }
        }, 5000);
      }
    });
  };
  await updateProcess();
  updateInProgress = true;
  // Process the request
  updateInProgress = false
}

Ok, I found a working solution, not sure how elegant it is tough...
I'm guessing the problem was, that new Promise was created with the Timeout function, and another one, and another one until one of them was resolved. That did not resolve the first Promise tough and the code got stuck. The solution was to create an interval which checked if the condition is met and then resolve the Promise. If someone smarter could comment, I'd appreciate it.

let updateInProgress = false;

const update = async () => {
  const updateProcess = () => {
    return new Promise((resolve, reject) => {
      if (!updateInProgress) {
        return resolve();
      } else {
        const processCheck = setInterval(() => {
          if (!updateInProgress) {
            clearInterval(processCheck);
            return resolve();
          }
        }, 5000);
      }
    });
  };
  await updateProcess();
  updateInProgress = true;
  // Process the request
  updateInProgress = false
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文