clazba
Mar 09, 2011Nimbostratus
HTTP Request Throttle, how to log?
Hi,
I am using a slightly modded version of the HTTP Request Throttle irule found on http://devcentral.f5.com/wiki/default.aspx/iRules/HTTPRequestThrottle.html:
when RULE_INIT {
set static::maxRate 60
set static::windowSecs 30
set static::timeout 60
set static::log_interval_vip1 2
set static::redirect_url_vip1 "http://some.site.com"
}
when HTTP_REQUEST {
set myUserID "user"
set currentTime [clock seconds]
set windowStart [expr {$currentTime - $static::windowSecs}]
set postCount 0
set timeTable "TS-limited_uri"
set key "ST-limited_uri"
table add -subtable $timeTable $key [clock seconds] indef indef
set log_timestamp [table lookup -subtable $timeTable $key]
foreach { requestTime } [table keys -subtable "PH:${myUserID}"] {
if { $requestTime > $windowStart } {
incr postCount 1
} else {
table delete -subtable "PH:${myUserID}" $requestTime
}
}
if {[expr {$currentTime - $log_timestamp}] > $static::log_interval_vip1 } {
table set -subtable $timeTable $key [clock seconds] indef indef
log -noname local0. "Rate: ($postCount) !!!"
}
if { $postCount < $static::maxRate } {
set requestID "PH:${myUserID}"
table set -subtable $requestID $currentTime "ignored" $static::timeout
} else {
HTTP::redirect $static::redirect_url_vip1
return
}
}
The irule works a treat and I have tried introducing some logging that would give an indication of the rate of reqs/s but when I log the value of postCount it will keep rising until it its the vales set for static::windowSecs. i.e
set static::windowSecs 30 == local/tmm info tmm[18771]: Rate: (29)
set static::windowSecs 60 == local/tmm info tmm[18771]: Rate: (59)
Any suggestions on how to do this properly?
Cheers,
Claud