I have a HttpServer based on TROHttpServerApiServer.
When testing the configuration “localHttpServer + localHttpClient”, I met strange delays (15-20ms) even for dummy calls that “do nothing”.
My further research showed that the reason is in TROHttpThread.IntExecute:
1 Removing the sleep(1) resolves the issue
procedure TROHttpThread.IntExecute;
begin
...
else
break; // unknown error
end;
// sleep(1); // <--- The patched line
end;
finally
FreeMem(lRequestBuffer);
end;
end;
As I see the idea of the sleep(1) is to switch execution from the current thread. Q1: Does removing the sleep(1) break something?
2 As well I noticed strange sleep(100) in
ERROR_IO_PENDING: begin
// If the function is being used asynchronously, a return value of ERROR_IO_PENDING
// indicates that the next request is not yet ready and will be retrieved later
// through normal overlapped I/O completion mechanisms.
sleep(100);
end;
I don’t see the reason why to sleep here if the call
Q1: Why should the execution yield to another thread here? As I see the execution is blocked by ReceiveHttpRequest when there is no request in the internal queue, doesn’t it?
if GetCurrentThreadID <> MainThreadID then sleep(100);
Q2: Given that in ~100% of times the code is executed in non-main thread this change does nothing. For what the sleep is required here?
I’m still not convinced that all these sleep(1)/Yield/... is a good approach to co-working the server and its threads.
IMO using of Sync objects in TROHttpServerApiServer.UpdateThreads and TROHttpThread.IntExecute to manage threads would be more robust and much better in the terms of performance.
No, I haven’t because it’s impossible. HttpServerAPI.ReceiveHttpRequest() in TROHttpThread.IntExecute is always called synchronously and it doesn’t make sense to handle ERROR_IO_PENDING here.
I think this case could be removed at all to avoid confusing.