In a Web API call, if the server's reply didn't get to the client, will the server know? - asp.net-web-api

A client, such as a C# program, invokes a server via its web API. The server process that is invoked does some database changes, then returns some data to the client, and the web API call finishes.
Suppose the response fails on its way to the client - for example the client dies and therefore the response can't be transmitted to the client.
In this case, will the server process get an exception from the operating system, so that it can rollback the database changes, or will the server happpily assume that the client has received the response, and commit the database changes?
The reason I am asking this, is that I don't want the server's database to be in a state that believes that the client has got the response, but the client hasn't. This will result in lost of sync between the client devices and the server.

Related

gRPC C++: Which is the method to be used for notifying the grpc client about server disconnect

I'm writing a client which communicates with multiple server via gRPC. The data transfer works absolutely fine but I'm getting an exception on client side when one of the server stops abruptly. I would like to know if there is any error handling in gRPC library which can handle this server disconnection and do a clean exit from client side.
I can see methods like bool IsCancelled() const which could be used on server side to get status about client disconnection.
I need a similar method to be used on client to get notified about server disconnection.

Backend to Frontend Websocket

I need to add support for instant messages or reminders to my web application. I was reading that this could be accomplished with websockets.
The idea is that during the time the web app is been used, it could receive messages originated (not as a request response) from the server. For example, the server application might want to remind the user about and unpaid service.
As I understand, when the web app starts it connects to the websocket server through a standard HTTP Request call to announce itself as a client. My question is:
"If I have hundreds of clients connected at the same time, how do I call one in particular?"
Do I need to store every websocket object in an array or something so I can use it to send a message when it is required?
What would be the right approach?
Thanks.

Understanding HTTP2 server push and multiplexing

From what I understand, what multiplexing means is that the client just need to create one TCP connection with the server and it can send multiple requests at the same time without having to wait for the response of one request before continuing another. So if I send 3 request at the same time, there are also 3 response streams.
And for server push, the client sends one request to the server, the server then guesses that the client needs other resources (also called promises) other than the one it requested, so it sends push promise streams hinting the client with the URL of the additional resources. The client may choose to request those additional resources or not.
My questions are:
For any response sent from the server to the client, does it have to be a request initiated first? I mean, it I created a connection to
the server, I did not send any request. Could I be getting responses
from server via server push? In multiplexing, I get same number of
responses for same number of requests. In server push, I can get
multiple responses for one request. So does there always have to be a
request first?
In server push, when a promise stream is sent by the server to the client containing the URL of the additional resources, does that mean
the server will only push the additional resources only when the
client accepts the promises?
[the server] sends push promise streams hinting the client with the URL of the additional resources. The client may choose to request those additional resources or not.
This is not correct. When the server sends to the client a PUSH_PROMISE, the server will then send the resource content associated to that pushed resource.
The only thing that the client can do is to reset the pushed stream via a RST_STREAM frame, but it may well be that the whole pushed resource is already in-flight so resetting the pushed stream has no effect: the client will receive the pushed resource bytes and may discard them if not interested.
To answer your specific questions:
Yes, a response from the server is always client-initiated. If a client does not send any request to the server, the server cannot push to the client. Even in the case of server push, the client always initiated a stream by making a request, and server pushes are always associated to that "parent" request.
The PUSH_PROMISE frame is an indication from the server to the client of what resource the server is about to push. The client does not "accept" pushes, the server forces them to the client. The only thing the client can do is to reset the stream associated with the pushed resource; as I said, the server may have already pushed the whole resource by the time it receives the RST_STREAM frame from the client.

What happens if UI makes a rest call and the server stops?

Suppose, UI is making calls to rest service and the server stops.
Does UI come to know about the server's state?
Does UI get any response back from the server?
What difference does it make if the rest call was POST or PUT?
When REST Api is stopped,the UI/Browser/Client will show "Could not get any response
There was an error connecting to " error, irrespective of request type: PUT /POST.
To answer your questions :
Does UI come to know about the server's state?
If Server stops, you will get an error like : error connecting server. Which can be traced to multiple rootcauses. One of them can be : Server has stopped.
In case of error, Server's state can be sent to client using various error codes like:
500 Internal Server Error The 500 status code, or Internal Server Error, means that server cannot process the request for an unknown reason. Here is list of status codes for REST APis: https://www.w3.org/Protocols/HTTP/HTRESP.html
Does UI get any response back from the server?
If server is stopped, UI/Client will receive no response.
What difference does it make if the rest call was POST or PUT?
If the server has stopped, it doesn't make any difference.
You can run this scenario using a browser and running any app/REST service in your local machine.
(For this test you need not have any REST application running in your local, as you only want to test when it is stopped)
For instance, if your REST application server is up and available at port 8080, you can send request to this server by sending request form your browser : http://localhost:8080
For testing with POST/PUT collections, you can use any API Development tools like : Postman.
Let's assue your REST Api , exposes following urls :
/myPostRequest POST
/myPutRequest PUT
When you hit these urls from POSTMAN you get the same response :
POST http://localhost:8080/myPostRequest :
Could not get any response
There was an error connecting to http://localhost:8080/transition-order.
PUT http://localhost:8080/myPutRequest :
Could not get any response
There was an error connecting to http://localhost:8080/transition-order.
Client(could be UI) and Server are two dumb applications. They do and say what they are told to.
So whenever a client tries to call a server on a socket and there is no server listening to it, there is no response returned to the client. The client can interpret it in whatever way it wants. Most of the browsers show <host> refused to connect.
But if a server is listening to a socket, it responds to the client whichever way it was programmed to do. The server sends an appropriate status code and expects that the client knows how to interpret it. A server can send any random number it wants... the catch is, the client must know how to interpret it. otherwise, it's just one-way communication.
In order to provide smooth client-server communication, section 10 of RFC 2616 defines various codes with specific meaning as shown here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
These status codes are the same for all the types of Http methods.

heroku router timeout/interrupt causing lost responses

I have what appears to be a race condition related to losing responses coming from my heroku web service.
The heroku router delivers the request to the web service, the web service processes the request and returns a response, but in the interim the heroku router fails the request, either due to client (interrupt) or backend timeout.
The problem is that the web service request processing changed state on the backend and expected to send the state change to the client in the body of the response. The response never gets to the client, therefore the state change is lost forever.
The state change in my case happens to be the delivery and removal of a message from a RabbitMQ message queue. The web service request handler pops the request from the RabbitMQ queue, but it fails to reach the client and is never heard of again.
I could implement my own client-based message ACK system to mitigate this. However, I suspect that some of you might have a better solution regarding how to deal with ensuring that the responses get to the client. Is there any callback that I can use on my web service to determine if the response was lost? FWIW my web service is a JAX-RS service running embedded Jetty.
Thanks!

Resources