Handle stream timeouts properly, for slow HTTP/HTTPS links (#3752)

This patch fixes update timeouts (error #6) on slow HTTP/HTTPS links.
This commit is contained in:
Denys Fedoryshchenko 2020-11-02 22:01:27 +02:00 committed by GitHub
parent 76afaf2d6b
commit e4b008e712
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -323,6 +323,8 @@ size_t UpdateClass::write(uint8_t *data, size_t len) {
size_t UpdateClass::writeStream(Stream &data) { size_t UpdateClass::writeStream(Stream &data) {
size_t written = 0; size_t written = 0;
size_t toRead = 0; size_t toRead = 0;
int timeout_failures = 0;
if(hasError() || !isRunning()) if(hasError() || !isRunning())
return 0; return 0;
@ -344,15 +346,24 @@ size_t UpdateClass::writeStream(Stream &data) {
bytesToRead = remaining(); bytesToRead = remaining();
} }
toRead = data.readBytes(_buffer + _bufferLen, bytesToRead); /*
if(toRead == 0) { //Timeout Init read&timeout counters and try to read, if read failed, increase counter,
delay(100); wait 100ms and try to read again. If counter > 300 (30 sec), give up/abort
toRead = data.readBytes(_buffer + _bufferLen, bytesToRead); */
if(toRead == 0) { //Timeout toRead = 0;
_abort(UPDATE_ERROR_STREAM); timeout_failures = 0;
return written; while(!toRead) {
toRead = data.readBytes(_buffer + _bufferLen, bytesToRead);
if(toRead == 0) {
timeout_failures++;
if (timeout_failures >= 300) {
_abort(UPDATE_ERROR_STREAM);
return written;
}
delay(100);
} }
} }
if(_ledPin != -1) { if(_ledPin != -1) {
digitalWrite(_ledPin, !_ledOn); // Switch LED off digitalWrite(_ledPin, !_ledOn); // Switch LED off
} }