简体   繁体   中英

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("..")
  }
}

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(...);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM