]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: peers: send code factorization.
authorFrédéric Lécaille <flecaille@haproxy.com>
Tue, 22 Jan 2019 14:54:53 +0000 (15:54 +0100)
committerWilly Tarreau <w@1wt.eu>
Tue, 29 Jan 2019 09:29:54 +0000 (10:29 +0100)
Implements peer_send_*msg() functions for switch and ack messages which call the
already defined peer_prepare_*msg() before calling ci_putblk().
These two new functions are used at three places in the peer_io_handler().

May be backported as far as 1.5.

src/peers.c

index 948ff7bcae60ff93502231c29bcd09eca318f821..d997f52eaefe3e692d00e2390e4b95241605fa62 100644 (file)
@@ -590,6 +590,64 @@ static inline int peer_getline(struct appctx  *appctx)
        return n;
 }
 
+/*
+ * Send a message after having called <peer_prepare_msg> to build it.
+ * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
+ * Returns -1 if there was not enough room left to send the message,
+ * any other negative returned value must  be considered as an error with an appcxt st0
+ * returned value equal to PEER_SESS_ST_END.
+ */
+static inline int peer_send_msg(struct shared_table *st, struct appctx *appctx,
+                                int (*peer_prepare_msg)(struct shared_table *, char *, size_t))
+{
+       int ret, msglen;
+       struct stream_interface *si = appctx->owner;
+
+       msglen = peer_prepare_msg(st, trash.area, trash.size);
+       if (!msglen) {
+               /* internal error: message does not fit in trash */
+               appctx->st0 = PEER_SESS_ST_END;
+               return 0;
+       }
+
+       /* message to buffer */
+       ret = ci_putblk(si_ic(si), trash.area, msglen);
+       if (ret <= 0) {
+               if (ret == -1) {
+                       /* No more write possible */
+                       si_rx_room_blk(si);
+                       return -1;
+               }
+               appctx->st0 = PEER_SESS_ST_END;
+       }
+
+       return ret;
+}
+
+/*
+ * Send a stick-table switch message.
+ * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
+ * Returns -1 if there was not enough room left to send the message,
+ * any other negative returned value must  be considered as an error with an appcxt st0
+ * returned value equal to PEER_SESS_ST_END.
+ */
+static inline int peer_send_switchmsg(struct shared_table *st, struct appctx *appctx)
+{
+       return peer_send_msg(st, appctx, peer_prepare_switchmsg);
+}
+
+/*
+ * Send a stick-table update acknowledgement message.
+ * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
+ * Returns -1 if there was not enough room left to send the message,
+ * any other negative returned value must  be considered as an error with an appcxt st0
+ * returned value equal to PEER_SESS_ST_END.
+ */
+static inline int peer_send_ackmsg(struct shared_table *st, struct appctx *appctx)
+{
+       return peer_send_msg(st, appctx, peer_prepare_ackmsg);
+}
+
 /*
  * IO Handler to handle message exchance with a peer
  */
@@ -1440,27 +1498,10 @@ incomplete:
 
                                                /* It remains some updates to ack */
                                                if (st->last_get != st->last_acked) {
-                                                       int msglen;
-
-                                                       msglen = peer_prepare_ackmsg(st,
-                                                                                    trash.area,
-                                                                                    trash.size);
-                                                       if (!msglen) {
-                                                               /* internal error: message does not fit in trash */
-                                                               appctx->st0 = PEER_SESS_ST_END;
-                                                               goto switchstate;
-                                                       }
-
-                                                       /* message to buffer */
-                                                       repl = ci_putblk(si_ic(si),
-                                                                        trash.area,
-                                                                        msglen);
+                                                       repl = peer_send_ackmsg(st, appctx);
                                                        if (repl <= 0) {
-                                                               /* no more write possible */
-                                                               if (repl == -1) {
-                                                                       goto full;
-                                                               }
-                                                               appctx->st0 = PEER_SESS_ST_END;
+                                                               if (repl == -1)
+                                                                       goto out;
                                                                goto switchstate;
                                                        }
                                                        st->last_acked = st->last_get;
@@ -1474,29 +1515,11 @@ incomplete:
                                                                int new_pushed;
 
                                                                if (st != curpeer->last_local_table) {
-                                                                       int msglen;
-
-                                                                       msglen = peer_prepare_switchmsg(st,
-                                                                                                       trash.area,
-                                                                                                       trash.size);
-                                                                       if (!msglen) {
-                                                                               HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
-                                                                               /* internal error: message does not fit in trash */
-                                                                               appctx->st0 = PEER_SESS_ST_END;
-                                                                               goto switchstate;
-                                                                       }
-
-                                                                       /* message to buffer */
-                                                                       repl = ci_putblk(si_ic(si),
-                                                                                        trash.area,
-                                                                                        msglen);
+                                                                       repl = peer_send_switchmsg(st, appctx);
                                                                        if (repl <= 0) {
                                                                                HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
-                                                                               /* no more write possible */
-                                                                               if (repl == -1) {
-                                                                                       goto full;
-                                                                               }
-                                                                               appctx->st0 = PEER_SESS_ST_END;
+                                                                               if (repl == -1)
+                                                                                       goto out;
                                                                                goto switchstate;
                                                                        }
                                                                        curpeer->last_local_table = st;
@@ -1576,29 +1599,13 @@ incomplete:
                                                                int new_pushed;
 
                                                                if (st != curpeer->last_local_table) {
-                                                                       int msglen;
-
-                                                                       msglen = peer_prepare_switchmsg(st,
-                                                                                                       trash.area,
-                                                                                                       trash.size);
-                                                                       if (!msglen) {
-                                                                               /* internal error: message does not fit in trash */
-                                                                               appctx->st0 = PEER_SESS_ST_END;
-                                                                               goto switchstate;
-                                                                       }
-
-                                                                       /* message to buffer */
-                                                                       repl = ci_putblk(si_ic(si),
-                                                                                        trash.area,
-                                                                                        msglen);
+                                                                       repl = peer_send_switchmsg(st, appctx);
                                                                        if (repl <= 0) {
-                                                                               /* no more write possible */
-                                                                               if (repl == -1) {
-                                                                                       goto full;
-                                                                               }
-                                                                               appctx->st0 = PEER_SESS_ST_END;
+                                                                               if (repl == -1)
+                                                                                       goto out;
                                                                                goto switchstate;
                                                                        }
+
                                                                        curpeer->last_local_table = st;
                                                                }
 
@@ -1670,29 +1677,13 @@ incomplete:
                                                                int new_pushed;
 
                                                                if (st != curpeer->last_local_table) {
-                                                                       int msglen;
-
-                                                                       msglen = peer_prepare_switchmsg(st,
-                                                                                                       trash.area,
-                                                                                                       trash.size);
-                                                                       if (!msglen) {
-                                                                               /* internal error: message does not fit in trash */
-                                                                               appctx->st0 = PEER_SESS_ST_END;
-                                                                               goto switchstate;
-                                                                       }
-
-                                                                       /* message to buffer */
-                                                                       repl = ci_putblk(si_ic(si),
-                                                                                        trash.area,
-                                                                                        msglen);
+                                                                       repl = peer_send_switchmsg(st, appctx);
                                                                        if (repl <= 0) {
-                                                                               /* no more write possible */
-                                                                               if (repl == -1) {
-                                                                                       goto full;
-                                                                               }
-                                                                               appctx->st0 = PEER_SESS_ST_END;
+                                                                               if (repl == -1)
+                                                                                       goto out;
                                                                                goto switchstate;
                                                                        }
+
                                                                        curpeer->last_local_table = st;
                                                                }