Forum Discussion

abeny_894's avatar
abeny_894
Icon for Nimbostratus rankNimbostratus
Dec 01, 2008

Session can't release

Dears,

 

 

I have got the iRules for session control the HTTP request from customer. However I found that the counter sometimes can't release after OVER the said limit even the iRules contains "set total_active_clients to 0" in CLIENT_CLOSED. As the iRules is so critical for my client also i am not so professional as you guys in iRules, I would like to seek you guys help for take a look on it? Moreover, any suggestion or modification for this iRules?

 

 

**********************************************************when RULE_INIT {

 

set ::total_active_clients_a 0

 

set ::max_active_clients_a 10

 

set ::total_active_clients_b 0

 

set ::max_active_clients_b 10

 

}

 

 

 

 

when HTTP_REQUEST {

 

switch [HTTP::host] {

 

abc.com {

 

log local0. "Access to abc.com"

 

set site a

 

incr ::total_active_clients_a

 

log local0. "total requests are $::total_active_clients_a"

 

set cur_time_a [clock seconds]

 

set start_time_a $cur_time_a

 

if { $cur_time_a == $start_time_a } {

 

if { $::total_active_clients_a <= $::max_active_clients_a } {

 

HTTP::redirect "http://www.yahoo.com.hk"

 

return

 

}

 

}

 

}

 

xyz.com {

 

log local0. "Access to xyz.com"

 

set site b

 

set cur_time_b [clock seconds]

 

incr ::total_active_clients_b

 

log local0. "total requests are $::total_active_clients_b"

 

set cur_time_b [clock seconds]

 

set start_time_b $cur_time_b

 

if { $cur_time_b == $start_time_b } {

 

if { $::total_active_clients_b >= $::max_active_clients_b } {

 

HTTP::redirect "http://www.google.com/"

 

return

 

}

 

}

 

}

 

}

 

}

 

 

 

 

when HTTP_RESPONSE {

 

log local0. "site is $site"

 

if { $site == "a" } {

 

log local0. "site a is true"

 

set cur_time_for_cookie [clock seconds]

 

log local0. "cookie value for site a is $cur_time_for_cookie"

 

set cookie_value_crc [crc32 $cur_time_for_cookie]

 

log local0. "crc32 is $cookie_value_crc"

 

HTTP::cookie insert name "site_a_cookie" value "$cookie_value_crc" domain "a.com"

 

HTTP::cookie encrypt "site_a_cookie" password

 

HTTP::cookie expires "site_a_cookie" 50000 relative

 

} elseif { $site == "b" } {

 

log local0. "site b is true"

 

set cur_time_for_cookie [clock seconds]

 

log local0. "cookie value for site b is $cur_time_for_cookie"

 

set cookie_value_crc [crc32 $cur_time_for_cookie]

 

log local0. "crc32 is $cookie_value_crc"

 

HTTP::cookie insert name "site_b_cookie" value "$cur_time_for_cookie" domain "b.com"

 

HTTP::cookie encrypt "site_b_cookie" password

 

HTTP::cookie expires "site_b_cookie" 50000 relative

 

}

 

}

 

 

 

 

when CLIENT_CLOSED {

 

log local0. "site $site request closed"

 

if { $site == "a" } {

 

incr ::total_active_clients_a -1

 

if { $::total_active_clients_a <= 0 } {

 

set ::total_active_clients_a 0

 

}

 

} elseif { $site == "b" } {

 

incr ::total_active_clients_b -1

 

if { $::total_active_clients_b <= 0 } {

 

set ::total_active_clients_b 0

 

}

 

}

 

}

 

**********************************************************

 

 

  • Colin_Walker_12's avatar
    Colin_Walker_12
    Historic F5 Account
    It looks like you're only conditionally setting the active_clients to 0. Are you sure those conditions are being met when you expect them to be? Have you tried adding logging inside those if/elseif sections?

     

     

    Colin
  • Thanks Colin, I observed that the log will show the accumulate value and it will decrease normally to 0 when CLIENT CLOSE the session. It is the normal expected result.

     

     

    However, I found that sometimes the value can't decrease if the request is over than the total_active_client and the log show the session still keep the value not reset to 0. So strange that the iRules got 2 different behavior.

     

     

    Is there need any changes for this iRules to make it stable? Please kindly help, thank you very much.
  • hoolio's avatar
    hoolio
    Icon for Cirrostratus rankCirrostratus
    Hi Abeny,

     

     

    It looks like you're incrementing the connection count on every HTTP request but only decrementing the count when the TCP connection is closed. As clients can make multiple HTTP requests over a single TCP connection, this might be where your count is going off. You could either assume there is one client per TCP connection (no proxies) and not increment the count in HTTP_REQUEST. Or maybe you could decrement the total count by the number of HTTP requests on the TCP connection in CLIENT_CLOSED.

     

     

    Aaron

     

     

  • Thanks Aaron and your suggestion. By the way, I know what you means but sorry that i have no idea on how to make this iRules "decrement the total count by the number of HTTP requests on the TCP connection in CLIENT_CLOSED"

     

     

    Is there any examples for me to study or would you kindly to give me some hints for edit this iRules? Many thanks.
  • Colin_Walker_12's avatar
    Colin_Walker_12
    Historic F5 Account
    To decrement by the total number of requests made you would simply increment a counter on every HTTP_REQUEST event in that given connection. Then in the CONNECTION_CLOSED event you'd decrement the connection count by that number, rather than just by one. This way it would decrement the count by the same number that it was incremented, as it's being incremented once per HTTP_REQUEST.

     

     

    Colin
  • Thanks Colin. It sounds great. Um...For iRules, is it means that i need add CONNECTION_CLOSED event under [HTTP::host] for each domain and create a new counter on every HTTP_REQUEST??

     

     

    By the way, is it possible to give me some example for this iRules look like?? Thank you very much for your help
  • Colin_Walker_12's avatar
    Colin_Walker_12
    Historic F5 Account
    It would look almost identical to what you have now. You'd just add a counter value under the HTTP_REQUEST section that's used and then emptied in the CLIENT_CLOSED event.

    Something like:

     
     when HTTP_REQUEST { 
       incr requestCount -1 
     ... 
     

    And then in your client closed section, instead of incrementing by -1, you'd increment by $requestCount.

     
     when CLIENT_CLOSED { 
       log local0. "site $site request closed" 
       if { $site == "a" } { 
         incr ::total_active_clients_a $requestCount 
         set requestCount 0 
         if { $::total_active_clients_a <= 0 } { 
           set ::total_active_clients_a 0 
         } 
     ... 
     

    That way you're always decrementing the "total_active_clients" value by the number of requests that were put through on this connection.

    Make sense?

    Colin