Erlang generic standard behaviours -- summary
gen_server 相关的片段分析得也差不多了, 这篇作为一个简要的总结.这一系列相关的分析暂且告一段落(之后如有必要,还会回来的 ^^ ),下一个系列主要是以pool 相关, 包括但不仅限于开源项目pooler, poolboy, emysql 中的pool 的管理, rabbitmq-server 中的worker_pool 角色等.
lapse request
对于gen_server handle_call callback, 主要处理同步请求,如果gen_server 进程在Timeout 时间内无法处理完成或根本来不及处理,这个时候就会出现失效的请求. 也就是当handle_call 在开始处理某请求时, 该请求的调用者早已经不再需要此次处理.
这样的需求并不一定是需要的,需要还是根据需求场景的不同定. 而避免此种情形出现的方式也比较简单,不着急,慢慢分析:
判断调用者alive
在gen module 处理call 请求的函数 do_call/4, 在将请求发送给目标gen_server 进程后, 请求调用者就会同步receive 目标gen_server 进程的返回, 超时Timeout 之后, 调用进程以exit(timeout) 退出.是的,等gen_server 进程在Timeout 时间内无法完成或根本来不及处理请求, 请求的调用者按照默认的方式, 就会exit. 也就是说,在没有try catch 的情况下,借助erlang:is_alive/0 API , 是可以判断当前handle_call 处理的请求是否是有效的.
但是, 如果使用了try catch 或者是catch 请求的调用进程就很有可能不按照预先的期望exit 退出了,这个时候, 这种方式就已经没有效果了.
monitored_by
还是在gen module 处理call 请求的函数 do_call/4,在发送给目标gen_server 进程请求之前, 请求的调用进程会首先monitor gen_server 进程,并在Timeout 之后demonitor. 也就是, 某请求如果还是有效的话, 在gen_server 进程的 monitored_by lists 中,应该还有请求的调用进程存在.
long-lived
通常情况下,Erlang process 更被期望于以short-lived 的方式存在.
An Erlang process starts with a small stack and heap in order to support a huge number of processes in a system. The size is configurable and the default value is 233 words. In general, Erlang processes are expected to short-lived and have small amounts of live data. When there is not enough free memory in the heap for a pro- cess, it is garbage collected, and if less memory can be freed than required it grows.
但是对于gen_server 来说,一般都是作为long-lived 的进程角色.对于long-lived 的进程角色,需要对其资源利用谨慎处理(通常情况下,是对于GC).
而优化的方案,需要根据应用场景的不同而不同,可供选的大致有:
1, hibernation
2, fullsweep_after
A non-negative integer which indicates how many times generational garbage collections can be done without forcing a fullsweep collection. In low-memory systems (especially without virtual memory), setting the value to
0
can help to conserve memory.
Riak 的官方文档中,对fullsweep_after 的参数设置为0.
3, min_heap_size
4, erlang:garbage_collect/1
参见RabbitMQ-server 中的background_gc module.
当然,最为理想的方案, 是尽可能将long-lived 的进程角色改为 short-lived . 参见riak_client 对gen_fsm 的使用.
参考
1, http://erlang.org/pipermail/erlang-questions/2011-October/061871.html
2, http://docs.basho.com/riak/latest/ops/advanced/configs/configuration-files/
3, http://www.erlang.org/doc/man/erlang.html#system_flag-2
4, Characterizing the Scalability of Erlang VM
5, Exploring Alternative Memory Architectures for Erlang- Implementation and Performance Evaluation (A How does the initial heap size affect the runtime? )
6, https://github.com/Eonblast/Emysql/pull/101
Erlang generic standard behaviours -- summary的更多相关文章
- Erlang generic standard behaviours -- gen
在分析 gen_server (或者是gen_fsm )之前,首先应该弄明白,gen 这个module . -module(gen). -compile({inline,[get_node/1]}). ...
- Erlang generic standard behaviours -- gen_server system msg
这是Erlang generic standard behaviors gen_server 分析的系列的最后一篇,主要分析gen_server module 辅助性的功能函数. 在gen_serve ...
- Erlang generic standard behaviours -- gen_server module
在分析完gen module (http://www.cnblogs.com/--00/p/4271386.html)之后,就可以开始进入gen_server 的主体module 了.gen_serv ...
- Erlang generic standard behaviours -- gen_server hibernate
hibernate 主要用于在内存空闲时,通过整理进程的stack,回收进程的heap 来达到回收内存节省资源的效果. hibernate 可用于OTP 进程以及普通进程, hibernate 的官方 ...
- Erlang generic standard behaviours -- gen_server noblock call
在Erlang 系统中,经常需要gen_server 进程来处理共享性的数据,也就是总希望一个gen_server 进程来为多个普通进程提供某种通用性的服务,这也是gen_server 设计的初衷.但 ...
- Erlang generic standard behaviours -- gen_server terminate
gen_server 主体 module 已经分析完了(http://www.cnblogs.com/--00/p/4271982.html),接着,分析下gen_server 中的terminate ...
- jQ1.5源码注释以及解读RE
jQ作为javascript的库( ▼-▼ ), 尽善尽美, 代码优美, 值得学习. 这一周平常上班没啥事也看jQ1.5的代码, 今天周六差不多看完了(Sizzle部分还没看), 重新看了一下, ...
- Java性能提示(全)
http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLi ...
- 《C++程序设计语言(英文第四版)》【PDF】下载
<C++程序设计语言(英文第四版)>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230382177 内容简介 本书是C++领域经典的参 ...
随机推荐
- JVM类加载器
系统中的类加载器 1.BootStrap ClassLoader a.启动ClassLoader b.加载rt.jar 2.Extension ClassLoader a.扩展ClassLoader ...
- Data Structure Binary Tree: Print ancestors of a given binary tree node without recursion
http://www.geeksforgeeks.org/print-ancestors-of-a-given-binary-tree-node-without-recursion/ #include ...
- 【leetcode刷题笔记】Single Number
题目: Given an array of integers, every element appears twice except for one. Find that single one. No ...
- Qt事件机制(是动作发生后,一种通知对象的消息,是被动与主动的总和。先处理自己队列中的消息,然后再处理系统消息队列中的消息)
Qt事件机制 Qt程序是事件驱动的, 程序的每个动作都是由幕后某个事件所触发.. Qt事件的发生和处理成为程序运行的主线,存在于程序整个生命周期. Qt事件的类型很多, 常见的qt的事件如下: 键盘事 ...
- zabbix实现mysql数据库的监控(三)
上面一章“zabbix实现mysql数据库的监控(二)”使用MPM来监控mysql,但是遇到安装问题始终解决不了,这里改用percona-monitoring-plugins进行zabbxi上监控my ...
- Tornado--基于H5图片的上传
日记 好久没有分享过东西,一直在学习状态,学的并不好很多东西都没有,也写了很多demo,后续整理出来在分享,就不分享了,不为什么因为今天周六,好不容易双休,大早上的一个人醒来,刷刷知乎,听音乐.分享一 ...
- echarts相关设置
1.显示隐藏工具栏 注释toolbox即可 /* toolbox: { show : true, feature : { dataView ...
- 阻挡ddos攻击的函数
- Mac 上Sublime Text 2配置lua环境
1,首先下载最新版lua 然后解压到你想解压到的位置 http://www.lua.org/ftp/ 2,运行终端,cd 进入该文件夹src目录下. 3,在终端输入 make macosx ...
- Codeforces 219D Choosing Capital for Treeland:Tree dp
题目链接:http://codeforces.com/problemset/problem/219/D 题意: 给你一棵树,n个节点. 树上的边都是有向边,并且不一定是从父亲指向儿子的. 你可以任意翻 ...