Commit a148b923 authored by Peer Beckmann's avatar Peer Beckmann Committed by Matt Holt

proxy: Add the first policy (#1513)

* Add the first policy which sends the request to the first available host

* Make the error message clear. As we expect the second not first upstream
host.
parent 36a62f09
......@@ -22,6 +22,7 @@ func init() {
RegisterPolicy("least_conn", func() Policy { return &LeastConn{} })
RegisterPolicy("round_robin", func() Policy { return &RoundRobin{} })
RegisterPolicy("ip_hash", func() Policy { return &IPHash{} })
RegisterPolicy("first", func() Policy { return &First{} })
}
// Random is a policy that selects up hosts from a pool at random.
......@@ -131,3 +132,16 @@ func (r *IPHash) Select(pool HostPool, request *http.Request) *UpstreamHost {
}
return nil
}
// First is a policy that selects the fist available host
type First struct{}
// Select selects the first host from the pool, that is available
func (r *First) Select(pool HostPool, request *http.Request) *UpstreamHost {
for _, host := range pool {
if host.Available() {
return host
}
}
return nil
}
......@@ -226,3 +226,20 @@ func TestIPHashPolicy(t *testing.T) {
t.Error("Expected ip hash policy host to be nil.")
}
}
func TestFirstPolicy(t *testing.T) {
pool := testPool()
firstPolicy := &First{}
req := httptest.NewRequest(http.MethodGet, "/", nil)
h := firstPolicy.Select(pool, req)
if h != pool[0] {
t.Error("Expected first policy host to be the first host.")
}
pool[0].Unhealthy = 1
h = firstPolicy.Select(pool, req)
if h != pool[1] {
t.Error("Expected first policy host to be the second host.")
}
}
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