Repository navigation
|
Currently one needs to track for aborted requests using something like (taken from examples): const ref = {aborted: false};
res.onAborted(() => {
ref.aborted = true;
});It is great to have I'm curious if res.onAborted = () => res.aborted = true;BTW, this project is awesome 🤘 |
Replies: 16 comments
|
you should use the response object so you don't need to create new object: res.onAborted(() => res.aborted = true) |
|
The whole point of this project is to get people to swallow more of C++ and less of JavaScript. That's why it's faster, because it does exactly the opposite of what a scripter would do. You're not supposed to set aborted=true. That's like the most lazy and half-assed thing to do. What you should do is deterministically alter your data structures and abort things in flight. After onAborted is called your program should keep no reference to the socket whatsoever. So you don't even need to set anything if you do it properly. This is the C++ way. |
|
I assume he wants to track the aborted status so can prevent responding to the request after doing some async: res.onAborted(() => res.aborted = true)
// do some async
!res.aborted && res.end('html')That is how I use it, seems to work good, this is ok? |
|
I don't care but I would never do that. In fact you cannot do that with the C++ version it would just segfault since it is illegal. The only reason it works in JavaScript is because the JavaScript object survives until garbage collection. There is no garbage collector in C++ so any solution that depend on having one is nonsensical since this is a C++ project. |
|
he needs to use the response object so no extra object created: @streamich wants it set automatically but I think it is fine to set it manually for only when you need it, 1 small line of code See example: https://github.com/uNetworking/uWebSockets.js/blob/master/examples/AsyncFunction.js#L17 |
|
Thanks, I just wanted to see you opinion. |
|
Yes the example sets aborted=true but this is the most basic thing and a proper cancel/abort of your async tasks in the very onAborted would be better since you immediately abort work instead of letting it succeed then ignore it. If you don't need work done then abort the work immediately. Don't let a worker do work you're going to ignore in the future anyways. To me this is obvious. |
|
Make it hard to do the wrong thing, make it easy to do the right thing. Yes, I could make it easier to do the wrong thing but I don't want to because it is wrong. In fact, you don't even need to set aborted=true anyways, because it will throw if you respond to something that is aborted so what you want is already implemented if you rely on try/catch instead of if(!res.aborted). You should of course NOT do this, but you CAN if you want to. |
|
I don't think you can 'cancel' an async promise/callback, it will get called and you might as well set res.onAborted(()=>res.ab=true)
await doAsync()
// check for and handle aborted response
!res.ab && res.end('html')seems to work fine, how would you do it differently? |
|
Promise rejection throws for any await, so you wrap it in try/catch. onAborted would execute something that makes the thing reject. That is in my opinion very nice, but most people probably don't care. I wouldn't care if the async thing is lightweight but imagine if it isn't. Then onAborted callback is very useful. |
|
ok you are saying use it to reject the async promise early if the response is aborted, I did example for that: uws.App().get('/',(res,req)=>{
doAsync(res).then(val=>res.end(val),e=>e!='aborted'&&res.writeStatus('500').end(e.toString()))
})
const doAsync=res=>new Promise((resolve,reject)=>{ // async function rejects early on abort
const t=setTimeout(()=>resolve(new Date().toString()),5000)
res.onAborted(()=>{clearTimeout(t);reject('aborted')})
})good to have options available 👍 |
|
You're still missing clearTimeout in that example to actually cancel the async work. |
|
ok I updated that |
|
Ignore client abort - this some case by design (business logic) For example, the client has upload images, but does not want to wait for finish processing and terminates the connection, this does not mean that we should terminate the processing of his image, we should always complete the processing and if there is no connection, do not send a response |
|
yes depends on the case, good to have options on how to handle it, I will save the Promise reject method for use if needed |
|
An even more revealing case: The user sends a correct payment request, server accept, start transaction, then abort client connection |
Yes the example sets aborted=true but this is the most basic thing and a proper cancel/abort of your async tasks in the very onAborted would be better since you immediately abort work instead of letting it succeed then ignore it. If you don't need work done then abort the work immediately. Don't let a worker do work you're going to ignore in the future anyways. To me this is obvious.