Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slow WiFiClient after commit "ClientContext (tcp) updates #5089" #5170

Closed
mongozmaki opened this issue Sep 26, 2018 · 2 comments · Fixed by #5172
Closed

Slow WiFiClient after commit "ClientContext (tcp) updates #5089" #5170

mongozmaki opened this issue Sep 26, 2018 · 2 comments · Fixed by #5172
Assignees

Comments

@mongozmaki
Copy link
Contributor

mongozmaki commented Sep 26, 2018

Hi!

I realized that after the last commit (ClientContext (tcp) updates (#5089)), my Webserver was very slow while sending big files.
I looked at the changes and it seems that ClientContext::_write_some() causes the problem.
My suspicion is, that the if-condition in line 510 should be reverted so that tcp_output is always called.
If I set WiFiClient::setDefaultNoDelay(true) at the beginning of my program, it works.

Possible solution:
ClientContext::_write_some (line 510):

Change

   if (has_written && (_sync || tcp_nagle_disabled(_pcb))) {
      ...
      tcp_output(_pcb);
    }

to

   if (has_written) {
      ...
      tcp_output(_pcb);
    }

Testcode:
Compile and visit IP address in webbrowser.
Toggle variable NO_DELAY to see effect.

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

const char* ssid = "***";
const char* password = "***";

constexpr size_t HUGE_PAGE_SIZE = 32 * 1024;
constexpr bool NO_DELAY = false;

ESP8266WebServer server(80);

void setup(void) {

    WiFiClient::setDefaultNoDelay(NO_DELAY);
   
    Serial.begin(115200);

    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);
    Serial.println("");

    // Wait for connection
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("");
    Serial.print("Connected to ");
    Serial.println(ssid);
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());

    server.onNotFound([]() {
        const String html = "<html><header><title>A test</title></header><body>A huge page</body></html>";
        String hugePage;
     
        hugePage.reserve(HUGE_PAGE_SIZE+html.length());
        for (size_t i = 0; i != HUGE_PAGE_SIZE; ++i) {
            hugePage += ' ';
        }

        hugePage += html;
        Serial.print("Sending huge page ");
        Serial.print(NO_DELAY?"WITHOUT": "WITH");
        Serial.print(" Nagle Algorithm...");
        ulong t1 = millis();
        server.send(200, "text/html", hugePage);
        ulong t2 = millis();
        Serial.print("done. Time: ");
        Serial.print(t2-t1);
        Serial.println("ms");
    });
    
    server.begin();
    Serial.println("HTTP server started");
}

void loop(void) {
    server.handleClient();
}

Debug Messages

Sending huge page WITH Nagle Algorithm...done. Time: 15542ms
Sending huge page WITHOUT Nagle Algorithm...done. Time: 318ms
@d-a-v
Copy link
Collaborator

d-a-v commented Sep 26, 2018

Thanks for this nice MCVE !
You are indeed right and after checking again, tcp_output() does not break Nagle as I was thinking.
Fix is on its way

@mongozmaki
Copy link
Contributor Author

No Problem! Glad to help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants