]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: stream: Add stream_generate_unique_id function
authorTim Duesterhus <tim@bastelstu.be>
Fri, 28 Feb 2020 14:13:33 +0000 (15:13 +0100)
committerWilly Tarreau <w@1wt.eu>
Thu, 5 Mar 2020 06:23:00 +0000 (07:23 +0100)
Currently unique IDs for a stream are generated using repetitive code in
multiple locations, possibly allowing for inconsistent behavior.

include/proto/stream.h
src/http_ana.c
src/stream.c

index f8c0887b94232f8506b5b7228baaca9db932ba0d..e54ac60ccdf3a808c77b57905c94fe4b3fc56f83 100644 (file)
@@ -53,6 +53,7 @@ extern struct trace_source trace_strm;
 #define IS_HTX_STRM(strm) ((strm)->flags & SF_HTX)
 
 extern struct pool_head *pool_head_stream;
+extern struct pool_head *pool_head_uniqueid;
 extern struct list streams;
 
 extern struct data_cb sess_conn_cb;
@@ -65,6 +66,8 @@ 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);
+
 void stream_process_counters(struct stream *s);
 void sess_change_server(struct stream *sess, struct server *newsrv);
 struct task *process_stream(struct task *t, void *context, unsigned short state);
index e3d22445eb09582a55bc37e69cb53ec2017a9378..20c7b6e502ad6cd734162c59a044857563f5e78d 100644 (file)
@@ -5093,7 +5093,6 @@ void http_end_txn(struct stream *s)
 
 
 DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
-DECLARE_POOL(pool_head_uniqueid, "uniqueid", UNIQUEID_LEN);
 
 __attribute__((constructor))
 static void __http_protocol_init(void)
index a54f523dff5ca0c6a32e218d0eeb33f62e1aaec4..331f24be761d281b0f606d8ce9fa28f3b9dd6ba5 100644 (file)
@@ -66,6 +66,7 @@
 #include <proto/vars.h>
 
 DECLARE_POOL(pool_head_stream, "stream", sizeof(struct stream));
+DECLARE_POOL(pool_head_uniqueid, "uniqueid", UNIQUEID_LEN);
 
 struct list streams = LIST_HEAD_INIT(streams);
 __decl_spinlock(streams_lock);
@@ -2661,6 +2662,29 @@ void stream_dump_and_crash(enum obj_type *obj, int rate)
        abort();
 }
 
+/* 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.
+ *
+ * If an ID is already stored within the stream nothing happens and length of the stored
+ * ID is returned.
+ */
+int stream_generate_unique_id(struct stream *strm, struct list *format)
+{
+       if (strm->unique_id != NULL) {
+               return strlen(strm->unique_id);
+       }
+       else {
+               char *unique_id;
+               if ((unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
+                       return -1;
+
+               strm->unique_id = unique_id;
+               strm->unique_id[0] = 0;
+
+               return build_logline(strm, strm->unique_id, UNIQUEID_LEN, format);
+       }
+}
+
 /************************************************************************/
 /*           All supported ACL keywords must be declared here.          */
 /************************************************************************/