From a3837c8fc0eaa35b9ba7ec8112632797ddfc9f3d Mon Sep 17 00:00:00 2001
From: hamza zia <ziahamza2007@gmail.com>
Date: Wed, 17 Jul 2013 13:31:30 +0200
Subject: [PATCH] Run STAGE40 plugins if they have more pending data

Currently, STAGE40 plugins are only called once if monkey has no pending
data to send to the socket, but the plugins may have pending data to
send in one call if they dont want to block the monkey event loop, so
call them again until they have no pending data to send.
---
 src/mk_plugin.c  | 15 ++++++++++++++-
 src/mk_request.c | 12 +++++++++---
 2 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/src/mk_plugin.c b/src/mk_plugin.c
index c7105d0..526a650 100644
--- a/src/mk_plugin.c
+++ b/src/mk_plugin.c
@@ -647,7 +647,20 @@ int mk_plugin_stage_run(unsigned int hook,
         while (stm) {
             MK_TRACE("[%s] STAGE 40", stm->p->shortname);
 
-            stm->p->stage.s40(cs, sr);
+            ret = stm->p->stage.s40(cs, sr);
+
+            switch (ret) {
+                case MK_PLUGIN_RET_NOT_ME:
+                    break;
+                case MK_PLUGIN_RET_END:
+                case MK_PLUGIN_RET_CLOSE_CONX:
+                case MK_PLUGIN_RET_CONTINUE:
+                    return ret;
+                default:
+                    mk_err("Plugin '%s' returns invalid value %i",
+                           stm->p->shortname, ret);
+                    exit(EXIT_FAILURE);
+            }
             stm = stm->next;
         }
     }
diff --git a/src/mk_request.c b/src/mk_request.c
index 579ed02..fb8bb4e 100644
--- a/src/mk_request.c
+++ b/src/mk_request.c
@@ -686,7 +686,7 @@ int mk_handler_read(int socket, struct client_session *cs)
 
 int mk_handler_write(int socket, struct client_session *cs)
 {
-    int final_status = 0;
+    int final_status = 0, plugin_ret = 0;
     struct session_request *sr_node;
     struct mk_list *sr_list, *sr_head;
 
@@ -716,15 +716,21 @@ int mk_handler_write(int socket, struct client_session *cs)
             return final_status;
         }
         else {
-            /* STAGE_40, request has ended */
-            mk_plugin_stage_run(MK_PLUGIN_STAGE_40, socket,
+            /* run STAGE_40 plugins, we have no data to send */
+            plugin_ret = mk_plugin_stage_run(MK_PLUGIN_STAGE_40, socket,
                                 NULL, cs, sr_node);
             switch (final_status) {
             case EXIT_NORMAL:
+                /* check if STAGE_40 plugin has some pending data to send */
+                if (plugin_ret == MK_PLUGIN_RET_CONTINUE) {
+                  return MK_PLUGIN_RET_CONTINUE;
+                }
+                break;
             case EXIT_ERROR:
                 if (sr_node->close_now == MK_TRUE) {
                     return -1;
                 }
+
                 break;
             case EXIT_ABORT:
                   return -1;
-- 
1.8.1.2

