MINOR: stream: Add stream_generate_unique_id function
Currently unique IDs for a stream are generated using repetitive code in
multiple locations, possibly allowing for inconsistent behavior.
diff --git a/include/proto/stream.h b/include/proto/stream.h
index f8c0887..e54ac60 100644
--- a/include/proto/stream.h
+++ b/include/proto/stream.h
@@ -53,6 +53,7 @@
#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_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 e3d2244..20c7b6e 100644
--- a/src/http_ana.c
+++ b/src/http_ana.c
@@ -5093,7 +5093,6 @@
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 a54f523..331f24b 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);
@@ -2661,6 +2662,29 @@
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. */
/************************************************************************/