mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-23 22:27:38 -05:00
Fix lint warnings for middleware/proxy
This commit is contained in:
parent
e42c6bf0bb
commit
506f131428
3 changed files with 27 additions and 19 deletions
|
@ -5,6 +5,7 @@ import (
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// HostPool is a collection of UpstreamHosts.
|
||||||
type HostPool []*UpstreamHost
|
type HostPool []*UpstreamHost
|
||||||
|
|
||||||
// Policy decides how a host will be selected from a pool.
|
// Policy decides how a host will be selected from a pool.
|
||||||
|
@ -12,9 +13,10 @@ type Policy interface {
|
||||||
Select(pool HostPool) *UpstreamHost
|
Select(pool HostPool) *UpstreamHost
|
||||||
}
|
}
|
||||||
|
|
||||||
// The random policy randomly selected an up host from the pool.
|
// Random is a policy that selects up hosts from a pool at random.
|
||||||
type Random struct{}
|
type Random struct{}
|
||||||
|
|
||||||
|
// Select selects an up host at random from the specified pool.
|
||||||
func (r *Random) Select(pool HostPool) *UpstreamHost {
|
func (r *Random) Select(pool HostPool) *UpstreamHost {
|
||||||
// instead of just generating a random index
|
// instead of just generating a random index
|
||||||
// this is done to prevent selecting a down host
|
// this is done to prevent selecting a down host
|
||||||
|
@ -37,11 +39,12 @@ func (r *Random) Select(pool HostPool) *UpstreamHost {
|
||||||
return randHost
|
return randHost
|
||||||
}
|
}
|
||||||
|
|
||||||
// The least_conn policy selects a host with the least connections.
|
// LeastConn is a policy that selects the host with the least connections.
|
||||||
// If multiple hosts have the least amount of connections, one is randomly
|
|
||||||
// chosen.
|
|
||||||
type LeastConn struct{}
|
type LeastConn struct{}
|
||||||
|
|
||||||
|
// Select selects the up host with the least number of connections in the
|
||||||
|
// pool. If more than one host has the same least number of connections,
|
||||||
|
// one of the hosts is chosen at random.
|
||||||
func (r *LeastConn) Select(pool HostPool) *UpstreamHost {
|
func (r *LeastConn) Select(pool HostPool) *UpstreamHost {
|
||||||
var bestHost *UpstreamHost
|
var bestHost *UpstreamHost
|
||||||
count := 0
|
count := 0
|
||||||
|
@ -71,11 +74,12 @@ func (r *LeastConn) Select(pool HostPool) *UpstreamHost {
|
||||||
return bestHost
|
return bestHost
|
||||||
}
|
}
|
||||||
|
|
||||||
// The round_robin policy selects a host based on round robin ordering.
|
// RoundRobin is a policy that selects hosts based on round robin ordering.
|
||||||
type RoundRobin struct {
|
type RoundRobin struct {
|
||||||
Robin uint32
|
Robin uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Select selects an up host from the pool using a round robin ordering scheme.
|
||||||
func (r *RoundRobin) Select(pool HostPool) *UpstreamHost {
|
func (r *RoundRobin) Select(pool HostPool) *UpstreamHost {
|
||||||
poolLen := uint32(len(pool))
|
poolLen := uint32(len(pool))
|
||||||
selection := atomic.AddUint32(&r.Robin, 1) % poolLen
|
selection := atomic.AddUint32(&r.Robin, 1) % poolLen
|
||||||
|
|
|
@ -19,18 +19,19 @@ type Proxy struct {
|
||||||
Upstreams []Upstream
|
Upstreams []Upstream
|
||||||
}
|
}
|
||||||
|
|
||||||
// An upstream manages a pool of proxy upstream hosts. Select should return a
|
// Upstream manages a pool of proxy upstream hosts. Select should return a
|
||||||
// suitable upstream host, or nil if no such hosts are available.
|
// suitable upstream host, or nil if no such hosts are available.
|
||||||
type Upstream interface {
|
type Upstream interface {
|
||||||
//The path this upstream host should be routed on
|
// The path this upstream host should be routed on
|
||||||
From() string
|
From() string
|
||||||
// Selects an upstream host to be routed to.
|
// Selects an upstream host to be routed to.
|
||||||
Select() *UpstreamHost
|
Select() *UpstreamHost
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpstreamHostDownFunc can be used to customize how Down behaves.
|
||||||
type UpstreamHostDownFunc func(*UpstreamHost) bool
|
type UpstreamHostDownFunc func(*UpstreamHost) bool
|
||||||
|
|
||||||
// An UpstreamHost represents a single proxy upstream
|
// UpstreamHost represents a single proxy upstream
|
||||||
type UpstreamHost struct {
|
type UpstreamHost struct {
|
||||||
// The hostname of this upstream host
|
// The hostname of this upstream host
|
||||||
Name string
|
Name string
|
||||||
|
@ -43,6 +44,9 @@ type UpstreamHost struct {
|
||||||
CheckDown UpstreamHostDownFunc
|
CheckDown UpstreamHostDownFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Down checks whether the upstream host is down or not.
|
||||||
|
// Down will try to use uh.CheckDown first, and will fall
|
||||||
|
// back to some default criteria if necessary.
|
||||||
func (uh *UpstreamHost) Down() bool {
|
func (uh *UpstreamHost) Down() bool {
|
||||||
if uh.CheckDown == nil {
|
if uh.CheckDown == nil {
|
||||||
// Default settings
|
// Default settings
|
||||||
|
@ -70,10 +74,10 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
proxy := host.ReverseProxy
|
proxy := host.ReverseProxy
|
||||||
r.Host = host.Name
|
r.Host = host.Name
|
||||||
|
|
||||||
if baseUrl, err := url.Parse(host.Name); err == nil {
|
if baseURL, err := url.Parse(host.Name); err == nil {
|
||||||
r.Host = baseUrl.Host
|
r.Host = baseURL.Host
|
||||||
if proxy == nil {
|
if proxy == nil {
|
||||||
proxy = NewSingleHostReverseProxy(baseUrl)
|
proxy = NewSingleHostReverseProxy(baseURL)
|
||||||
}
|
}
|
||||||
} else if proxy == nil {
|
} else if proxy == nil {
|
||||||
return http.StatusInternalServerError, err
|
return http.StatusInternalServerError, err
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package proxy
|
package proxy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/mholt/caddy/config/parse"
|
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -9,6 +8,8 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/mholt/caddy/config/parse"
|
||||||
)
|
)
|
||||||
|
|
||||||
type staticUpstream struct {
|
type staticUpstream struct {
|
||||||
|
@ -24,7 +25,7 @@ type staticUpstream struct {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// newStaticUpstreams parses the configuration input and sets up
|
// NewStaticUpstreams parses the configuration input and sets up
|
||||||
// static upstreams for the proxy middleware.
|
// static upstreams for the proxy middleware.
|
||||||
func NewStaticUpstreams(c parse.Dispenser) ([]Upstream, error) {
|
func NewStaticUpstreams(c parse.Dispenser) ([]Upstream, error) {
|
||||||
var upstreams []Upstream
|
var upstreams []Upstream
|
||||||
|
@ -130,8 +131,8 @@ func NewStaticUpstreams(c parse.Dispenser) ([]Upstream, error) {
|
||||||
}
|
}
|
||||||
}(upstream),
|
}(upstream),
|
||||||
}
|
}
|
||||||
if baseUrl, err := url.Parse(uh.Name); err == nil {
|
if baseURL, err := url.Parse(uh.Name); err == nil {
|
||||||
uh.ReverseProxy = NewSingleHostReverseProxy(baseUrl)
|
uh.ReverseProxy = NewSingleHostReverseProxy(baseURL)
|
||||||
} else {
|
} else {
|
||||||
return upstreams, err
|
return upstreams, err
|
||||||
}
|
}
|
||||||
|
@ -152,8 +153,8 @@ func (u *staticUpstream) From() string {
|
||||||
|
|
||||||
func (u *staticUpstream) healthCheck() {
|
func (u *staticUpstream) healthCheck() {
|
||||||
for _, host := range u.Hosts {
|
for _, host := range u.Hosts {
|
||||||
hostUrl := host.Name + u.HealthCheck.Path
|
hostURL := host.Name + u.HealthCheck.Path
|
||||||
if r, err := http.Get(hostUrl); err == nil {
|
if r, err := http.Get(hostURL); err == nil {
|
||||||
io.Copy(ioutil.Discard, r.Body)
|
io.Copy(ioutil.Discard, r.Body)
|
||||||
r.Body.Close()
|
r.Body.Close()
|
||||||
host.Unhealthy = r.StatusCode < 200 || r.StatusCode >= 400
|
host.Unhealthy = r.StatusCode < 200 || r.StatusCode >= 400
|
||||||
|
@ -199,7 +200,6 @@ func (u *staticUpstream) Select() *UpstreamHost {
|
||||||
|
|
||||||
if u.Policy == nil {
|
if u.Policy == nil {
|
||||||
return (&Random{}).Select(pool)
|
return (&Random{}).Select(pool)
|
||||||
} else {
|
|
||||||
return u.Policy.Select(pool)
|
|
||||||
}
|
}
|
||||||
|
return u.Policy.Select(pool)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue