Thttpd source code parsing timer Module

Source: Internet
Author: User
Thttpd source code parsing timer Module
  • Thttpd is a lightweight HTTP server with a executable file of only 50 kb. The first in the nametTiny, Turbo, or throttling
  • Compared with Lighttpd, memcached, and redis, they are very small, with less than 8 K rows, and the last three are respectively 60 K, 13 K, and 86 K.
  • Supports HTTP/1.1 and CGI; implements Io multiplexing, single-thread, and portable; implements the URL-based file traffic Limit Function
  • It is particularly suitable for scenarios where a large number of Static Data Accesses exist.
  • Maintenance has been stopped on April 9, 2004. There is a bug about X-forwarded-for HTTP header. Later, stthhpd was developed based on this project.
  • Performance Comparison
  • This article analyzes the timer Module
Timer Module
  • Includes two files: timer. h and timer. C.
  • Globally open hash is used. The default size is 67. Values on each hash node are arranged in chronological order.
  • Clientdata is defined as follows:
    typedef union {  void* p;  int i;  long l;  } ClientData;
  • The timerproc type declaration is as follows:void TimerProc( ClientData client_data, struct timeval* nowP ). The function will be called when the timer times out.
  • The timer structure is defined as follows:
    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 )
    • Initialize the timer package, that is, the timer hash table.
  • Timer* tmr_create( struct timeval* nowP, TimerProc* timer_proc, ClientData client_data, long msecs, int periodic )
    • Create a timer, which is a one-time/periodic task. Add the timer to the hash table.
    • The timer time is set to the nowp time plus msecs milliseconds. If nowp is set to 0, it is set to the current time plus msecs milliseconds.
  • timeval* tmr_timeout( struct timeval* nowP )
    • Returns the interval from the next trigger.
    • Call tmr_mstimeout to obtain
  • tmr_mstimeout( struct timeval* nowP )
    • Returns the number of milliseconds from the next trigger interval, that is, the number of milliseconds from nowp, after which a timer is triggered in the hash table.
    • Because each linked list in the hash table is ordered, just traverse the hash table once.
  • void tmr_run( struct timeval* nowP )
    • Traverse the hash table. If the timer does not time out, call timer_proc.
    • If the timer is periodic, msecs is extended after the call time. If the timer is non-cyclical, tmr_cancel is called to remove
  • void tmr_reset( struct timeval* nowP, Timer* timer )
    • Run the timer again, and set the clock to the current time nowp plus the timer duration.
  • void tmr_cancel( Timer* timer )
    • Release the timer. Because tmr_cancel has been called for all non-periodic timers in tmr_run, you no longer need to call the non-periodic timer.
    • Adding timers to the free_timers linked list saves free and malloc overhead, which is equivalent to a buffer pool.
  • void tmr_cleanup( void )
    • Clear the timer package and release all useless memory: free_timers linked list
  • void tmr_destroy( void )
    • Call tmr_cancel to release all timers to prepare for exit,
  • void tmr_logstats( long secs )
    • Generate debugging log information and record the number of currently allocated, in-use, and free timers
  • Operate static functions of a hash table
    • Hash:(time.tv_sec ^ time.tv_usec) % 67Obtain the hash value.
    • Rochelle Add: Insert a timer
    • Rochelle remove: removes a timer.
    • Re_sort: the timer struct contains the previous hash value. If the timer value changes, remove it and re-calculate the hash value and insert it to the correct position.
Use of timer Module
  • Use timer-like modules in the main function
  • Call tmr_init for initialization
  • Create a periodic timer with a period of occasional_time. The callback function is occasional.
  • Create a timer with a cycle of 5 seconds and the callback function is idle.
  • Creates a periodic timer with a cycle of throttle_time and calls back update_throttles.
  • Creates a periodic timer with a period of stats_time and calls back show_stats.
  • In the main event processing cycle:
    • If no socket event occurs, call tmr_run and continue once.
    • If a new connection exists, continue to ensure that the new connection takes priority.
    • If an event occurs, the event is processed.
    • Run tmr_run once
  • Occasional
    • Call mmc_cleanup
    • Call tmr_cleanup to clear useless timer Memory Pool
    • Set watchdog_flag = 1 so that watchdog knows that the program is still running.
  • Idle
  • Update_throttles update traffic control
  • Show_stats
    • Call the logstats function to record information.

  
  

For more information, see focustc. The blog address is http://blog.csdn.net/caozhk.
  
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.