THTTPD Source Code Parsing timer module
- THTTPD is a very lightweight httpserver, can run files only 50kB. The first of the names
t represents tiny, turbo, or throttling
- Small compared to lighttpd, memcached, and Redis, there are only less than 8k lines, and the latter three sizes are: 60k,13k,86k
- Support for http/1.1 and CGI; implementation of IO multiplexing, single threaded, portable; URL-based file traffic throttling capability
- Ideal for a large number of static data access scenarios, chip storage
- 2004 has stopped maintenance, there is a bug about X-forwarded-for HTTP header. Later appeared STTHHPD based on this project
- Comparison of performance comparison
- This paper analyzes the timer module
Timer module
- Contains timer.h,timer.c of two files
- Use a global open hash list. The default size is 67, and the values on each hash node are in chronological order
- Clientdata definitions such as the following:
typedef union { void* p; int i; long l; } ClientData;
- The Timerproc type declaration is for example the following:
void TimerProc( ClientData client_data, struct timeval* nowP ) .function will be called at Timer timeout
- timer structure definitions such as the following:
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, specify a one-time/Periodic, add hash list
-
timeval* tmr_timeout (struct timeval* nowp)
- Return to the next triggered interval
- call tmr_mstimeout get
tmr_mstimeout (struct timeval* nowp)
- Returns the number of milliseconds to the next trigger interval. That is, from NOWP start, after how many milliseconds the hash table will have a timer trigger
- because every list in the hash table is ordered, You can iterate through the hash table once
void tmr_run (struct timeval* nowp)
- traverse the hash table. Assuming that the timer does not time out, call Timer_proc
- Assuming the timer is cyclical, the call is deferred after the time msecs, If the hypothesis is non-cyclical, call Tmr_cancel to remove
void tmr_reset (struct timeval* nowp, timer* Timer)
- and start the timer again. Clock set to current time NOWP plus timing duration
void tmr_cancel (timer* Timer)
- release timer because Tmr_run is already called Tmr_cancel for all aperiodic timers, The user does not need to call the
-
void tmr_cleanup( void )
- Empty the timer pack. Free all useless memory: free_timers linked list
void tmr_destroy( void )
- Call Tmr_cancel to release all timers, prepare for exit,
void tmr_logstats (long secs)
- Generate debug log information that records the number of timers currently allocated, in use, and free
- Static functions for manipulating hash tables
- Hash: By
(time.tv_sec ^ time.tv_usec) % 67 getting the hash value
- L_add: Inserting a timer
- L_remove: Remove a timer
- Re_sort: The timer structure contains the previous hash value, assuming that the value of the timer changes, after the removal of the calculation of the hash, inserted into the correct position
Use of the Timer module
- Use the class Timer module in the main function
- Call Tmr_init Initialization
- Create a periodic timer with a period of occasional_time, and a callback function of occasional
- Creates a periodic timer with a period of 5s, and the callback function is idle
- Create a periodic timer with a period of throttle_time. Callback Update_throttles
- Create a periodic timer with a period of stats_time, callback Show_stats
- In the main Event processing loop:
- Suppose there is no socket event. Call once Tmr_run,continue
- Suppose there is a new connection, continue. To ensure that new connections are processed first
- If an event occurs, the event is handled
- Perform Tmr_run once
- Occasional
- Call Mmc_cleanup
- Call Tmr_cleanup, clear the unused timer memory pool
- Set Watchdog_flag = 1 To make watchdog aware that the program is still executing
- Idle
- Update_throttles Update Flow control
- Show_stats
- Call the function Logstats. Record information
Reprint please specify FOCUSTC, blog address is http://blog.csdn.net/caozhk. Click to open the original link
THTTPD Source Code Parsing timer module