Currently unique IDs for a stream are generated using repetitive code in
multiple locations, possibly allowing for inconsistent behavior.
---
 include/proto/stream.h |  3 +++
 src/http_ana.c         |  1 -
 src/stream.c           | 19 +++++++++++++++++++
 3 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/include/proto/stream.h b/include/proto/stream.h
index f8c0887b9..e54ac60cc 100644
--- a/include/proto/stream.h
+++ b/include/proto/stream.h
@@ -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);
diff --git a/src/http_ana.c b/src/http_ana.c
index e3d22445e..20c7b6e50 100644
--- a/src/http_ana.c
+++ b/src/http_ana.c
@@ -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)
diff --git a/src/stream.c b/src/stream.c
index 9798c5f0f..e0899b019 100644
--- a/src/stream.c
+++ b/src/stream.c
@@ -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);
@@ -2657,6 +2658,24 @@ 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);
+       }
+
+       if ((strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
+               return -1;
+       strm->unique_id[0] = 0;
+
+       return build_logline(strm, strm->unique_id, UNIQUEID_LEN, format);
+}
+
 /************************************************************************/
 /*           All supported ACL keywords must be declared here.          */
 /************************************************************************/
-- 
2.25.1


Reply via email to