Can a request be handled and ended prematurely, early in the pipeline? - asp.net

I have an HttpModule that has bound an event handler to EndRequest.
Is there any way to handle the request inside the event handler? Meaning, I don't just want to run code and keep the request moving -- I want to stop it dead in its tracks, return a 200 Status Code, and call it a day, without it request continuing to the next step in the pipeline.

HttpContext.Current.ApplicationInstance.CompleteRequest();
Documentation

Related

How do I make a Vertx handler execute earlier in eventloop?

I'm using Vertx 3.5.0 and very new to it. I'm trying to cancel the code execution when a client cancels their request.
Currently it's setup to where the first thing we do is deploy a verticle to run an HttpServer, and we add all of our Routes to the Router. From here we have a handler function per route. Inside this handler I'm trying this:
routingContext.request().connection().closeHandler({
//execute logic for ending execution
});
This is the only method I've seen that actually catches the closing of the connection, but the problem is it doesn't execute the handler early enough in the eventloop. So if I have any logs in there it will look like:
...[vert.x-eventloop-thread-0].....
...[vert.x-eventloop-thread-0]..... (Let's say I cancelled the request at this point)
...[vert.x-eventloop-thread-0].....
...[vert.x-eventloop-thread-0]..... (Final log of regular execution before waiting on asynchronous db calls)
...[vert.x-eventloop-thread-0]..... (Execution of closeHandler code)
I'd like for the closeHandler code to interrupt the process and execute essentially when the event actually happens.
This seems to always be the case regardless of when I cancel the request so I figure I'm missing something about how Vertx is handling the asynchronousity.
I've tried executing the closeHandler code via a worker verticle, inside the blockingHandler from the Router object, and inside the connectionHandler from the HttpServer object. All had the same result.
The main code execution is also not executed by a worker verticle, just a regular one.
Thanks!
It seems you misunderstand what a closeHandler is. It's a callback method, that Vert.x invokes when the request is being closed. It is not way to terminate the request early.
If you would like to terminate request early, one way is to use response().close() instead.
As a footnote, I'd like to mention that Vert.x 3.5.0 is 4 years old now, and you should be upgrading to 3.9, or, if you can, to 4.0

notify controller action method from other independent handler within specific time

I have a situation where I need to wait for response from device(using mqtt broker which doesnt matter in current questions context).
Whenever I get an API call on one specific endpoint
I need to wait(2-5 seconds depending upon the need) for response from device on the other handler(mqtt handler => https://github.com/gausby/tortoise)
this handler needs to notify me somehow I got this msg(if handler received msg withing that time) for the particular device id
if device matches and controller action method get notified we send back success response otherwise we send failure response
Any msg received before or after wait time doesnt matter(just consider it unsubscribed)
I am not really sure about whats the best way to achieve above requirement. any help is welcome, thanks
spawn() a process for the first handler. In the first handler, spawn() another process for the second handler passing self() as one of the arguments. Then enter a receive clause with a 2-5 second timeout specified in the after clause. Have the second handler send() a message to the first handler with the data that the second handler acquires.
If the receive in the first handler times out, then do whatever you want to do in the after clause, if the receive reads a message before it times out, then do whatever you need to do with the data.
Then, if you let the process running the first handler die, then you won't have to worry about junk messages in its mailbox.

Set time out for a web response from client side

I am calling a webservice to send sms to clients from my ASP.NET web app. Sometimes this webservice takes too much time to return a response, and this is causing problems with the next functions called in my app. So is there any way to add time out for the response from my app? In other words: can I add a time out so whenever the response time exceeds time out, my app continue working instead of waiting for a response?
How do you make the request ... HttpWebRequest? If this is the case you have a timeout property - https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.timeout(v=vs.110).aspx
In case you don't care about the response, you can go with "Fire and forget" approach. You can find more information here - Simplest way to do a fire and forget method in C#?

How to Kill Processes on hitting a button flex spark

When I send request to backend(java) its taking time to get the response, so If I hit cancel button, it only removing my title window but the process is still going on. So, after hitting cancel button how to stop Listening to the response immediately?
If remoteObject is using then we can cancel listening to the response by this small line
remoteObject.getOperation("serviceYouWantToStop").cancel();
You can stop listening for a response by removing the event listeners for the response. How you do this depends on the service you're using and how you call it, but generically like this:
myService.removeEventListener(ResultEvent.RESULT, myResultHandler);
myService.removeEventListener(FaultEvent.FAULT, myFaultHandler);
You should consider editing your question to provide those details. To actually stop the RemoteObject call, you can use the cancel() method of HTTPService; the disconnect() method on RemoteObject and a disconnect() method on WebService

Understanding how netty works

I'm trying to understand how netty works, and after reading some of the documentation I was to see if I understood how things work at a high level.
Basically netty has an event cycle, so whenever you make a call it gets serialized and the request gets pushed down to the o/s level, and it uses epoll and waits for an event to send back to netty.
When the operation system generates an event that netty subscribed to, netty then has an event loop that gets triggered.
Now the interested part here is, the event that gets triggered has to be parsed, and the client code (or custom code) has to figure out who actually this event is for.
So for example, if this was for a chat application, when a message is sent, it is up to the client code to figure out to send this message via ajax to the correct user.
Is this, at a high level, a correct overview of how netty works?
BTW, when netty listens for events sent via epoll, is this event loop single threaded or does it work from a pool of threads?
Sounds correct to me.
There are more than one event loop thread in Netty, but it does not mean a single Channel's event is handled by multiple event loop threads. Netty picks one thread and assigns it to a Channel. Once assigned, all events related with the Channel is handled by the picked thread.
It does not also necessarily mean that an event loop thread handles only one Channel. An event loop thread can handle multiple Channels.

Resources