Skip to content

Commit 95191af

Browse files
schveiguyAbscissa
authored andcommitted
Add cleanup function. (#210)
* Add cleanup function. * Add note for vibe-core requirement for removeUnused, and also a note about squelching exceptions. Also fixed some spacing.
1 parent d97767c commit 95191af

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

source/mysql/pool.d

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,17 @@ debug(MYSQLN_TESTS)
2121
import mysql.test.common;
2222
}
2323

24-
version(Have_vibe_core) version = IncludeMySQLPool;
25-
version(MySQLDocs) version = IncludeMySQLPool;
24+
version(Have_vibe_core)
25+
{
26+
version = IncludeMySQLPool;
27+
static if(is(typeof(ConnectionPool!Connection.init.removeUnused((c){}))))
28+
version = HaveCleanupFunction;
29+
}
30+
version(MySQLDocs)
31+
{
32+
version = IncludeMySQLPool;
33+
version = HaveCleanupFunction;
34+
}
2635

2736
version(IncludeMySQLPool)
2837
{
@@ -50,6 +59,10 @@ version(IncludeMySQLPool)
5059

5160
/// See: $(LINK http://vibed.org/api/vibe.core.connectionpool/ConnectionPool.maxConcurrency)
5261
uint maxConcurrency;
62+
63+
/// See: $(LINK https://github.com/vibe-d/vibe-core/blob/24a83434e4c788ebb9859dfaecbe60ad0f6e9983/source/vibe/core/connectionpool.d#L113)
64+
void removeUnused(scope void delegate(Connection conn) @safe nothrow disconnect_callback)
65+
{}
5366
}
5467

5568
/++
@@ -94,6 +107,7 @@ version(IncludeMySQLPool)
94107
{
95108
bool queuedForRelease = false;
96109
}
110+
97111
}
98112

99113
/// Sets up a connection pool with the provided connection settings.
@@ -427,6 +441,30 @@ version(IncludeMySQLPool)
427441
{
428442
preparedRegistrations.clear();
429443
}
444+
445+
version(HaveCleanupFunction)
446+
{
447+
/++
448+
Removes all unused connections from the pool. This can
449+
be used to clean up before exiting the program to
450+
ensure the event core driver can be properly shut down.
451+
452+
Note: this is only available if vibe-core 1.7.0 or later is being
453+
used.
454+
+/
455+
void removeUnusedConnections() @safe
456+
{
457+
// Note: we squelch all exceptions here, because vibe-core
458+
// requires the function be nothrow, and because an exception
459+
// thrown while closing is probably not important enough to
460+
// interrupt cleanup.
461+
m_pool.removeUnused((conn) @trusted nothrow {
462+
try {
463+
conn.close();
464+
} catch(Exception) {}
465+
});
466+
}
467+
}
430468
}
431469

432470
@("registration")

0 commit comments

Comments
 (0)