Background
I am playing with https://github.com/arnoudbuzing/wolfram-server and I faced problems with requests that are slightly longer. I tried to narrow it down here.
Problem
This could be my failure to grasp sockets world, in such case I apologize. But I don't understand why SocketReadyQ
returns False
while it can be read?
SocketListen[
SocketOpen["127.0.0.1:5432"],
Function[message,
Print["Msg//Short: ", Short @ message["Data"]];
Print["SocketReadyQ: ", SocketReadyQ @ message["SourceSocket"]];
Print["Read more anyway: \n", Short @ ByteArrayToString @ SocketReadMessage @ message["SourceSocket"]];
WriteString[message["SourceSocket"], ExportString[GenerateHTTPResponse[HTTPErrorResponse[500]], "HTTPResponse"]];
Close@message["SourceSocket"]]
]
URLRead[HTTPRequest[
"http://127.0.0.1:5432", <|"Headers" -> {"Expect" -> ""},
"Body" -> StringRepeat["a", 10000]|>]
];
Msg//Short: POST / HTTP/1.1 Host: 127.0.0.1:5432 Accept: */... aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
SocketReadyQ: False
Read more anyway: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ... aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
So the problem is I am trying to handle a bigger request but it is split. I can't read blindly because SocketReadMessage will freeze when there's no more data and the point is to respond quickly.
If I Close
then the next listener call will capture the next part of the original data but that does not help because it would be cumbersome to assemble and respond to client across many messages.
Question
Did I miss something? What is a proper way to handle long requests?
Notes
I tried to SocketReadMessage
wrraped with TimeConstrained
as suggested, for the old api, here: https://mathematica.stackexchange.com/a/80053/5478
But the constraint needs to be at least 0.5s to be able to read if there's full buffer. So in case of all those short requests I will be adding 0.5s just to check if they are complete. That is too much.