pollin shouldnt always win over pollout
authorAndy Green <andy@warmcat.com>
Sat, 23 Apr 2016 01:26:11 +0000 (09:26 +0800)
committerAndy Green <andy@warmcat.com>
Sat, 23 Apr 2016 01:26:11 +0000 (09:26 +0800)
https://github.com/warmcat/libwebsockets/issues/501

Signed-off-by: Andy Green <andy@warmcat.com>
lib/private-libwebsockets.h
lib/server.c

index 522ca8e..6eb0428 100644 (file)
@@ -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
index d8096fc..24392af 100644 (file)
@@ -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;
                }