This helper is in preparation for adding a second struct check element
to struct server.

Signed-off-by: Simon Horman <ho...@verge.net.au>

---

v7
* Moved cfgparse change to previous patch
* Reversed polarity of start_check_task() and updated caller accordingly
* Move increment of servpos to outside of if condition

Notes by Willy on v6:
    Cosmetic, but the cfgparse change just changes the previous patch, so
    it could have been merged there.

    Please change the "polarity" of the start_check_task() function or make it
    very clear that it returns an error code, or change the check below :

    -          if (start_check_task(&s->check, mininter, nbcheck, srvpos++))
    +          if (start_check_task(&s->check, mininter, nbcheck, srvpos++) < 0)
                       return -1

    The problem is I'm reading it as "if we could start a check task,
    return -1".  And yes, I know that all the kernel's networking code
    you're dealing with every day works this way so this can be confusing.

    Last cosmetic point, please keep srvpos++ out of the condition (I thought
    we lost it). I tend to discourage people from doing variable assignments
    in conditions because these are the places that are commented out the first
    during debugging sessions, causing the accidental loss of the assignment.

    Otherwise OK.

v5
* Rebase
---
 src/checks.c | 44 +++++++++++++++++++++++++++-----------------
 1 file changed, 27 insertions(+), 17 deletions(-)

diff --git a/src/checks.c b/src/checks.c
index cfbb8a3..e34ed0f 100644
--- a/src/checks.c
+++ b/src/checks.c
@@ -1509,6 +1509,32 @@ static struct task *process_chk(struct task *t)
        return t;
 }
 
+static int start_check_task(struct check *check, int mininter,
+                           int nbcheck, int srvpos)
+{
+       struct task *t;
+       /* task for the check */
+       if ((t = task_new()) == NULL) {
+               Alert("Starting [%s:%s] check: out of memory.\n",
+                     check->server->proxy->id, check->server->id);
+               return 0;
+       }
+
+       check->task = t;
+       t->process = process_chk;
+       t->context = check;
+
+       /* check this every ms */
+       t->expire = tick_add(now_ms,
+                            MS_TO_TICKS(((mininter &&
+                                          mininter >= srv_getinter(check)) ?
+                                         mininter : srv_getinter(check)) * 
srvpos / nbcheck));
+       check->start = now;
+       task_queue(t);
+
+       return 1;
+}
+
 /*
  * Start health-check.
  * Returns 0 if OK, -1 if error, and prints the error in this case.
@@ -1569,24 +1595,8 @@ int start_checks() {
                        if (!(s->state & SRV_CHECKED))
                                continue;
 
-                       /* one task for the checks */
-                       if ((t = task_new()) == NULL) {
-                               Alert("Starting [%s:%s] check: out of 
memory.\n", px->id, s->id);
+                       if (!start_check_task(&s->check, mininter, nbcheck, 
srvpos))
                                return -1;
-                       }
-
-                       s->check.task = t;
-                       t->process = process_chk;
-                       t->context = &s->check;
-
-                       /* check this every ms */
-                       t->expire = tick_add(now_ms,
-                                            MS_TO_TICKS(((mininter &&
-                                                          mininter >= 
srv_getinter(&s->check)) ?
-                                                         mininter : 
srv_getinter(&s->check)) * srvpos / nbcheck));
-                       s->check.start = now;
-                       task_queue(t);
-
                        srvpos++;
                }
        }
-- 
1.8.4


Reply via email to