Hi Vivek,

On Thu, Mar 05, 2015 at 01:31:53AM -0600, Vivek Malik wrote:
> Hi Willy,
> 
> I am using haproxy/rand to simulate A/B/C... testing between multiple
> environments. Each backend emits a long expiry cookie to put the
> session into their experiment. If a request comes with a cookie of the
> experiment, the request goes to that backend. If a session comes
> without a cookie, rand is used to decide which backend would be used
> for the request.

OK.

> My earlier code looked similar to
> 
> acl testa req.cookie(abtest) eq a
> acl testb req.cookie(abtest) eq b
> acl testa_rand rand(100) lt 80
> acl testb_rand rand(20) lt 20
> http-request set-header expirment=a if testa
> http-request set-header expirment=b if testb
> 
> http-request set-header expirment=a if !testa !testb testa_rand
> http-request set-header expirment=a if !testa !testb testb_rand
> 
> use_backend bk_a if testa
> use_backend bk_b if testb
> use_backend bk_a if testa_rand
> use_backend bk_b if testb_rand

One possibility is also to use default_backend as a fallback. But
it would indeed not fix the multiple/missing header addition.

> However, this config was failing as request would often to backend
> with experiment header not set properly. Once I understood that acl
> was evaluating rand every time, I was able to write the configuration
> something like
> 
> http-request del-header experiment
> http-request set-header experiment a if { req.cook(abtest) eq a }
> http-request set-header experiment b if { req.cook(abtest) eq b } &&
> !{ req.hdr(experiment) -m found }
> http-request set-header experiment a if { rand(100) lt 80 } && !{
> req.hdr(experiment) -m found }
> http-request set-header experiment b if { rand(20) lt 20 } && !{
> req.hdr(experiment) -m found }
> 
> use_backend bk_a if { req.hdr(experiment) eq a }
> use_backend bk_b if { req.hdr(experiment) eq b }
> 
> Using the request header as a temporary variable, I was able to keep
> state and avoid calling rand acl more than once.

Yes that definitely is the way to go.

Cheers,
Willy


Reply via email to