[Erlang_Question29]进程收到不是期望的消息时怎么办?
handle_info({add,X},Sum) ->
{noreply,Sum+X};
{add,X}------->{plus,X}
在A进程中存着大量的{plus,X}信息,使得这个进程的内存占用也变得异常的大。
1.你可以记录这些消息:
1a.) as info 1b.) as warning 1c.) as error
2.对消息进行记数,然后对记数做怎样的处理? 3.直接忽略它们,不做trace?
4.让server直接crash掉?
I tend to go the log route. There isn‘t a super good reason, but the way I think about it is a bit of probability. When do I send messages to the wrong process? A few ideas are: - Manual debugging - Typoes - A Refactoring gone bad - Initial design got messed up - Erroneous third-party code that doesn‘t come from my precise development right away. Then the question is what are the consequences I want. - Manual debugging: do nothing, I‘m poking around - Typoes: I have to know about these ASAP - Refactoring gone bad: I have to know about these ASAP - Initial design got messed up: Something has to be loud and bad - Third party code: I want the third party to suffer. For these reasons, I tend to take the following approach: - In handle_call/3, I log the event with a string a bit like "mod=MYMOD at=handle_call warning=unexpected_call msg=~p" and then return `{noreply, State}` to force the caller to crash after a timeout. It‘s their fault, not mine. - In other callbacks, just log similar messages, replacing at=handle_call with at=handle_cast|info and warning=unexpected_cast|info. I can, from time to time, look at logs for ‘warning=unexpected_*‘ in logs and see if something is going weird. If it‘s something happening rarely, I‘m gonna have traces, but without the weird failures (unless it‘s a call). If it‘s something frequent, bugs will either show themselves differently, the log volume will be very high, and so on. It tends to give me what I need given the circumstances. It‘s not always as loud as I‘d expect (except for calls, which is ideal), but it tends to give me enough visibility for the occasional stray message, without compromising service levels.
handlle_call(Msg,_From,State) -> log:("mod=~p at=handle_call warning=unexpected_call msg=~p",[?MOdule,Msg]), {noreply, State};
强制调用者Crash,这是他们的错,不能怪我.然后反复查看Log中是不是存在‘warning=unexpected_*‘信息。
Running Dialyzer on my codebase:
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。