As shown in Abort DoJo XHR-Request it is possible to cancel a (dojo/request/xhr) http request on the Client.
The problem is, that the according server process (initiated by the request) is still running.
If we use a XMLHttpRequest-Object, calling abort() on it would stop the server process.
So the question is: Is it possible to achive this with dojo? Or is it possible to get access to the internal XMLHttpRequest-Object (used by dojo/request/xhr) to call abort() on it?
Related
I am working on an go application, that uses gin to serve the rest APIs.
I 4 of my handler functions for the APIs call a goroutine which makes a gRPC call. A strange thing i see, all the grpc calls for one of the handler fails with context cancelled while the other 3 succeed.
Looking at the logs, i noticed that my API returns before the goroutine is executed. Could it be possible that gin cancels the context after the API returns?
The context is propagated from the handler function to the goroutine through a couple of intermediate calls.
The error that i see is rpc error: code = Canceled desc = context canceled
How can i debug this? Does gin cancel the context once it returns the response?
Does gin cancel the context once it returns the response?
The docs for Request.Context() (gin uses http.Request) say:
For incoming server requests, the context is canceled when the client's connection closes, the request is canceled (with HTTP/2), or when the ServeHTTP method returns.
So, yes, the context is closed when your handler (effectively the ServeHTTP method) returns (see here). The context would also be cancelled if the connection dropped for another reason (network issue, or even your server shutting down).
The right way to handle this really depends upon what you want to achieve:
In many cases the correct behaviour is to cancel the RPC calls when the context is cancelled. For example if you are retrieving info needed to fulfil the request then cancelling those requests may save resources.
If you are triggering calls that must complete then pass in a context.Background() (or another context not directly tied to the request).
How can i debug this?
Look at why your handler is returning before all RPC calls complete; if that is what you want then you will need to use a different context (but remember this probably means you are returning an OK response code when you don't actually know yet if the entire process will be successful).
Here is the socket message I see in the browser debugger console:
More illustrative, perhaps:
I call an API operation that triggers this message over a socket.
What I Tried
To preclude inaccuracies, I started 2 instances of JMeter.
REST API call.
Revised version of the GitHub JMeter example of sockets.io, in which I just call a WebSocket Sampler repeatedly on wss://events.dev.myserver.com:443/socket.io/?EIO=4&transport=websocket.
I kicked off (2).
While that was running, I kicked off (1).
Expected
Eventually, (1) should show me a sampler in the View Results Tree with the message in the screenshot ("42" - GAME_STARTED)
Actual
The only messages I see look like this:
This is really all I want to do: run the appropriate sampler, a sufficient time after making the API call, to get the message.
Update
We succeeded in finding the message using python-socketio:
sio.connect("https://events.dev.server.com", transports='websocket',
headers={'Sec-WebSocket-Extensions: permessage-deflate', 'Sec-Fetch-Dest: websocket',
'Sec-Fetch-Mode: websocket',
'Cookie: ABCSESSIONDEV=NTI3MzkwNWUtMTJmNS00Y2U0LTk1NGUtMjQ2Mzk5OTYxZWE0'})
And here is the output:
Received packet MESSAGE data 2["message","{\"locationId\":110,\"name\":\"GAME_STARTED\",\"payload\":{\"id\":146724,\"boxId\":2002,\"userId\":419,\"createdAt\":\"2022-03-02T14:35:31\",\"lastModifiedAt\":\"2022-03-02T14:35:36.752\",\"completedAt\":\"2022-03-02T14:35:36.621\",\"activationMethod\":\"TAG\",\"nfcTagId\":\"xxxxxx\",\"gameCount\":1,\"app\":false}}"]
I would like to use the websocket plugin to do this in JMeter now.
tried adding Cookie to WebSocket call - only sids, no messages.
tried adding Cookie to an HTTPS request (like the above code) - 400, bad request.
Take a look at other fields of the HTTP Request, in particular HTTP Headers, most probably your JMeter request is missing some essential information.
My expectation is that in order to "start the game" (whatever it means) you need to open the page in the browser, authorize somehow, follow the steps of the protocol upgrade mechanism, etc. to wit exactly mimic what real browser does, all the request sequence which is prior to starting the game.
You might need to correlate dynamic parameters, add HTTP Header Manager, add HTTP Cookie Manager, etc.
1.Through the ajax, i sent a request to servlet when the OK button clicked.
2.That request processing some db call and holding some result sets
3.now, when i click on the CANCEL button,need to cancel/abort the request.
4.meanwhile, i need the resultsets which is holding by request.
There is no mechanism in the Servlet API (or any container specific mechanism of which I am aware) that allows an in progress HTTP request to be aborted (short of a kill -9 which is going to abort rather more than you wish in this case).
In, for example, JavaScript AJAX libraries it is possible to abort an AJAX request. Is there any server side advantage to this or is it just for client-side cleanliness? Is it part of TCP?
If, for example, I am requesting via AJAX a Python based server service – which is resource intensive – from my JavaScript Web App and abort this AJAX request, is it possible that aborting will ease the load on the server, or will the my ajax library just ignore the response from the server?
It does not affect the server-side if you use your frameworks abort feature. The server will still process the request regardless.
Once you made an HTTP request to a resource URL on your server (be it Asynch or not, aka ajax or "regular"), you can't abort it from your client / with another http request (unless your service has some weird listener that awaits for potential consequent http requests and stops itself up receiving one). My proposition, if you have one resource and time consuming operation, either split it into simpler operations, parallelize it or at east make some periodic responses to at least inform the user that it's still working and hasn't died
When people create real-time web apps, they are leaving a ajax request open/long running.
how do they do this in javascript?
There is really no difference from a normal ajax request. A callback is associated with the XMLHttpRequest. Once the request is complete the callback is invoked. The difference is on the server-side where the request is held open until data is ready for the client, or a timeout occurs. On the browser side, the callback is invoked as each successive request is answered. The callback must process the data from the server and initiate another request. The request is handled asynchronously, so the browser is not blocked.
A really good example of the whole thing is the chat demo included in Tornado.