Manual cookie persistence

Problem this snippet solves:

I had an app admin ask me for a way to temporarily force node assignment for his session, for testing purposes, in an app where we already use cookie persistence. We wanted a particular URL request to force the node assignment, then have all subsequent requests (regardless of URL) use that same pool member. I would have thought "pool mypool member 10.2.3.4 80" and "persist cookie insert" would do the trick, but no. We were able to assign the first request (with special URL) to the right app server, but unable to update the "BIGipServermy_pool" cookie with the new node assignment, so subsequent requests were sent to the wrong node. As of version 9.3 LTM does not honor cookie persistence settings during forced node assignment.

Thus, I give you manual cookie persistence...

Code :

when RULE_INIT {
set cookie_name "my_node_forced"
}
when HTTP_REQUEST {
# Get node id from URL ending in ?node=server1, ?node=server2, etc.
set node_forced [findstr [HTTP::uri] "?node=" 6]

# Or from previous cookie
set has_cookie [HTTP::cookie exists $::cookie_name]

if { $node_forced eq "" and $has_cookie } {
set node_forced [HTTP::cookie value $::cookie_name]
}

# Map node id to IP and port found in a class
# Entries should be strings of the form
#   

if { $node_forced ne "" } {
set node_data [findclass $node_forced $::my_nodes " "]
if { $node_data ne "" } {
set node_ip [getfield $node_data " " 1]
set node_port [getfield $node_data " " 2]
pool my_pool member $node_ip $node_port
} else {
set node_forced ""
}
}
}
when HTTP_RESPONSE {
if { $node_forced ne "" } {
# Add a cookie to continue forcing node assignment
HTTP::cookie insert name $::cookie_name value $node_forced path "/"
}
elseif { $has_cookie } {
# Delete cookie and resume load balancing (with built-in persistence)
HTTP::cookie insert name $::cookie_name value "" path "/"
HTTP::cookie expires $::cookie_name 0 absolute
}
}
Published Mar 18, 2015
Version 1.0
No CommentsBe the first to comment