如何在Vapor中处理取消的请求?

huangapple go评论74阅读模式
英文:

How to handle canceled request in vapor?

问题

我正在使用Vapor在一台Mac服务器上。服务器执行了长时间的计算,因此我需要允许客户端能够取消这个计算。

在客户端,我有以下的JavaScript代码来发送POST请求并等待结果:

async function doSomething(text) {
    controller = new AbortController()
    const response = await fetch("http://" + website + "/api/dosomething",
        {
            method: 'POST',
            signal: controller.signal,
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
        },
        body: JSON.stringify({ text: text })
        })
    const results = await response.json()
    return results
}

以及一个用于取消进程的按钮:

async function cancelquery() {
    controller.abort()
}

这在客户端上运行得很好,也就是说,当我点击取消按钮时,客户端不再等待结果。然而,我不知道如何在服务器端捕获“abort”信号以停止计算。在Vapor中是否有任何外部变量、方法或事件,我可以用来知道客户端发送了一个中止信号?

英文:

I am using Vapor on a mac server. The server makes lengthy computation so I need to allow the client to be able to cancel the computation.

On the client side, I have the following Javascript code to send a POST request and wait for the result:

async function doSomething(text) {
    controller = new AbortController()
    const response = await fetch("http://" + website + "/api/dosomething",
        {
            method: 'POST',
            signal: controller.signal,
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
        },
        body: JSON.stringify({ text: text })
        })
    const results = await response.json()
    return results
}

as well as a button to cancel the process:

async function cancelquery() {
    controller.abort()
}

This works fine on the client side, i.e., the client does no longer wait for the result when I click the Cancel button. However, I don't know how to catch the "abort" signal on the server side to stop the computation. Is there any external variable, method or event in Vapor that I could use to know that the client sent an Abort signal?

答案1

得分: 1

这绝对与服务器端无关,因为服务器不知道客户端是否仍在等待响应。

https://stackoverflow.com/questions/7506413/aborting-an-http-request-server-side-advantage

这里有一个类似的问题,但与Vapor不完全相关。

英文:

It is absolutely not related to server-side since server doesn't know if client still waiting for the response or not.

https://stackoverflow.com/questions/7506413/aborting-an-http-request-server-side-advantage

Here's similar question but not related to exactly Vapor.

答案2

得分: 1

我会调查将您的 'fetch' 提交到一个 eventLooppromise,但通过在应用配置键上存储一个指向它的指针来跟踪它,并将一个键传回给客户端。然后,您可以创建一个 'cancel' 路由,该路由使用该键检索此事件循环并调用 close()

我没有尝试过这样做,但这似乎是一个合理的起点。请参见此处。您可以通过添加一个 whenComplete 处理程序来允许正常终止以删除字典中的条目。

英文:

I would investigate submitting your 'fetch' to an eventLoop or promise, but keeping track of it by storing a pointer to it in a dictionary on an App Config Key and passing a key back to your client. You can then create a 'cancel' route that uses the key to retrieve this event loop and invokes close() on it.

I haven't played with doing this, but it seems a reasonable place to start. See here. You can allow for normal termination by adding a whenComplete handler to remove the entry in your dictionary.

huangapple
  • 本文由 发表于 2023年7月24日 17:09:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76752943.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定