Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
caddy
Commits
506f1314
Commit
506f1314
authored
May 24, 2015
by
Zac Bergquist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix lint warnings for middleware/proxy
parent
e42c6bf0
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
27 additions
and
19 deletions
+27
-19
middleware/proxy/policy.go
middleware/proxy/policy.go
+9
-5
middleware/proxy/proxy.go
middleware/proxy/proxy.go
+10
-6
middleware/proxy/upstream.go
middleware/proxy/upstream.go
+8
-8
No files found.
middleware/proxy/policy.go
View file @
506f1314
...
...
@@ -5,6 +5,7 @@ import (
"sync/atomic"
)
// HostPool is a collection of UpstreamHosts.
type
HostPool
[]
*
UpstreamHost
// Policy decides how a host will be selected from a pool.
...
...
@@ -12,9 +13,10 @@ type Policy interface {
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
{}
// Select selects an up host at random from the specified pool.
func
(
r
*
Random
)
Select
(
pool
HostPool
)
*
UpstreamHost
{
// instead of just generating a random index
// this is done to prevent selecting a down host
...
...
@@ -37,11 +39,12 @@ func (r *Random) Select(pool HostPool) *UpstreamHost {
return
randHost
}
// The least_conn policy selects a host with the least connections.
// If multiple hosts have the least amount of connections, one is randomly
// chosen.
// LeastConn is a policy that selects the host with the least connections.
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
{
var
bestHost
*
UpstreamHost
count
:=
0
...
...
@@ -71,11 +74,12 @@ func (r *LeastConn) Select(pool HostPool) *UpstreamHost {
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
{
Robin
uint32
}
// Select selects an up host from the pool using a round robin ordering scheme.
func
(
r
*
RoundRobin
)
Select
(
pool
HostPool
)
*
UpstreamHost
{
poolLen
:=
uint32
(
len
(
pool
))
selection
:=
atomic
.
AddUint32
(
&
r
.
Robin
,
1
)
%
poolLen
...
...
middleware/proxy/proxy.go
View file @
506f1314
...
...
@@ -19,18 +19,19 @@ type Proxy struct {
Upstreams
[]
Upstream
}
//
An u
pstream manages a pool of proxy upstream hosts. Select should return a
//
U
pstream manages a pool of proxy upstream hosts. Select should return a
// suitable upstream host, or nil if no such hosts are available.
type
Upstream
interface
{
//The path this upstream host should be routed on
//
The path this upstream host should be routed on
From
()
string
// Selects an upstream host to be routed to.
Select
()
*
UpstreamHost
}
// UpstreamHostDownFunc can be used to customize how Down behaves.
type
UpstreamHostDownFunc
func
(
*
UpstreamHost
)
bool
//
An
UpstreamHost represents a single proxy upstream
// UpstreamHost represents a single proxy upstream
type
UpstreamHost
struct
{
// The hostname of this upstream host
Name
string
...
...
@@ -43,6 +44,9 @@ type UpstreamHost struct {
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
{
if
uh
.
CheckDown
==
nil
{
// Default settings
...
...
@@ -70,10 +74,10 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
proxy
:=
host
.
ReverseProxy
r
.
Host
=
host
.
Name
if
baseU
rl
,
err
:=
url
.
Parse
(
host
.
Name
);
err
==
nil
{
r
.
Host
=
baseU
rl
.
Host
if
baseU
RL
,
err
:=
url
.
Parse
(
host
.
Name
);
err
==
nil
{
r
.
Host
=
baseU
RL
.
Host
if
proxy
==
nil
{
proxy
=
NewSingleHostReverseProxy
(
baseU
rl
)
proxy
=
NewSingleHostReverseProxy
(
baseU
RL
)
}
}
else
if
proxy
==
nil
{
return
http
.
StatusInternalServerError
,
err
...
...
middleware/proxy/upstream.go
View file @
506f1314
package
proxy
import
(
"github.com/mholt/caddy/config/parse"
"io"
"io/ioutil"
"net/http"
...
...
@@ -9,6 +8,8 @@ import (
"strconv"
"strings"
"time"
"github.com/mholt/caddy/config/parse"
)
type
staticUpstream
struct
{
...
...
@@ -24,7 +25,7 @@ type staticUpstream struct {
}
}
//
n
ewStaticUpstreams parses the configuration input and sets up
//
N
ewStaticUpstreams parses the configuration input and sets up
// static upstreams for the proxy middleware.
func
NewStaticUpstreams
(
c
parse
.
Dispenser
)
([]
Upstream
,
error
)
{
var
upstreams
[]
Upstream
...
...
@@ -130,8 +131,8 @@ func NewStaticUpstreams(c parse.Dispenser) ([]Upstream, error) {
}
}(
upstream
),
}
if
baseU
rl
,
err
:=
url
.
Parse
(
uh
.
Name
);
err
==
nil
{
uh
.
ReverseProxy
=
NewSingleHostReverseProxy
(
baseU
rl
)
if
baseU
RL
,
err
:=
url
.
Parse
(
uh
.
Name
);
err
==
nil
{
uh
.
ReverseProxy
=
NewSingleHostReverseProxy
(
baseU
RL
)
}
else
{
return
upstreams
,
err
}
...
...
@@ -152,8 +153,8 @@ func (u *staticUpstream) From() string {
func
(
u
*
staticUpstream
)
healthCheck
()
{
for
_
,
host
:=
range
u
.
Hosts
{
hostU
rl
:=
host
.
Name
+
u
.
HealthCheck
.
Path
if
r
,
err
:=
http
.
Get
(
hostU
rl
);
err
==
nil
{
hostU
RL
:=
host
.
Name
+
u
.
HealthCheck
.
Path
if
r
,
err
:=
http
.
Get
(
hostU
RL
);
err
==
nil
{
io
.
Copy
(
ioutil
.
Discard
,
r
.
Body
)
r
.
Body
.
Close
()
host
.
Unhealthy
=
r
.
StatusCode
<
200
||
r
.
StatusCode
>=
400
...
...
@@ -199,7 +200,6 @@ func (u *staticUpstream) Select() *UpstreamHost {
if
u
.
Policy
==
nil
{
return
(
&
Random
{})
.
Select
(
pool
)
}
else
{
return
u
.
Policy
.
Select
(
pool
)
}
return
u
.
Policy
.
Select
(
pool
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment