Example of Static Variables with Virtual Server specific settings

Problem this snippet solves:

When you have an iRule that requires some virtual server specific "setting" that is configured in RULE_INIT; unless you have a different version of the iRule for each virtual server where you use it, it can be tricky to manage the $static:: variables, because they are shared globally. This is an example solution for that problem where you embed the virtual server name in the static variable and then when executing your code, you leverage the [virtual name] command inside the use of the $static:: variable (via [subst ...]) to use the appropriate virtual server-specific setting.

How to use this snippet:

This is a proof of concept and not useful, but to try the proof of concept, you'd apply this to any virtual server with an http profile.

Code :

when RULE_INIT {
    set static::virtualLimit_http_test "25"
    set static::virtualLimit_https_test "50"
}
 
when HTTP_REQUEST {
    # code below is becuase [virtual name] will return partition/folder name (example "/Common/http_test")
    set virtualname [lindex [split [virtual name] \/] 2]
    log "virtualname: $virtualname"
    set limit [subst "\$static::virtualLimit_$virtualname"]
    log "Static Variable: $limit"
    HTTP::respond 200 content "OK - Limit Variable = $limit"
}

Tested this on version:

13.0
Published Sep 11, 2020
Version 1.0
  • Hi Chad, 

    you may think about using static::array() variables. Much cleaner for such usecases... 

    when RULE_INIT {
        set static::virtualLimit(http_test) "25"
        set static::virtualLimit(https_test) "50"
    }
    when HTTP_REQUEST {
        HTTP::respond 200 content "OK - Limit Variable = $static::virtualLimit([URI::basename [virtual]])"
    }

     Cheers, Kai