Various cleanups and fixes (#88)

Add port number to health check. Add tests the rewrite

middleware.



Fixes #36
This commit is contained in:
Miek Gieben
2016-04-07 17:42:35 +01:00
parent 09207867e4
commit efcb5cddbc
5 changed files with 155 additions and 145 deletions

View File

@@ -22,9 +22,9 @@ proxy from to... {
policy random | least_conn | round_robin
fail_timeout duration
max_fails integer
health_check path [duration]
health_check path:port [duration]
except ignored_names...
preset
tcp
}
~~~
@@ -33,8 +33,10 @@ proxy from to... {
* `policy` is the load balancing policy to use; applies only with multiple backends. May be one of random, least_conn, or round_robin. Default is random.
* `fail_timeout` specifies how long to consider a backend as down after it has failed. While it is down, requests will not be routed to that backend. A backend is "down" if Caddy fails to communicate with it. The default value is 10 seconds ("10s").
* `max_fails` is the number of failures within fail_timeout that are needed before considering a backend to be down. If 0, the backend will never be marked as down. Default is 1.
* `health_check` will check path on each backend. If a backend returns a status code of 200-399, then that backend is healthy. If it doesn't, the backend is marked as unhealthy for duration and no requests are routed to it. If this option is not provided then health checks are disabled. The default duration is 10 seconds ("10s").
* `health_check` will check path (on port) on each backend. If a backend returns a status code of 200-399, then that backend is healthy. If it doesn't, the backend is marked as unhealthy for duration and no requests are routed to it. If this option is not provided then health checks are disabled. The default duration is 10 seconds ("10s").
* `ignored_names...` is a space-separated list of paths to exclude from proxying. Requests that match any of these paths will be passed thru.
* `tcp` use TCP for all upstream queries, otherwise it depends on the transport of the incoming
query. TODO(miek): implement.
## Policies
@@ -68,9 +70,9 @@ proxy . web1.local:53 web2.local:1053 web3.local {
With health checks and proxy headers to pass hostname, IP, and scheme upstream:
~~~
proxy / web1.local:80 web2.local:90 web3.local:100 {
proxy . web1.local:53 web2.local:53 web3.local:53 {
policy round_robin
health_check /health
health_check /health:8080
}
~~~

View File

@@ -3,6 +3,7 @@ package proxy
import (
"io"
"io/ioutil"
"net"
"net/http"
"strconv"
"strings"
@@ -28,6 +29,7 @@ type staticUpstream struct {
MaxFails int32
HealthCheck struct {
Path string
Port string
Interval time.Duration
}
WithoutPathPrefix string
@@ -138,7 +140,11 @@ func parseBlock(c *parse.Dispenser, u *staticUpstream) error {
if !c.NextArg() {
return c.ArgErr()
}
u.HealthCheck.Path = c.Val()
var err error
u.HealthCheck.Path, u.HealthCheck.Port, err = net.SplitHostPort(c.Val())
if err != nil {
return err
}
u.HealthCheck.Interval = 30 * time.Second
if c.NextArg() {
dur, err := time.ParseDuration(c.Val())
@@ -175,7 +181,11 @@ func parseBlock(c *parse.Dispenser, u *staticUpstream) error {
func (u *staticUpstream) healthCheck() {
for _, host := range u.Hosts {
hostURL := host.Name + u.HealthCheck.Path
port := ""
if u.HealthCheck.Port != "" {
port = ":" + u.HealthCheck.Port
}
hostURL := host.Name + port + u.HealthCheck.Path
if r, err := http.Get(hostURL); err == nil {
io.Copy(ioutil.Discard, r.Body)
r.Body.Close()