nmattisson / HttpClient

Http Client Library for the Spark Core (also well suited for Arduino and other embedded platforms).
Other
121 stars 150 forks source link

TCP connection seems to block until timeout #28

Open yerpj opened 8 years ago

yerpj commented 8 years ago

Hi,

I don't know if this is the best place to share my thoughts, but I didn't find any Particle room on Gitter. I am playing with the HttpClient in order to POST some data from my Particle Core to a nodejs http server on my local network. Globally it works. But It seems that the http.post() method is blocking until timeout. I think it could come from the line 237 of HttpClient.cpp (}while (client.connected() && !timeout && !error);). Let me explain why:

When in local network, the server could potentially respond to a request very quickly. Furthermore, it is stated in Particle reference that client.connected() returns TRUE if the socket is still open OR if there is any data remaining in the RX buffer. So if the server answers quickly, due to line 235 delay(200);, the client is likely to get some data BEFORE client.connected() is called. In my opinion, this could lead to block the http.post request in the do{}while() loop UNTIL timeout. Thereby, I am experiencing this behaviour right now. The POST request is sent in less than 100ms, then the server answers in less than 2ms. After that, I see some traffic (with Wireshark) sur as [PSH,ACK],[RST,ACK],as well as retransmissions for about 5.5 seconds. Finally, the client sends the [FIN,ACK] packet. I tried to address a distant server, the complete request-response is during max 2 seconds. I also looked at the Particle forum, nobody seems to talk about that issue, maybe I'm totally wrong and misunderstood the TCP protocol. On demand, I can explore further.

Here is the wireshark log: http://s000.tinyupload.com/?file_id=09753784621306542778

yerpj commented 8 years ago

I am not sure anymore that this delay is the cause of my problem. I activated LOGGING, and this is what I got from the Http Client:

HttpClient>  Connecting to IP: 192.168.1.125:80
HttpClient>     Start of HTTP Request.
POST /wifispark HTTP/1.0
Connection: close
Content-Length: 19
Content-Type: application/json
Accept: */*
Connection: Close
{"humidity":"43.5"}
HttpClient>     End of HTTP Request.

HttpClient>     Receiving TCP transaction of 85 bytes.
HTTP/1.1 200 OK
Date: Sat, 09 Apr 2016 10:54:46 GMT
Connection: close

Thank you!
HttpClient>     End of TCP transaction.
HttpClient>     Error: Timeout while reading response.

HttpClient>     End of HTTP Response (5700ms).
HttpClient>     Status Code: 200

So there surely happens something that cause a Timeout to occur. Does the HTTP server has to respond with a strictly formated body? Currently, the server only answers Thank you! when it correctly receives a JSON object by the POST method.

nmattisson commented 8 years ago

Hi Jean-Philippe,

We used Particle at Minut (www.minut.com) in the early prototyping days, and back then there didn't exist an HTTP Library—so I wrote this. Since moving to production we're no longer using Particle (but remain great fans!) which is why this library isn't as maintained as perhaps it should be.

There are still many users and the occasional bugfix, but for the most part discussion takes place in the Particle forums. I'd recommend posting your concerns there to get some feedback from people using this code more actively!

Cheers, Nils

yerpj commented 8 years ago

I just posted on the Particle forum: HERE

derrickrc commented 3 years ago

@yerpj did you ever figure this out? I'm experimenting with the same thing and haven't gotten any timeouts yet, but it does seem to block in general while either making the POST request or waiting for the response. I'd like to be able to continually make post requests without waiting for any kind of response