Skip to content

Commit b8dec12

Browse files
authored
Limit SSL_ERROR_WANT_READ retries to 1 sec (#957)
retry with 1ms delays to prevent CPU hoggin
1 parent fc9b223 commit b8dec12

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

httplib.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6687,15 +6687,17 @@ inline ssize_t SSLSocketStream::read(char *ptr, size_t size) {
66876687
auto ret = SSL_read(ssl_, ptr, static_cast<int>(size));
66886688
if (ret < 0) {
66896689
auto err = SSL_get_error(ssl_, ret);
6690+
int n = 1000;
66906691
#ifdef _WIN32
6691-
while (err == SSL_ERROR_WANT_READ ||
6692-
err == SSL_ERROR_SYSCALL && WSAGetLastError() == WSAETIMEDOUT) {
6692+
while (--n >= 0 && (err == SSL_ERROR_WANT_READ ||
6693+
err == SSL_ERROR_SYSCALL && WSAGetLastError() == WSAETIMEDOUT)) {
66936694
#else
6694-
while (err == SSL_ERROR_WANT_READ) {
6695+
while (--n >= 0 && err == SSL_ERROR_WANT_READ) {
66956696
#endif
66966697
if (SSL_pending(ssl_) > 0) {
66976698
return SSL_read(ssl_, ptr, static_cast<int>(size));
66986699
} else if (is_readable()) {
6700+
std::this_thread::sleep_for(std::chrono::milliseconds(1));
66996701
ret = SSL_read(ssl_, ptr, static_cast<int>(size));
67006702
if (ret >= 0) { return ret; }
67016703
err = SSL_get_error(ssl_, ret);

0 commit comments

Comments
 (0)