Commit d3b731e9 authored by Alexander Danilov's avatar Alexander Danilov Committed by Matt Holt

proxy: Fix 502 errors for requests without headers (#2188)

* Fix 502 errors for requests without headers

* Add unexported roundRobinPolicier

We have to preserve state for fallback mode of Header policy, so
it's required to save state in some variable
parent 3e0695ee
...@@ -183,6 +183,8 @@ type Header struct { ...@@ -183,6 +183,8 @@ type Header struct {
Name string Name string
} }
var roundRobinPolicier RoundRobin
// Select selects the host based on hashing the header value // Select selects the host based on hashing the header value
func (r *Header) Select(pool HostPool, request *http.Request) *UpstreamHost { func (r *Header) Select(pool HostPool, request *http.Request) *UpstreamHost {
if r.Name == "" { if r.Name == "" {
...@@ -190,7 +192,8 @@ func (r *Header) Select(pool HostPool, request *http.Request) *UpstreamHost { ...@@ -190,7 +192,8 @@ func (r *Header) Select(pool HostPool, request *http.Request) *UpstreamHost {
} }
val := request.Header.Get(r.Name) val := request.Header.Get(r.Name)
if val == "" { if val == "" {
return nil // fallback to RoundRobin policy in case no Header in request
return roundRobinPolicier.Select(pool, request)
} }
return hostByHashing(pool, val) return hostByHashing(pool, val)
} }
...@@ -320,21 +320,25 @@ func TestUriPolicy(t *testing.T) { ...@@ -320,21 +320,25 @@ func TestUriPolicy(t *testing.T) {
func TestHeaderPolicy(t *testing.T) { func TestHeaderPolicy(t *testing.T) {
pool := testPool() pool := testPool()
tests := []struct { tests := []struct {
Name string
Policy *Header Policy *Header
RequestHeaderName string RequestHeaderName string
RequestHeaderValue string RequestHeaderValue string
NilHost bool NilHost bool
HostIndex int HostIndex int
}{ }{
{&Header{""}, "", "", true, 0}, {"empty config", &Header{""}, "", "", true, 0},
{&Header{""}, "Affinity", "somevalue", true, 0}, {"empty config+header+value", &Header{""}, "Affinity", "somevalue", true, 0},
{&Header{""}, "Affinity", "", true, 0}, {"empty config+header", &Header{""}, "Affinity", "", true, 0},
{&Header{"Affinity"}, "", "", true, 0}, {"no header(fallback to roundrobin)", &Header{"Affinity"}, "", "", false, 1},
{&Header{"Affinity"}, "Affinity", "somevalue", false, 1}, {"no header(fallback to roundrobin)", &Header{"Affinity"}, "", "", false, 2},
{&Header{"Affinity"}, "Affinity", "somevalue2", false, 0}, {"no header(fallback to roundrobin)", &Header{"Affinity"}, "", "", false, 0},
{&Header{"Affinity"}, "Affinity", "somevalue3", false, 2},
{&Header{"Affinity"}, "Affinity", "", true, 0}, {"hash route to host", &Header{"Affinity"}, "Affinity", "somevalue", false, 1},
{"hash route to host", &Header{"Affinity"}, "Affinity", "somevalue2", false, 0},
{"hash route to host", &Header{"Affinity"}, "Affinity", "somevalue3", false, 2},
{"hash route with empty value", &Header{"Affinity"}, "Affinity", "", false, 1},
} }
for idx, test := range tests { for idx, test := range tests {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment