我有 vivi-d 程序,用作链接代理。 我使用 mysql-native 连接 SQL。 它可以工作,但在流量较高的情况下,该服务会在 20 秒 - 2 分钟后终止。 除了 core.exception.AssertError 之外,我没有看到任何具体错误。 这让我开始思考是否一切都设置正确。我没有找到任何关于如何设置这样的项目的示例。
这是我的应用程序的非常简化的版本。这是 Vibe-d 项目中连接 MySQL 的正确方法吗?我在 Proxyd 类中创建一个 mysql 池,然后通过 lockConnection 在每个操作中打开新连接。
void main()
{
Proxyd proxy = new Proxyd(dbConfig);
auto settings = new HTTPServerSettings;
HTTPListener http_listener = listenHTTP(settings, proxy.getRouter());
runApplication();
}
class Proxyd
{
URLRouter router;
MySQLPool db_pool;
public:
this(Node dbConfig)
{
router = new URLRouter;
router.get("/link", &link);
db_pool = new MySQLPool(host,username,password,database,port);
}
private:
void link(HTTPServerRequest request, HTTPServerResponse response)
{
db = db_pool.lockConnection();
ResultRange rows = db.query("..")
}
}
我不太确定,但这可能是因为 vivi.core.connectionpool 无法在工作线程之间共享。 https://github.com/vibe-d/vibe-core/blob/f19401bfbe3d689b8ff7d50a9aafdf9f52887083/source/vibe/core/connectionpool.d#L74
这将是工作。
MySQLPool pool; // per threads, on TLS.
static this() {
pool = new MySQLPool(...);