From: Tim Duesterhus Date: Fri, 28 Feb 2020 14:13:33 +0000 (+0100) Subject: MINOR: stream: Add stream_generate_unique_id function X-Git-Tag: v2.2-dev4~67 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=127a74dd48035b22c2273eddfae3713a5a65d053;p=thirdparty%2Fhaproxy.git 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 f8c0887b94..e54ac60ccd 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 e3d22445eb..20c7b6e502 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 a54f523dff..331f24be76 100644 --- a/src/stream.c +++ b/src/stream.c @@ -66,6 +66,7 @@ #include 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 , stores it in the given 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. */ /************************************************************************/