Skip to content

Commit 0d48a8f

Browse files
mszabo-wikiahenryr
andcommitted
Expose whether a DB connection came from the connection pool
It would be useful for userland Hack code in Slack to determine whether a connection obtained via an HHVM async_mysql connection pool was recycled from the pool or freshly opened, to allow more granular instrumentation than what pool-level stats currently provide. See facebook#8013 for the full rationale. The groundwork for this on the Squangle side has already been laid in D92081429 née facebook/squangle#20. Add methods to `AsyncMysqlConnection` that make use of the newly added accessors to expose this information to Hack. hphp/test/slow/ext_async_mysql/ doesn't seem to currently have tests that interact with a MySQL server so I didn't include tests in this patch. I can add some if needed. Closes facebook#8013. Co-authored-by: Henry Robinson <hrobinson@slack-corp.com>
1 parent 7dab394 commit 0d48a8f

3 files changed

Lines changed: 30 additions & 0 deletions

File tree

hphp/hack/hhi/stdlib/builtins_async_mysql.hhi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ namespace {
218218
public function getSslCertSan(): Vector<string> {}
219219
public function getSslCertExtensions(): Vector<string> {}
220220
public function isSslCertValidationEnforced(): bool {}
221+
public function wasFromPoolHit(): bool {}
222+
public function wasReusedWithChangeUser(): bool {}
221223
}
222224

223225
abstract class AsyncMysqlResult {

hphp/runtime/ext/async_mysql/ext_async_mysql.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,18 @@ static bool HHVM_METHOD(AsyncMysqlConnection, isSSL) {
12081208
return data->m_conn->isSSL();
12091209
}
12101210

1211+
static bool HHVM_METHOD(AsyncMysqlConnection, wasFromPoolHit) {
1212+
auto* data = Native::data<AsyncMysqlConnection>(this_);
1213+
data->verifyValidConnection();
1214+
return data->m_conn->wasFromPoolHit();
1215+
}
1216+
1217+
static bool HHVM_METHOD(AsyncMysqlConnection, wasReusedWithChangeUser) {
1218+
auto* data = Native::data<AsyncMysqlConnection>(this_);
1219+
data->verifyValidConnection();
1220+
return data->m_conn->wasReusedWithChangeUser();
1221+
}
1222+
12111223
static int64_t HHVM_METHOD(AsyncMysqlConnection, warningCount) {
12121224
auto* data = Native::data<AsyncMysqlConnection>(this_);
12131225

@@ -2264,6 +2276,8 @@ static struct AsyncMysqlExtension final : Extension {
22642276
HHVM_ME(AsyncMysqlConnection, port);
22652277
HHVM_ME(AsyncMysqlConnection, setReusable);
22662278
HHVM_ME(AsyncMysqlConnection, isReusable);
2279+
HHVM_ME(AsyncMysqlConnection, wasFromPoolHit);
2280+
HHVM_ME(AsyncMysqlConnection, wasReusedWithChangeUser);
22672281
HHVM_ME(AsyncMysqlConnection, connectResult);
22682282
HHVM_ME(AsyncMysqlConnection, lastActivityTime);
22692283
HHVM_ME(AsyncMysqlConnection, getSslCertCn);

hphp/runtime/ext/async_mysql/ext_async_mysql.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,20 @@ public function getSslCertExtensions(): Vector<string>;
664664
*/
665665
<<__Native>>
666666
public function isSslCertValidationEnforced(): bool;
667+
668+
/**
669+
* Whether this connection was newly opened or recycled from a connection pool.
670+
* @return - "true" if the pool was recycled from a connection pool, "false" otherwise.
671+
*/
672+
<<__Native>>
673+
public function wasFromPoolHit(): bool;
674+
675+
/**
676+
* Whether this connection was recycled from a connection pool with changed credentials.
677+
* @return - "true" if the pool was recycled from a connection pool with a credential change, "false" otherwise.
678+
*/
679+
<<__Native>>
680+
public function wasReusedWithChangeUser(): bool;
667681
}
668682

669683
/**

0 commit comments

Comments
 (0)