Hi,
So I'm currently using Adam's MsSql driver to connect to an existing database. I've got a server set up with:
/* Initalize Router */
auto router = new URLRouter;
router.registerWebInterface(new WebInterface);
router.get("*", serveStaticFiles("./public/"));
/* Initalize Server */
auto settings = new HTTPServerSettings;
settings.port = 8080;
settings.sessionStore = new MemorySessionStore;
listenHTTP(settings, router);
And have implemented the WebInterface class. I'm working on implementing the sql connection to best match what I'm trying to do. Basically I just need to be able to pull some arbitrary number of SQL queries before each page to get relevant information for that page.
Right now I have a wrapper for queries that looks like this:
private auto query (string sql)
{
auto db = new MsSql(connectionString);
return db.query(sql);
}
This then gets used some number of times for each page. For instance the index page calls it 6 times. Since opening that connection is fairly expensive I just went ahead and imported std.parallelism
and threw each query into something like:
auto usersTask = scopedTask(&query,"
SELECT Users.DisplayName
FROM dbo.Users
");
usersTask.executeInNewThread;
And then at the end of all these tasks I have the following:
auto users = usersTask.yieldForce;
...
render!("index.dt", users, ...);
When it loads correctly it takes ~190ms, on localhost, time to first byte. This is compared to about 800ms without the multi-threading, so a marked improvement.
That said, it only loads correct about half the time (probably a bit less). For some reason it returns a 200 OK
message before the page finishes loading one the diet templates. I do not have this problem when I'm not using std.parallelism
and I just run the queries serially.
Any thoughts as to what I'm missing, or a better approach to doing this in general?
Thanks,
Charles