Forum Discussion

kendalladkins_5's avatar
kendalladkins_5
Icon for Nimbostratus rankNimbostratus
Nov 09, 2007

Variable Scope & Lifetime

I am relatively new to iRule development and I need a little help understanding variable scope. My main objective is to understand how I pass values from the HTTP_REQUEST event to the HTTP_RESPONSE event for a single HTTP handshake. Here is an example:

 

 

when HTTP_REQUEST {

 

if {[TCP::local_port] == 80} {

 

set protocol "http"

 

} else {

 

set protocol "https"

 

}

 

set url "$protocol://[HTTP::host][HTTP::uri]"

 

}

 

 

when HTTP_RESPONSE {

 

HTTP::respond 200 content "$url"

 

}

 

 

Here are some questions I have:

 

 

1. Will the above iRule always return the client's original request URL?

 

2. What is the scope and lifetime of a local variable?

 

3. What is the scope and lifetime of a global variable?

 

4. Should I use the global command to declare globals instead of set ::varname? There is significant tcl documentation that implies that this performs better.

 

5. Is there any documentation on this?

 

 

Thanks

 

3 Replies

  • Taking this one step further, I've just installed a working iRule onto a bigger machine that has 4 tmm's, and it gets a but cranky. Presumably with this setup "global" variables are specific to each instance of the tcl interpreter, rather than the logical iRule, making 4 sets of globals...

     

     

    Is there are way to truly share data between the 4 instances?

     

  • Deb_Allen_18's avatar
    Deb_Allen_18
    Historic F5 Account
    That's correct, global variables are not shared across TMMs, rather 1 is maintained per TMM, separately.

     

     

    From PD:

     

    Each TMM has it's own copy of the global variables.... That's why we added logic to the Tcl syntax checker to detect the use of globals and disable CMP for any virtuals that are using iRules that use global variables. However, the detection logic certainly isn't foolproof and if someone has disabled syntax checking or otherwise obfuscated their use of global variables, then we won't detect them and thus not disable CMP.This limitation will be removed in an upcoming release. Until then, the best way to share global data between TMMs is to store it in the session table using the session command: (Click here)

     

     

    HTH

     

    /deb