# Session can't release

**URL:** <https://community.f5.com/t/session-cant-release/38158>\
**Category:** Technical Forums\
**Tags:** dev, irules, devops, application-delivery\
**Created:** [December 1, 2008, 1:39am UTC](https://community.f5.com/t/session-cant-release/38158 "2008-12-01T01:39:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![abeny\_894](https://avatars.discourse-cdn.com/v4/letter/a/ecb155/32.png) [@abeny\_894](https://community.f5.com/u/abeny_894)\
**Post date:** [December 1, 2008, 1:39am UTC](https://community.f5.com/t/session-cant-release/38158/1 "2008-12-01T01:39:33Z")

</div>

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](http://abc.com) {

log local0. “Access to [abc.com](http://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](http://www.yahoo.com.hk)”

return

}

}

}

[xyz.com](http://xyz.com) {

log local0. “Access to [xyz.com](http://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/](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://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://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

}

}

}

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

---

<div class="post-metadata">

**Author:** ![Colin\_Walker\_12](https://avatars.discourse-cdn.com/v4/letter/c/a88e4f/32.png) [@Colin\_Walker\_12](https://community.f5.com/u/Colin_Walker_12)\
**Post date:** [December 2, 2008, 12:43am UTC](https://community.f5.com/t/session-cant-release/38158/2 "2008-12-02T00:43:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![abeny\_894](https://avatars.discourse-cdn.com/v4/letter/a/ecb155/32.png) [@abeny\_894](https://community.f5.com/u/abeny_894)\
**Post date:** [December 2, 2008, 1:15am UTC](https://community.f5.com/t/session-cant-release/38158/3 "2008-12-02T01:15:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![hoolio](https://avatars.discourse-cdn.com/v4/letter/h/f6c823/32.png) [@hoolio](https://community.f5.com/u/hoolio)\
**Post date:** [December 2, 2008, 8:11am UTC](https://community.f5.com/t/session-cant-release/38158/4 "2008-12-02T08:11:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![abeny\_894](https://avatars.discourse-cdn.com/v4/letter/a/ecb155/32.png) [@abeny\_894](https://community.f5.com/u/abeny_894)\
**Post date:** [December 3, 2008, 12:49am UTC](https://community.f5.com/t/session-cant-release/38158/5 "2008-12-03T00:49:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Colin\_Walker\_12](https://avatars.discourse-cdn.com/v4/letter/c/a88e4f/32.png) [@Colin\_Walker\_12](https://community.f5.com/u/Colin_Walker_12)\
**Post date:** [December 3, 2008, 3:29am UTC](https://community.f5.com/t/session-cant-release/38158/6 "2008-12-03T03:29:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![abeny\_894](https://avatars.discourse-cdn.com/v4/letter/a/ecb155/32.png) [@abeny\_894](https://community.f5.com/u/abeny_894)\
**Post date:** [December 3, 2008, 3:57am UTC](https://community.f5.com/t/session-cant-release/38158/7 "2008-12-03T03:57:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin\_Walker\_12](https://avatars.discourse-cdn.com/v4/letter/c/a88e4f/32.png) [@Colin\_Walker\_12](https://community.f5.com/u/Colin_Walker_12)\
**Post date:** [December 3, 2008, 4:02am UTC](https://community.f5.com/t/session-cant-release/38158/8 "2008-12-03T04:02:10Z")

</div>

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:

```tcl
when HTTP_REQUEST { 
   incr requestCount -1 
 ...

```

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

```tcl
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
