4

I want to build an application server with Dart. The httpServer in the dart:io library is certainly a good starting point for that. But I struggle with the task to "deploy" an application without restarting the server process.

To be more precise: I want to have something like a servlet container in Java, like Tomcat, into which I can easily deploy or redeploy an application without restarting the container. I thought I could utilize the mirror system, which allows me in principle to load a library and its contained classes from the filesystem. But unfortunately it seems that I cannot re-load the library. When I add for example a new class to the library, or change the coding of an existing class, a new reflection of the library without restarting the dart process, does not reflect the changes. Only when I stop the process and restart it again, the changes are visible.

So: is there a way to scrub the mirror system and let it load the library and its classes again, within the same Dart process?

Gregor
  • 2,917
  • 5
  • 28
  • 50

2 Answers2

1

I think isolates are a good fit for this requirement.

I haven't used them myself much yet but as far as I know you can load and unload them dynamically. The documentation is not very extensive yet. A few things I found:

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Works great! Indeed, within each new isolate the MirrorSystem mirrors the library anew. But this comes with a performance impact. The time to execute a "Dartlet" increases from ~0.3ms to ~30.0ms. So I will not reload the lib with every request, but only if a "deployment" event is triggered. – Gregor Apr 18 '14 at 16:52
1

So, yes, it is possible in Dart to dynamically (re-)load dart-files at runtime. Every new isolate has its own MirrorSystem. If you want to reload a dart-file you must create a new isolate and use the MirrorSystem of this isolate to iterate over the contents in the libraries known to this MirrorSystem. If your dart-file is part of a library known to the MirrorSystem, all functions and classes contained in this file are loaded and reflected anew.

This solution has some drawbacks: First, it is quite heavyweight. The programming of inter-isolate communication is cumbersome. Also it is to be seen whether the memory consumption increases with each reload. Second, the solution is not really dynamic: Isolates load only libraries that are "known" at design-time. They must be directly or indirectly imported into the dart file that contains the static function, which is called when the isolate is created.

Two ideas how the situation could be improved: 1. It would help if the spawn and the spawnUri methods of Isolate could get a list of additional libraries as parameter, which are included in the MirrorSystem of the isolate. 2. The classloaders in Java are independent of processes and threads. They just load classes. Why isn't this possible in Dart?

Gregor
  • 2,917
  • 5
  • 28
  • 50