URI HASH persistence based on URI depth and string length

Problem this snippet solves:

When dealing with caching or AV analysis systems, we may need to define a static persistence method to maximize hits and performances on backend servers.

How to use this snippet:

Installation

You just need to assign the irule to a standard Virtual Server. The VS require at least an http profile and ssl profiles (if doing ssl offloafing or ssl bridging)

Variables

  • static::lb_depth - define a subset of the path to use for HASH calculation
  • static::lb_len - define the number of chars to retrieve in the path for HASH calculation
  • static::timeout - define the timeout of the persistence record

Features

  • Universal persistence
  • Persistence using URI Hash
  • Specify a depth and/or a string length to get a subset of the URI for the Hash calculation

Code :

when RULE_INIT {
    # define the depth of the path that will be used for HASH calculation
    set static::lb_depth 5
    
    # lb_len variable should be set to 0 if you don't want to use it
    set static::lb_len 0
    
    set static::timeout 3600
}

when HTTP_REQUEST {

    # initialize required variables
    set path [string tolower [HTTP::path]]
    set depth [URI::path [HTTP::uri] depth]

    # define the depth of the path for hash calculation
    if { $depth < $static::lb_depth } {
        set depth_path [HTTP::path]
    } else {
        set depth_path [URI::path [HTTP::uri] 1 $static::lb_depth]
    }

    set len [string length $depth_path]

    # define the chars length to be processed for hash calculation
    if { $static::lb_len > 0 and $static::lb_len < $len } {
        binary scan [sha1 [string range $depth_path 0 $static::lb_len-1]] w1 key
    } else {
        binary scan [sha1 $depth_path] w1 key
    }

    persist uie $key $static::timeout
}

Tested this on version:

11.3
Published Jul 27, 2016
Version 1.0

Was this article helpful?

No CommentsBe the first to comment