lasselukkari / aWOT

Arduino web server library.
MIT License
283 stars 41 forks source link

Request processing sometimes freezes... but not really #151

Open briandilley opened 1 week ago

briandilley commented 1 week ago

Sometimes I notice that request processing simply wont work, but then all of a sudden will process all of the requests quickly one after another. It's as if the web server doesn't see any requests, and they're queued up somewhere and then all of a sudden they come through. This can even happen with a single request. I'm judging this based on when my endpoint handler gets called. At the top of the handler i write something to the console (with ESP_LOGI(TAG, "...")). I'll see my browser make the network request for a single HTTP request and it will just sit there without a response for upwards of ~30 seconds before I see my log entry and the request is serviced. The endpoint handler itself seems to be sufficiently fast though - it's just getting to that that seems to take a while. The weird part is that nothing seems to be frozen or halted anywhere - because I see other stuff going on. The other weird thing is that this seems to be intermittent - it doesn't always act this way, but it's consistent enough to be a PITA.

Here's my processing code:

  if (this->wifiServer.hasClient()) {
    for (int i = 0; i < this->clients.size(); i++) {
      if (!this->clients[i].connected()) {
        this->clients[i] = this->wifiServer.available();
        this->timeToLive[i] = CLOCK.now() + LED_WEBSERVER_MAX_IDLE;
        break;
      }
    }
  }

  for (int i = 0; i < this->clients.size(); i++) {
    if (this->clients[i].available()) {
      this->webServer.process(&this->clients[i]);
      this->timeToLive[i] = CLOCK.now() + LED_WEBSERVER_MAX_IDLE;
    } else if (this->timeToLive[i] && this->timeToLive[i] < CLOCK.now()) {
      this->clients[i].stop();
      this->timeToLive[i] = 0;
    }
  }

and some other relevant settings:

#define LED_WEBSERVER_MAX_CLIENTS 16
#define LED_WEBSERVER_MAX_IDLE 5000
#define LED_WEBSERVER_HTTP_BUFFER_SIZE 4096

and an extremely simple example endpoint call that exhibits this behavior:

  webServer.post("/api/ping",
                 [](Request &req, Response &res) {
                   ESP_LOGI(TAG, "API Ping");
                   JSON_RESPONSE_OK();
                 });
lasselukkari commented 5 days ago

Sound like that for some reason the system is running out of free sockets or they are somehow blocked from reading or writing. If you don't have a specific reason to use keep alive and multiple open sockets I would suggest that you don't do that. The performance benefits are usually rather minimal while complicating things a a lot.

lasselukkari commented 5 days ago

This may be also somehow related to how some browsers open another connection to be ready before it's even being used. This may cause sockets to just hang there before the browser times them out. But as said before there is rarely a real need to maintain multiple open connections.

The library intentionally leaves the connection management to the user as it's a complicated platform dependant matter.