mysqldvibed

How to properly setup Vibe-d server with mysql-native


I have vibe-d program that is used as a proxy for links. I use mysql-native to connect to SQL. It works, but the service dies after 20s - 2mins on higher traffic. I didn't see any specific error besides: core.exception.AssertError. This got me thinking if I have everything set up properly. I didn't find any example on how to set a project like this.

This is a VERY simplified version of my app. Is this the right way to connect to MySQL in Vibe-d project? I create a mysql pool in Proxyd class and then open new connection in every action by 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("..")
  }
}

Solution

  • I'm not sure exactly, but it may be due to that vibe.core.connectionpool cannot be shared across worker threads. https://github.com/vibe-d/vibe-core/blob/f19401bfbe3d689b8ff7d50a9aafdf9f52887083/source/vibe/core/connectionpool.d#L74

    This would be work.

    MySQLPool pool;  // per threads, on TLS.
    
    static this() {
        pool = new MySQLPool(...);