thttpd源码解析 定时器模块
- thttpd是非常轻量级的http服务器,可执行文件仅50kB。名称中的第一个t表示tiny,
 turbo, 或throttling
- 与lighttpd、memcached、redis相比非常小巧,仅有不到8k行,而后三者大小分别为:60k,13k,86k
- 支持HTTP/1.1和CGI;采用IO复用实现,单线程,可移植;实现了基于URL的文件流量限制功能
- 特别适用于大量静态数据访问的场景,如图片存储
- 2004年已经停止维护,有一个关于X-Forwarded-For HTTP header的bug。后来出现stthhpd基于此项目
- 性能比较参考对比
- 本文针对timer模块进行分析
timer模块
- 包括timer.h,timer.c两个文件
- 使用全局开放式散列表,默认大小67,每个hash节点上的值按照时间顺序排列
- ClientData定义如下:
typedef union {
  void* p;
  int i;
  long l;
  } ClientData;
 
- TimerProc类型声明如下:void
 TimerProc( ClientData client_data, struct timeval* nowP )。函数将在定时器超时时调用
- Timer结构定义如下:
typedef struct TimerStruct {
  TimerProc* timer_proc;
  ClientData client_data;
  long msecs;
  int periodic;
  struct timeval time;
  struct TimerStruct* prev;
  struct TimerStruct* next;
  int hash;
  } Timer;
 
- void tmr_init(
 void )
- Timer*
 tmr_create( struct timeval* nowP, TimerProc* timer_proc, ClientData client_data, long msecs, int periodic )- 
- 创建一个定时器,指定是一次性/周期性,加入散列表
- 定时器的时间设置为nowP的时刻加上msecs毫秒之后,若nowP为0,设置为当前时刻加上msecs毫秒
 
- timeval*
 tmr_timeout( struct timeval* nowP )- 
- 返回到下次触发的时间间隔
- 调用tmr_mstimeout得到
 
- tmr_mstimeout(
 struct timeval* nowP )- 
- 返回到下次触发时间间隔的毫秒数,即从nowP开始,经过多少毫秒hash表中会有一个定时器触发
- 因为hash表中的每个链表都是有序的,遍历一次hash表即可
 
- void tmr_run(
 struct timeval* nowP )- 
- 遍历hash表,如果定时器没有超时,调用timer_proc
- 如果定时器是周期性的,则调用后时间后延msecs,如果是非周期性的,则调用tmr_cancel去除
 
- void tmr_reset(
 struct timeval* nowP, Timer* timer )- 
- 重新开始运行定时器,时钟设置为当前时间nowP加上定时时长
 
- void tmr_cancel(
 Timer* timer )- 
- 释放定时器,由于tmr_run中对所有非周期性定时器都已经调用tmr_cancel,用户无需再自己对非周期定时器调用
- 将timers加入free_timers链表,节省free和malloc的开销,相当于一个缓冲池
 
- void tmr_cleanup(
 void )- 
- 清空定时器包,释放所有无用的内存:free_timers链表
 
- void tmr_destroy(
 void )- 
- 调用tmr_cancel释放所有定时器,为退出做准备,
 
- void tmr_logstats(
 long secs )- 
- 生成调试log信息,记录当前已分配、使用中、free的定时器个数
 
- 操作hash表的静态函数
- hash:由(time.tv_sec
 ^ time.tv_usec) % 67得到hash值
- l_add:插入一个定时器
- l_remove:移除一个定时器
- re_sort:定时器结构体含有之前的hash值,如果定时器的值改变,移除后重新计算hash,插入到正确的位置
 
timer模块的使用
- 在main函数中使用类timer模块
- 调用tmr_init初始化
- 创建周期为OCCASIONAL_TIME的周期定时器,回调函数为occasional
- 创建周期为5s的周期定时器,回调函数为idle
- 创建周期为THROTTLE_TIME的周期定时器,回调update_throttles
- 创建周期为STATS_TIME的周期定时器,回调show_stats
- 在主要事件处理循环中:
- 如果没有socket发生事件,调用一次tmr_run,continue
- 如果有新连接,continue,以保证新连接优先得到处理
- 如果有事件发生,则处理事件
- 运行一次tmr_run
 
- occasional
- 调用mmc_cleanup
- 调用tmr_cleanup,清除无用的定时器内存池
- 设置watchdog_flag = 1,使watchdog知道程序仍在运行
 
- idle
- update_throttles 更新流量控制
- show_stats
  
  
转载请注明作者:Focustc,博客地址为http://blog.csdn.net/caozhk,原文链接为点击打开
  
  thttpd源码解析 定时器模块,布布扣,bubuko.com
thttpd源码解析 定时器模块
原文:http://blog.csdn.net/caozhk/article/details/38491045