]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: stream: Make the `unique_id` member of `struct stream` a `struct ist`
authorTim Duesterhus <tim@bastelstu.be>
Thu, 5 Mar 2020 19:19:02 +0000 (20:19 +0100)
committerWilly Tarreau <w@1wt.eu>
Thu, 5 Mar 2020 19:21:58 +0000 (20:21 +0100)
The `unique_id` member of `struct stream` now is a `struct ist`.

include/proto/stream.h
include/types/stream.h
src/http_ana.c
src/http_fetch.c
src/log.c
src/stream.c

index e54ac60ccdf3a808c77b57905c94fe4b3fc56f83..69153df5d4640785a10137f52ba08191b8a5aef6 100644 (file)
@@ -66,7 +66,7 @@ void stream_shutdown(struct stream *stream, int why);
 void stream_dump(struct buffer *buf, const struct stream *s, const char *pfx, char eol);
 void stream_dump_and_crash(enum obj_type *obj, int rate);
 
-int stream_generate_unique_id(struct stream *strm, struct list *format);
+struct ist stream_generate_unique_id(struct stream *strm, struct list *format);
 
 void stream_process_counters(struct stream *s);
 void sess_change_server(struct stream *sess, struct server *newsrv);
index 90819e66dfcd30ff4f897bdcbb5d3a5abcd663ce..39a68557eee0f755de0ced5d4f2711ecfa6ca78b 100644 (file)
@@ -177,7 +177,7 @@ struct stream {
        int pcli_next_pid;                      /* next target PID to use for the CLI proxy */
        int pcli_flags;                         /* flags for CLI proxy */
 
-       char *unique_id;                        /* custom unique ID */
+       struct ist unique_id;                   /* custom unique ID */
 
        /* These two pointers are used to resume the execution of the rule lists. */
        struct list *current_rule_list;         /* this is used to store the current executed rule list. */
index 010b170a34ec87a68c3287e94b732a14a22b0c80..b9067dd0c4841575f01cb666d46ebf6c608855a5 100644 (file)
@@ -793,9 +793,9 @@ int http_process_request(struct stream *s, struct channel *req, int an_bit)
         * fetches only available in the HTTP request processing stage.
         */
        if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
-               int length;
+               struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
 
-               if ((length = stream_generate_unique_id(s, &sess->fe->format_unique_id)) < 0) {
+               if (!isttest(unique_id)) {
                        if (!(s->flags & SF_ERR_MASK))
                                s->flags |= SF_ERR_RESOURCE;
                        goto return_int_err;
@@ -803,7 +803,7 @@ int http_process_request(struct stream *s, struct channel *req, int an_bit)
 
                /* send unique ID if a "unique-id-header" is defined */
                if (isttest(sess->fe->header_unique_id) &&
-                   !http_add_header(htx, sess->fe->header_unique_id, ist2(s->unique_id, length)))
+                   unlikely(!http_add_header(htx, sess->fe->header_unique_id, s->unique_id)))
                                goto return_int_err;
        }
 
@@ -5078,9 +5078,9 @@ void http_end_txn(struct stream *s)
        pool_free(pool_head_requri, txn->uri);
        pool_free(pool_head_capture, txn->cli_cookie);
        pool_free(pool_head_capture, txn->srv_cookie);
-       pool_free(pool_head_uniqueid, s->unique_id);
+       pool_free(pool_head_uniqueid, s->unique_id.ptr);
 
-       s->unique_id = NULL;
+       s->unique_id = IST_NULL;
        txn->uri = NULL;
        txn->srv_cookie = NULL;
        txn->cli_cookie = NULL;
index 9113b5e493fd56d5fb8bafa1a229072b95ccd155..9cfcee2ab6c0312131cd980a992f1f557d2c40b0 100644 (file)
@@ -409,17 +409,17 @@ static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const ch
 
 static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
 {
-       int length;
+       struct ist unique_id;
 
        if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
                return 0;
 
-       length = stream_generate_unique_id(smp->strm, &smp->sess->fe->format_unique_id);
-       if (length < 0)
+       unique_id = stream_generate_unique_id(smp->strm, &smp->sess->fe->format_unique_id);
+       if (!isttest(unique_id))
                return 0;
 
-       smp->data.u.str.area = smp->strm->unique_id;
-       smp->data.u.str.data = length;
+       smp->data.u.str.area = smp->strm->unique_id.ptr;
+       smp->data.u.str.data = smp->strm->unique_id.len;
        smp->data.type = SMP_T_STR;
        smp->flags = SMP_F_CONST;
        return 1;
index b46605b8de3b558a06eb30e4df9d119d2dfe0faf..8f502ac7eafba72492286170fac27cc5f3709329 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -2927,8 +2927,10 @@ int sess_build_logline(struct session *sess, struct stream *s, char *dst, size_t
 
                        case LOG_FMT_UNIQUEID: // %ID
                                ret = NULL;
-                               src = s ? s->unique_id : NULL;
-                               ret = lf_text(tmplog, src, maxsize - (tmplog - dst), tmp);
+                               if (s)
+                                       ret = lf_text_len(tmplog, s->unique_id.ptr, s->unique_id.len, maxsize - (tmplog - dst), tmp);
+                               else
+                                       ret = lf_text_len(tmplog, NULL, 0, maxsize - (tmplog - dst), tmp);
                                if (ret == NULL)
                                        goto out;
                                tmplog = ret;
@@ -2982,7 +2984,7 @@ void strm_log(struct stream *s)
        }
 
        /* if unique-id was not generated */
-       if (!s->unique_id && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
+       if (!isttest(s->unique_id) && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
                stream_generate_unique_id(s, &sess->fe->format_unique_id);
        }
 
index 331f24be761d281b0f606d8ce9fa28f3b9dd6ba5..7b63f1d31319f4a185e2defbb7f50fef6cea990e 100644 (file)
@@ -170,8 +170,10 @@ static void strm_trace(enum trace_level level, uint64_t mask, const struct trace
        /* General info about the stream (htx/tcp, id...) */
        chunk_appendf(&trace_buf, " : [%u,%s]",
                      s->uniq_id, ((s->flags & SF_HTX) ? "HTX" : "TCP"));
-       if (s->unique_id)
-               chunk_appendf(&trace_buf, " id=%s", s->unique_id);
+       if (isttest(s->unique_id)) {
+               chunk_appendf(&trace_buf, " id=");
+               b_putist(&trace_buf, s->unique_id);
+       }
 
        /* Front and back stream-int state */
        chunk_appendf(&trace_buf, " SI=(%s,%s)",
@@ -399,7 +401,7 @@ struct stream *stream_new(struct session *sess, enum obj_type *origin)
        s->call_rate.curr_sec = s->call_rate.curr_ctr = s->call_rate.prev_ctr = 0;
        s->pcli_next_pid = 0;
        s->pcli_flags = 0;
-       s->unique_id = NULL;
+       s->unique_id = IST_NULL;
 
        if ((t = task_new(tid_bit)) == NULL)
                goto out_fail_alloc;
@@ -605,8 +607,8 @@ static void stream_free(struct stream *s)
                offer_buffers(NULL, tasks_run_queue);
        }
 
-       pool_free(pool_head_uniqueid, s->unique_id);
-       s->unique_id = NULL;
+       pool_free(pool_head_uniqueid, s->unique_id.ptr);
+       s->unique_id = IST_NULL;
 
        hlua_ctx_destroy(s->hlua);
        s->hlua = NULL;
@@ -2663,25 +2665,28 @@ void stream_dump_and_crash(enum obj_type *obj, int rate)
 }
 
 /* Generates a unique ID based on the given <format>, stores it in the given <strm> and
- * returns the length of the ID. -1 is returned on memory allocation failure.
+ * returns the unique ID.
+
+ * If this function fails to allocate memory IST_NULL is returned.
  *
- * If an ID is already stored within the stream nothing happens and length of the stored
- * ID is returned.
+ * If an ID is already stored within the stream nothing happens existing unique ID is
+ * returned.
  */
-int stream_generate_unique_id(struct stream *strm, struct list *format)
+struct ist stream_generate_unique_id(struct stream *strm, struct list *format)
 {
-       if (strm->unique_id != NULL) {
-               return strlen(strm->unique_id);
+       if (isttest(strm->unique_id)) {
+               return strm->unique_id;
        }
        else {
                char *unique_id;
+               int length;
                if ((unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
-                       return -1;
+                       return IST_NULL;
 
-               strm->unique_id = unique_id;
-               strm->unique_id[0] = 0;
+               length = build_logline(strm, unique_id, UNIQUEID_LEN, format);
+               strm->unique_id = ist2(unique_id, length);
 
-               return build_logline(strm, strm->unique_id, UNIQUEID_LEN, format);
+               return strm->unique_id;
        }
 }