#1 2025-01-08 18:30:52

zen010101
Member
Registered: 2024-06-15
Posts: 80

How to Detect Client Connection in THttpServer OnRequest Event?

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

#2 2025-01-08 20:09:26

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,793
Website

Re: How to Detect Client Connection in THttpServer OnRequest Event?

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

#3 2025-01-09 02:19:38

zen010101
Member
Registered: 2024-06-15
Posts: 80

Re: How to Detect Client Connection in THttpServer OnRequest Event?

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

#4 2025-01-09 02:21:38

zen010101
Member
Registered: 2024-06-15
Posts: 80

Re: How to Detect Client Connection in THttpServer OnRequest Event?

No matter THttpAsyncServer or THttpServer, the result is the same .

Offline

#5 2025-01-09 07:59:13

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,793
Website

Re: How to Detect Client Connection in THttpServer OnRequest Event?

On which operating system?

Offline

#6 2025-01-09 08:40:44

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,793
Website

Re: How to Detect Client Connection in THttpServer OnRequest Event?

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

#7 2025-01-09 10:48:46

zen010101
Member
Registered: 2024-06-15
Posts: 80

Re: How to Detect Client Connection in THttpServer OnRequest Event?

ab wrote:

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

#8 2025-01-09 10:51:33

zen010101
Member
Registered: 2024-06-15
Posts: 80

Re: How to Detect Client Connection in THttpServer OnRequest Event?

It sounds reasonable tongue

Offline

Board footer

Powered by FluxBB