1 |
rule HTTP_session_limit {<br /> when RULE_INIT {<br /> set ::total_active_clients 0<br /> set ::max_active_clients 100<br /> log local0. "rule session_limit initialized: total/max: $::total_active_clients/$::max_active_clients"<br /> }<br /> when HTTP_REQUEST {<br /> ;# test cookie presence<br /> if {[HTTP::cookie exists "ClientID"]} {<br /> set need_cookie 0<br /> set client_id [HTTP::cookie "ClientID"]<br /> ;# if cookie not present & connection limit not reached, set up client_id<br /> } else {<br /> if {$::total_active_clients < $::max_active_clients} {<br /> set need_cookie 1<br /> set client_id [format "%08d" [expr { int(100000000 * rand()) }]]<br /> incr ::total_active_clients<br /> ;# otherwise redirect<br /> } else {<br /> HTTP::redirect "http://sorry.domain.com/"<br /> return<br /> }<br /> }<br /> }<br /> when HTTP_RESPONSE {<br /> ;# insert cookie if needed<br /> if {$need_cookie == 1} {<br /> HTTP::cookie insert name "ClientID" value $client_id path "/"<br /> }<br /> }<br /> when CLIENT_CLOSED {<br /> ;# decrement current connection counter for this client_id<br /> if {$::total_active_clients > 0} {<br /> incr ::total_active_clients -1<br /> }<br /> }<br /> }<br />这个rule主要是限制http<strong>并发</strong>总量,一个连接产生的计数在访问结束时自动被清0,因而可用来计算并发。 |
1 |
当并发总数达到限制后,没有cookie的新建链接则无法正常访问。一次访问只产生一个cookie,只要浏览器不关闭 |
1 |
cookie就没有失效,对这个人而言他后续的点击不再累加到连接数里。看下面日志: |
1 |
<table cellspacing="0" cellpadding="2" width="100%"><tbody><tr><td class="Severity4">18:40 </td><td class="Severity4">192.168.162.254 </td><td class="Severity4" nowrap="nowrap">warnings </td><td class="Severity4">tmm tmm[1045]: Rule test_length: 有cookie:05290852,当前连接0 </td></tr><tr><td class="Severity4">18:40 </td><td class="Severity4">192.168.162.254 </td><td class="Severity4" nowrap="nowrap">warnings </td><td class="Severity4">tmm tmm[1045]: Rule test_length: 有cookie:05290852,当前连接0 </td></tr><tr><td class="Severity6"><font color="#ff0000">18:39 </font></td><td class="Severity6"><font color="#ff0000">192.168.162.254 </font></td><td class="Severity6" nowrap="nowrap"><font color="#ff0000">informational </font></td><td class="Severity6"><p><font color="#ff0000">这是首次打开首页,最终是为0。上面黑色日志,是后来刷新页面的,不产生</font></p><p><font color="#ff0000">计数。</font></p><p><font color="#ff0000">tmm tmm[1045]: Rule test_length: 关闭了减少一个连接,当前0 </font></p></td></tr><tr><td class="Severity4"><font color="#ff0000">18:39 </font></td><td class="Severity4"><font color="#ff0000">192.168.162.254 </font></td><td class="Severity4" nowrap="nowrap"><font color="#ff0000">warnings </font></td><td class="Severity4"><font color="#ff0000">tmm tmm[1045]: Rule test_length: 有cookie:05290852,当前连接1 </font></td></tr><tr><td class="Severity4"><font color="#ff0000">18:39 </font></td><td class="Severity4"><font color="#ff0000">192.168.162.254 </font></td><td class="Severity4" nowrap="nowrap"><font color="#ff0000">warnings </font></td><td class="Severity4"><font color="#ff0000">tmm tmm[1045]: Rule test_length: 有cookie:05290852,当前连接1 </font></td></tr><tr><td class="Severity4"><font color="#ff0000">18:39 </font></td><td class="Severity4"><font color="#ff0000">192.168.162.254 </font></td><td class="Severity4" nowrap="nowrap"><font color="#ff0000">warnings </font></td><td class="Severity4"><font color="#ff0000">tmm tmm[1045]: Rule test_length: 有cookie:05290852,当前连接1 </font></td></tr><tr><td class="Severity4"><font color="#ff0000">18:39 </font></td><td class="Severity4"><font color="#ff0000">192.168.162.254 </font></td><td class="Severity4" nowrap="nowrap"><font color="#ff0000">warnings </font></td><td class="Severity4"><font color="#ff0000">tmm tmm[1045]: Rule test_length: 有cookie:05290852,当前连接1 </font></td></tr><tr><td class="Severity4"><font color="#ff0000">18:39 </font></td><td class="Severity4"><font color="#ff0000">192.168.162.254 </font></td><td class="Severity4" nowrap="nowrap"><font color="#ff0000">warnings </font></td><td class="Severity4"><font color="#ff0000">tmm tmm[1045]: Rule test_length: 有cookie:05290852,当前连接1 </font></td></tr><tr><td class="Severity4"><font color="#ff0000">18:39 </font></td><td class="Severity4"><font color="#ff0000">192.168.162.254 </font></td><td class="Severity4" nowrap="nowrap"><font color="#ff0000">warnings </font></td><td class="Severity4"><font color="#ff0000">tmm tmm[1045]: Rule test_length: 有cookie:05290852,当前连接1 </font></td></tr><tr><td class="Severity4"><font color="#ff0000">18:39 </font></td><td class="Severity4"><font color="#ff0000">192.168.162.254 </font></td><td class="Severity4" nowrap="nowrap"><font color="#ff0000">warnings </font></td><td class="Severity4"><font color="#ff0000">tmm tmm[1045]: Rule test_length: 有cookie:05290852,当前连接1</font> </td></tr></tbody></table> |
1 |
<strong>分析下面这个代码:</strong> |
1 |
when RULE_INIT { |
1 |
触发建立一个数组,irule是什么时候被触发??!第一次访问触发,后面再刷新浏览器会触发吗。重新访问会触发吗?<br /> array set ::active_clients { }<br />}<br />when CLIENT_ACCEPTED {<br /> set client_ip [IP::remote_addr]<br /> puts "starting client---------$client_ip" |
1 |
判断,如果数组元素变量::active_clients($client_ip)存在(第一次访问的时候这个数组肯定不存在,没人给这个数组元素赋值)<br /> if { [info exists ::active_clients($client_ip)] } {<br /> puts "origin connection is ==== $::active_clients($client_ip)" |
1 |
如果存在,判断这个数组元素变量,如果大于指定的值则拒绝,否则加+1<br /> if {$::active_clients($client_ip) > 3 } {<br /> reject<br /> puts "client connection is reject"<br /> return<br /> } else {<br /> incr ::active_clients($client_ip)<br /> puts "bynow connection is ==== $::active_clients($client_ip)"<br /> }<br /> } else { |
1 |
第一次,给数组元素赋值<br /> puts "client connection is the first one"<br /> set ::active_clients($client_ip) 1<br /> }<br />} |
1 |
在事件最后,这个总被触发<strong>This event is fired at the end of any client connection, regardless of protocol<br /></strong>when CLIENT_CLOSED {<br /> puts "closing_________[IP::remote_addr]"<br /> if { [info exists ::active_clients($client_ip)] } {<br /> incr ::active_clients($client_ip) -1<br /> if { $::active_clients($client_ip) <= 0 } {<br /> unset ::active_clients($client_ip)<br /> }<br /> }<br />}<br /> |
这段代码,是分析一
文章评论