Ok. So I kinda stepped back and re thought how to approach this and this is what I have came up with. Instead of usinig connection limits, I'm using session limits. Please take a look at this Irule and tell me if you see any issues or ways to optimize this. Also I don't believe it's possible, but is there anyway to do this with a session based cookie intead of using a timout value?
I have briefly tested this and it seems to work. I would love to hear how I can improve this and or if anyone sees any errors? Also if it's possible to utilize "session Cookies"?
when RULE_INIT {
set static::max_active_clients_1 1500
set static::max_active_clients_2 100
set static::sessionCookiePrefix "session"
set static::sessionTimeout 300
set static::sorrypage {
We're Sorry
We're Sorry. Our servers are currently over capacity and certain areas of our site may be temporarily unavailable. We're working to resolve the issue as quickly as possible.
}
}
when CLIENT_ACCEPTED {
if { ![matchclass [IP::remote_addr] equals $::privatenet] } {
drop
}
}
when HTTP_REQUEST {
set my_host_header [HTTP::host]
if { [matchclass [IP::remote_addr] equals $::privatenet] } {
pool pl_x.x.x.x_http_internal
}
switch -glob [string tolower [HTTP::host]] {
"www.test1.gov*" {
set subtableName "sessionLimit-[virtual name]"
set sessionCookieName1 "$static::sessionCookiePrefix-TD-[virtual name]"
set need_cookie 0
if {[HTTP::cookie exists $sessionCookieName1]} {
set client_id [HTTP::cookie $sessionCookieName1]
set sessiondata [table lookup -subtable $subtableName $client_id]
if { $sessiondata != "" } {
return
}
}
set sessionCount [table keys -subtable $subtableName -count]
if {$sessionCount < $static::max_active_clients_1} {
set need_cookie 1
set client_id [format "%08d" [expr { int(100000000 * rand()) }]]
set sessionValue [IP::client_addr]
table add -subtable $subtableName $client_id $sessionValue $static::sessionTimeout
log local0. "New Session ($client_id) added value $sessionValue Timeout $static::sessionTimeout"
} else {
HTTP::respond 200 content $static::sorrypage
}
}
"www.test2.gov*" {
set subtableName "sessionLimit-[virtual name]"
set sessionCookieName2 "$static::sessionCookiePrefix-2-[virtual name]"
set need_cookie 0
if {[HTTP::cookie exists $sessionCookieName2]} {
set client_id [HTTP::cookie $sessionCookieName2]
set sessiondata [table lookup -subtable $subtableName $client_id]
if { $sessiondata != "" } {
return
}
}
set sessionCount [table keys -subtable $subtableName -count]
if {$sessionCount < $static::max_active_clients_2} {
set need_cookie 1
set client_id [format "%08d" [expr { int(100000000 * rand()) }]]
set sessionValue [IP::client_addr]
table add -subtable $subtableName $client_id $sessionValue $static::sessionTimeout
} else {
HTTP::respond 200 content $static::sorrypage
}
}
}
}
when HTTP_RESPONSE {
switch -glob [string tolower $my_host_header] {
"www.test1.gov*" {
if {$need_cookie == 1} {
HTTP::cookie insert name $sessionCookieName1 value $client_id path "/"
}
}
"www.test2.gov*" {
if {$need_cookie == 1} {
HTTP::cookie insert name $sessionCookieName2 value $client_id path "/"
}
}
}
}