From 2405d846524496ec607b61e30d4f05683f27afcb Mon Sep 17 00:00:00 2001 From: Steven Schveighoffer Date: Wed, 16 Oct 2019 14:12:21 -0400 Subject: [PATCH 1/2] Add cleanup function. --- source/mysql/pool.d | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/source/mysql/pool.d b/source/mysql/pool.d index d231861d..4e48998a 100644 --- a/source/mysql/pool.d +++ b/source/mysql/pool.d @@ -21,8 +21,17 @@ debug(MYSQLN_TESTS) import mysql.test.common; } -version(Have_vibe_core) version = IncludeMySQLPool; -version(MySQLDocs) version = IncludeMySQLPool; +version(Have_vibe_core) +{ + version = IncludeMySQLPool; + static if(is(typeof(ConnectionPool!Connection.init.removeUnused((c){})))) + version = HaveCleanupFunction; +} +version(MySQLDocs) +{ + version = IncludeMySQLPool; + version = HaveCleanupFunction; +} version(IncludeMySQLPool) { @@ -50,6 +59,10 @@ version(IncludeMySQLPool) /// See: $(LINK http://vibed.org/api/vibe.core.connectionpool/ConnectionPool.maxConcurrency) uint maxConcurrency; + + /// See: $(LINK https://github.com/vibe-d/vibe-core/blob/24a83434e4c788ebb9859dfaecbe60ad0f6e9983/source/vibe/core/connectionpool.d#L113) + void removeUnused(scope void delegate(Connection conn) @safe nothrow disconnect_callback) + {} } /++ @@ -94,6 +107,7 @@ version(IncludeMySQLPool) { bool queuedForRelease = false; } + } /// Sets up a connection pool with the provided connection settings. @@ -427,6 +441,23 @@ version(IncludeMySQLPool) { preparedRegistrations.clear(); } + + version(HaveCleanupFunction) + { + /++ + Removes all unused connections from the pool. This can + be used to clean up before exiting the program to + ensure the event core driver can be properly shut down. + +/ + void removeUnusedConnections() @safe + { + m_pool.removeUnused((conn) @trusted nothrow { + try { + conn.close(); + } catch(Exception) {} + }); + } + } } @("registration") From 6005074676e9bf120e0870c5779c674823420532 Mon Sep 17 00:00:00 2001 From: Steven Schveighoffer Date: Mon, 28 Oct 2019 19:36:40 -0400 Subject: [PATCH 2/2] Add note for vibe-core requirement for removeUnused, and also a note about squelching exceptions. Also fixed some spacing. --- source/mysql/pool.d | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/source/mysql/pool.d b/source/mysql/pool.d index 4e48998a..f81ac626 100644 --- a/source/mysql/pool.d +++ b/source/mysql/pool.d @@ -62,7 +62,7 @@ version(IncludeMySQLPool) /// See: $(LINK https://github.com/vibe-d/vibe-core/blob/24a83434e4c788ebb9859dfaecbe60ad0f6e9983/source/vibe/core/connectionpool.d#L113) void removeUnused(scope void delegate(Connection conn) @safe nothrow disconnect_callback) - {} + {} } /++ @@ -448,9 +448,16 @@ version(IncludeMySQLPool) Removes all unused connections from the pool. This can be used to clean up before exiting the program to ensure the event core driver can be properly shut down. + + Note: this is only available if vibe-core 1.7.0 or later is being + used. +/ void removeUnusedConnections() @safe { + // Note: we squelch all exceptions here, because vibe-core + // requires the function be nothrow, and because an exception + // thrown while closing is probably not important enough to + // interrupt cleanup. m_pool.removeUnused((conn) @trusted nothrow { try { conn.close();