2013-06-24 144 views
2

Id'like使用dart开发Web服务+ Web套接字服务器,但问题是我无法确保服务器的高可用性,因为在分离时未捕获异常。Dart Web服务器:防止崩溃

当然,我试图抓住我的主要功能,但这还不够。

如果将来的()部分发生异常,服务器将崩溃。

这意味着ONE瑕疵请求可能会使服务器停机。

我意识到这是一个open issue,但有什么办法来确认没有崩溃的虚拟机的任何崩溃,以便服务器可以继续提供其他请求?

谢谢。

回答

3

我过去所做的是使用主隔离来启动托管实际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 
} 
+0

如果我可以让uncaughtExceptionHandler工作,但不知何故,这将是非常好的。 [我发布了一个新的问题](http://stackoverflow.com/questions/17292762/dart-unhandledexceptioncallback-is-ignored)。 –

+0

谢谢 - 看到你的问题,并提出了一个错误报告:http://dartbug.com/11505 –

3

克里斯Buckett给你失败时,重新启动服务器的好方法。但是,您仍然不希望服务器出现故障。

try-catch仅适用于同步代码。

doSomething() { 
    try { 
    someSynchronousFunc(); 

    someAsyncFunc().then(() => print('foo')); 
    } catch (e) { 
    // ... 
    } 
} 

当你的异步方法完成或失败,它发生的程序与方法doSomething完成后“长”

当你编写异步代码,它通常是一个好主意,通过返回的将来启动的方法:

Future doSomething() { 
    return new Future(() { 
    // your code here. 
    var a = b + 5; // throws and is caught. 

    return someAsyncCall(); // Errors are forwarded if you return the Future directly. 
    }); 
} 

这保证了,如果你有一个抛出的代码,它捉住他们,主叫方可以再catchError()他们。

如果以这种方式编写,假设您至少在顶层有一些错误处理,那么崩溃就会少得多。

无论您何时调用返回Future的方法,都可以直接返回(如上所示)或catchError(),以便您在本地处理可能的错误。您需要阅读的主页上有a great lengthy article