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