Forum Discussion
Nathan_McMahon_
May 05, 2005Historic F5 Account
Direct requests based on hashed cookie data
I have a requirement to pull a numerical value from a cookie and direct the request to one of 12 pools based on the hashed value of that cookie data. For example, a client's cookie contains 62. Dividing by 12 leaves a remainder of 2, so the request would be sent to pool_2. The idea is that the each of the server pools would then only need to cache 1/12 of the total sum of the application data. The initial cookie generation is in place today, with Apache proxies to perform this persistence mechanism. I'm hoping to remove the extra layer of Apache proxies with this rule.
when HTTP_REQUEST {
set CookieId [HTTP::cookie User_cookie]
set TempCookieId [expr $CookieId / 12.0]
set TempCookieId2 [expr floor $TempCookieId]
set NewCookieId [expr 12 * ($TempCookieId - $TempCookieId2)]
then send the connection to pool $NewCookieId
}
I'm running into a number of problems, mostly centered around string to integer or floating point. Using 12 instead of 12.0 doesn't error out, but my attempt at logic is to capture the remainder. Using the 12.0 I find %g via the logs. Also, I'm not sure what the best workaround is in place of the floor command (dropping off everything after the decimal point, perhaps findstr?) as it seems this is an unsupported command.
Is there a better way to generate this hash mechanism, am I close to being on the right track? Any suggestions are greatly appreciated in advance.
Nathan
- rapmaster_c_127Historic F5 AccountWhat does your cookie data look like? Any particular reason you're using floor and divides instead of modular division? (The % operator.)
- Aren't you just looking for the modulus operator? If all you want is the remainder from the first number divided by the second, you could just use the % operator. In your example, 62 % 12 would return 2. Here's an iRule as a reference.
when HTTP_REQUEST { set CookieId [HTTP::cookie User_cookie] if { [string is integer $CookieId] } { set PoolId [expr $CookieId % 12 ] set PoolName "Pool_$PoolId" pool $PoolName log "cookieid_rule: CookieId = $CookieId; PoolId = $PoolId; PoolName = $PoolName" } else { log "cookieid_rule: Invalid CookieId - not in integer format!" } }
Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects