我想使用dart开发Web服务+ Web套接字服务器,但问题是由于隔离中未捕获的异常,我无法确保服务器的高可用性。
当然,我已经尝试捕获了我的主要功能,但这还不够。
如果将来的then()部分发生异常,则服务器将崩溃。
这意味着一个有缺陷的请求可能会使服务器停机。
我意识到这是一个open issue,但是有没有办法确认任何崩溃而又不会使VM崩溃,以便服务器可以继续处理其他请求?
谢谢。
最佳答案
我过去所做的是使用主隔离区启动一个托管实际Web服务器的子隔离区。启动隔离时,您可以将“未捕获的异常”处理程序传递给子隔离(我也认为您也应该能够在顶层注册一个,以防止出现该问题,如问题所引用)在原始问题中)。
例:
import 'dart:isolate';
void main() {
// Spawn a child isolate
spawnFunction(isolateMain, uncaughtExceptionHandler);
}
void isolateMain() {
// this is the "real" entry point of your app
// setup http servers and listen etc...
}
bool uncaughtExceptionHandler(ex) {
// TODO: add logging!
// respawn a new child isolate.
spawnFunction(isolateMain, uncaughtException);
return true; // we've handled the uncaught exception
}
关于dart - Dart Web Server:防止崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17271178/