Forum Discussion
iRule Variables and Connection Re-Use
Hi John,
TCL session variables will be keept during the duration of the underlying TCP connection. So if you [set variable 1] conditionally and laster use a [info exists variable] to handle those conditional requests, then you have to make sure that you [unset variable] after each single request or at least just within the [info exists variable] code structure.
when HTTP_REQUEST {
set HOST [HTTP::host]
set PATH [HTTP::path]
if { [class match $PATH eq www_redirects] } {
set ACTION "redirect"
set VALUE [class match -value $PATH equals www_redirects]
}
if { [info exists ACTION] } {
if { $ACTION eq "redirect" } {
HTTP::respond 301 "Location" "https://$HOST$VALUE"
}
unset $ACTION
}
}
But the most effective way to support your scenario would be:
- Reduce $variable usage to an absolute minimum (each of them is eating your CPU and RAM).
- Avoid [info exists] since it has a somewhat high overhead. You may use [set] on the very beginning of a request to always set/overwrite $variable to a default value (e.g. "") and then check if $variable has a non-default value to handle the conditioal requests.
- Try to use nested control structures to speed up the execution. Repetive code blocks may look slow, but they are much faster compared to setting variables and then evaluate a variable to trigger additional code blocks.
- Reduce the [class match] executions to a minimum by skipping the initial data-group entry exist checks. Its far more effective to directly pull the -value for a given data-group entry and then just check if a value could be retrived or not (aka. value not equal ""). While doing so, you may store the output into a $variable for further usage.
Based on the recommendations the most effective code for you scenario would look like that:
when HTTP_REQUEST { if { [set class_uri [class lookup -value [HTTP::path] equals www_redirects]] ne "" } then { HTTP::respond 301 "Location" "https://[HTTP::host]$class_uri" } }
Note: The iRule will query the data-group www_redirects a single time per request and saves the output to $class_uri. If the data-group query got some data back (aka. $class_uri not equal ""), it will directly throw a 301 redirect to the location https://[HTTP::host]$class_uri.
Cheers, Kai
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
* 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
