commit e89fc35: [Minor] Allow to capture upstream object

Vsevolod Stakhov vsevolod at highsecure.ru
Mon Sep 13 13:42:04 UTC 2021


Author: Vsevolod Stakhov
Date: 2021-09-13 14:40:06 +0100
URL: https://github.com/rspamd/rspamd/commit/e89fc350172a710d97eb301d8dfc9a9667610ed3 (HEAD -> master)

[Minor] Allow to capture upstream object

---
 src/libserver/fuzzy_backend/fuzzy_backend_redis.c |  9 +++++----
 src/libserver/redis_pool.cxx                      |  3 ++-
 src/libutil/upstream.c                            | 13 +++++++++++++
 src/libutil/upstream.h                            | 12 ++++++++++++
 4 files changed, 32 insertions(+), 5 deletions(-)

diff --git a/src/libserver/fuzzy_backend/fuzzy_backend_redis.c b/src/libserver/fuzzy_backend/fuzzy_backend_redis.c
index 75bb005d7..0de487534 100644
--- a/src/libserver/fuzzy_backend/fuzzy_backend_redis.c
+++ b/src/libserver/fuzzy_backend/fuzzy_backend_redis.c
@@ -163,6 +163,7 @@ rspamd_fuzzy_redis_session_dtor (struct rspamd_fuzzy_redis_session *session,
 	rspamd_fuzzy_redis_session_free_args (session);
 
 	REF_RELEASE (session->backend);
+	rspamd_upstream_unref (session->up);
 	g_free (session);
 }
 
@@ -678,7 +679,7 @@ rspamd_fuzzy_backend_check_redis (struct rspamd_fuzzy_backend *bk,
 			NULL,
 			0);
 
-	session->up = up;
+	session->up = rspamd_upstream_ref (up);
 	addr = rspamd_upstream_addr_next (up);
 	g_assert (addr != NULL);
 	session->ctx = rspamd_redis_pool_connect (backend->pool,
@@ -818,7 +819,7 @@ rspamd_fuzzy_backend_count_redis (struct rspamd_fuzzy_backend *bk,
 			NULL,
 			0);
 
-	session->up = up;
+	session->up = rspamd_upstream_ref (up);
 	addr = rspamd_upstream_addr_next (up);
 	g_assert (addr != NULL);
 	session->ctx = rspamd_redis_pool_connect (backend->pool,
@@ -956,7 +957,7 @@ rspamd_fuzzy_backend_version_redis (struct rspamd_fuzzy_backend *bk,
 			NULL,
 			0);
 
-	session->up = up;
+	session->up = rspamd_upstream_ref (up);
 	addr = rspamd_upstream_addr_next (up);
 	g_assert (addr != NULL);
 	session->ctx = rspamd_redis_pool_connect (backend->pool,
@@ -1530,7 +1531,7 @@ rspamd_fuzzy_backend_update_redis (struct rspamd_fuzzy_backend *bk,
 			NULL,
 			0);
 
-	session->up = up;
+	session->up = rspamd_upstream_ref (up);
 	addr = rspamd_upstream_addr_next (up);
 	g_assert (addr != NULL);
 	session->ctx = rspamd_redis_pool_connect (backend->pool,
diff --git a/src/libserver/redis_pool.cxx b/src/libserver/redis_pool.cxx
index 896dc7750..26b5f2f81 100644
--- a/src/libserver/redis_pool.cxx
+++ b/src/libserver/redis_pool.cxx
@@ -395,7 +395,8 @@ redis_pool_connection::redis_pool_connection(redis_pool *_pool,
 
 	pool->register_context(ctx, this);
 	ctx->data = this;
-	rspamd_random_hex((guchar *) tag, sizeof(tag));
+	memset(tag, 0, sizeof(tag));
+	rspamd_random_hex((guchar *)tag, sizeof(tag) - 1);
 
 	redisLibevAttach(pool->event_loop, ctx);
 	redisAsyncSetDisconnectCallback(ctx, redis_pool_connection::redis_on_disconnect);
diff --git a/src/libutil/upstream.c b/src/libutil/upstream.c
index 889b524ac..98e766194 100644
--- a/src/libutil/upstream.c
+++ b/src/libutil/upstream.c
@@ -1714,3 +1714,16 @@ void rspamd_upstreams_add_watch_callback (struct upstream_list *ups,
 
 	DL_APPEND (ups->watchers, nw);
 }
+
+struct upstream*
+rspamd_upstream_ref (struct upstream *up)
+{
+	REF_RETAIN (up);
+	return up;
+}
+
+void
+rspamd_upstream_unref (struct upstream *up)
+{
+	REF_RELEASE (up);
+}
diff --git a/src/libutil/upstream.h b/src/libutil/upstream.h
index 89bcd9b52..72a768892 100644
--- a/src/libutil/upstream.h
+++ b/src/libutil/upstream.h
@@ -299,6 +299,18 @@ struct upstream *rspamd_upstream_get_except (struct upstream_list *ups,
  */
 void rspamd_upstream_reresolve (struct upstream_ctx *ctx);
 
+/**
+ * Share ownership on upstream
+ * @param up
+ * @return
+ */
+struct upstream* rspamd_upstream_ref (struct upstream *up);
+/**
+ * Unshare ownership on upstream
+ * @param up
+ */
+void rspamd_upstream_unref (struct upstream *up);
+
 #ifdef  __cplusplus
 }
 #endif


More information about the Commits mailing list