diff --git a/lib/private-libwebsockets.h b/lib/private-libwebsockets.h index 522ca8ec..6eb0428d 100644 --- a/lib/private-libwebsockets.h +++ b/lib/private-libwebsockets.h @@ -1251,6 +1251,7 @@ struct lws { unsigned int cache_reuse:1; unsigned int cache_revalidate:1; unsigned int cache_intermediaries:1; + unsigned int favoured_pollin:1; #ifdef LWS_WITH_ACCESS_LOG unsigned int access_log_pending:1; #endif diff --git a/lib/server.c b/lib/server.c index d8096fc7..24392afc 100644 --- a/lib/server.c +++ b/lib/server.c @@ -1414,6 +1414,19 @@ lws_server_socket_service(struct lws_context *context, struct lws *wsi, if (!(pollfd->revents & pollfd->events & LWS_POLLIN)) goto try_pollout; + /* + * If we previously just did POLLIN when IN and OUT were + * signalled (because POLLIN processing may have used up + * the POLLOUT), don't let that happen twice in a row... + * next time we see the situation favour POLLOUT + */ + + if (wsi->favoured_pollin && + (pollfd->revents & pollfd->events & LWS_POLLOUT)) { + wsi->favoured_pollin = 0; + goto try_pollout; + } + /* these states imply we MUST have an ah attached */ if (wsi->state == LWSS_HTTP || @@ -1492,14 +1505,19 @@ lws_server_socket_service(struct lws_context *context, struct lws *wsi, /* just ignore incoming if waiting for close */ if (wsi->state != LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE) { /* - * hm this may want to send + * this may want to send * (via HTTP callback for example) */ n = lws_read(wsi, pt->serv_buf, len); if (n < 0) /* we closed wsi */ return 1; - /* hum he may have used up the - * writability above */ + /* + * he may have used up the + * writability above, if we will defer POLLOUT + * processing in favour of POLLIN, note it + */ + if (pollfd->revents & LWS_POLLOUT) + wsi->favoured_pollin = 1; break; }