You are not logged in.
Using THttpAsyncServer or THttpServer, in the OnRequest event handler function, how do I know whether the Client Socket is still connected? For example, the Client crashes after initiating many parallel requests, or other similar situations. I found that if a large number of concurrent requests flood into the server instantly, they will be placed in the PendingQueue and then processed by the thread pool, even if these subsequent connections have been Timeout or Closed.
Offline
Are you sure?
With THttpAsyncServer, if the connection is marked as closed no data is read, so nothing is processed.
See
https://github.com/synopse/mORMot2/blob … .pas#L2032
and
https://github.com/synopse/mORMot2/blob … .pas#L2103
So the pending queue is still there, put very quickly processed.
Offline
I don't know if there is something wrong with me here. You can reproduce the situation on my end like this:
1. Use the official example httpServerRaw, only modify DoRequest to add some delay
function TSimpleHttpAsyncServer.DoOnRequest(Ctxt: THttpServerRequestAbstract): cardinal;
...
// POST = echo
Ctxt.OutContent := Ctxt.InContent;
Ctxt.OutContentType := TEXT_CONTENT_TYPE;
Writeln(Ctxt.ConnectionID, Ctxt.InContent);
Sleep(5000);
...
2. In TSimpleHttpAsyncServer.Create, use a thread pool of only 1 thread:
fHttpServer := THttpAsyncServer.Create(
'55555', nil, nil, '', 1, 30000,
[hsoNoXPoweredHeader,
hsoNoStats,
hsoHeadersInterning
hsoThreadSmooting
//, hsoLogVerbose
]);
3. On the client side, simply create 100 threads to send requests using TSimpleHttpClient:
procedure TtestHttpRequestThread.Execute;
...
FHttpClient := TSimpleHttpClient.Create(True);
Response := FHttpClient.Request(Uri, 'POST', Headers, Format(Params, [FID]), mimetype, 0);
Response := FHttpClient.Request(Uri, 'POST', Headers, Format(Params, [FID]), mimetype, 0);
FHttpClient.Free;
4. When the sending request is complete, press Ctrl-C to exit the client program.
On the server side, you can see that the server has finally processed all 100 requests.
Offline
No matter THttpAsyncServer or THttpServer, the result is the same .
Offline
I guess it may be a "feature".
Imagine you send some POSTs to the server. If the client closes too soon, it is the client problem.
If the server is stuck because the process takes too much time - like a Sleep(5000) - it is a server issue anyway: the server should switch to an asynchronous mode.
To protect from DOS attacks, any almost-not-instant process should be authenticated from a trusted client (e.g. with a JWT), if possible in the OnBeforeBody() event.
I just checked the nginx sources, and AFAICT they do the same as we do.
Offline
On which operating system?
Windows 11 64bit and Linux aarch64, server side;
Linux aarach64, client side.
Last edited by zen010101 (2025-01-09 11:02:32)
Offline
It sounds reasonable
Offline