0

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("..")
  }
}
Alireza S.N
  • 209
  • 1
  • 4
  • 10
GTO
  • 127
  • 11
  • I have been using vibe-d + mysql-d for years. What you wrote should be fine, so the bug lies elsewhere. Does the AssertError provide a file/line where it is triggering? Is it inside vibe-d or mysql-d? How are you building your project? Make sure to build in debug mode (the default of dub), not release mode (which will turn off almost all assert messages). – Steven Schveighoffer Sep 27 '22 at 13:02

1 Answers1

1

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(...);
kubo39
  • 26
  • 2
  • Thx for the answer. So you think I should create new pool in every action (void link in the example) and then db_pool.lockConnection? – GTO Sep 27 '22 at 10:14
  • No, you don't have to do so. static constructor runs only threads are created. https://dlang.org/spec/module.html#staticorder – kubo39 Sep 27 '22 at 10:48
  • I wouldn't be 100% sure of this, there's no indication that multiple threads are being used. By default only one thread is used in vibe-d. – Steven Schveighoffer Sep 27 '22 at 13:03
  • vibe.d spawns the same number of threads as the number of logical cores by default. https://github.com/vibe-d/vibe-core/blob/f19401bfbe3d689b8ff7d50a9aafdf9f52887083/source/vibe/core/core.d#L910-L917 and I got 6 via vibe.core.core.workerThreadCounts in my machine. – kubo39 Sep 27 '22 at 15:54
  • I don't believe the worker threads are used by default. If you are using worker threads, then it appears that will be the case, but only for things you run on worker threads. In my vibe-d application, I have one TLS MySQLPool, and it gets initialized once, in a shared static ctor. If vibe-d used worker threads, surely the system would crash when it tried to use the null pool? – Steven Schveighoffer Sep 27 '22 at 17:47
  • 1
    Ah, sorry, I'm wrong. Spawning worker threads only if called runWorkerTask via getFileInfos or used directly. – kubo39 Sep 28 '22 at 00:02