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
Show 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 (
...
@@ -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 {
...
@@ -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 {
...
@@ -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 {
...
@@ -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
...
...
middleware/proxy/proxy.go
View file @
506f1314
...
@@ -19,18 +19,19 @@ type Proxy struct {
...
@@ -19,18 +19,19 @@ type Proxy struct {
Upstreams
[]
Upstream
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.
// 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 {
...
@@ -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) {
...
@@ -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
baseU
rl
,
err
:=
url
.
Parse
(
host
.
Name
);
err
==
nil
{
if
baseU
RL
,
err
:=
url
.
Parse
(
host
.
Name
);
err
==
nil
{
r
.
Host
=
baseU
rl
.
Host
r
.
Host
=
baseU
RL
.
Host
if
proxy
==
nil
{
if
proxy
==
nil
{
proxy
=
NewSingleHostReverseProxy
(
baseU
rl
)
proxy
=
NewSingleHostReverseProxy
(
baseU
RL
)
}
}
}
else
if
proxy
==
nil
{
}
else
if
proxy
==
nil
{
return
http
.
StatusInternalServerError
,
err
return
http
.
StatusInternalServerError
,
err
...
...
middleware/proxy/upstream.go
View file @
506f1314
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 (
...
@@ -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 {
...
@@ -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.
// 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) {
...
@@ -130,8 +131,8 @@ func NewStaticUpstreams(c parse.Dispenser) ([]Upstream, error) {
}
}
}(
upstream
),
}(
upstream
),
}
}
if
baseU
rl
,
err
:=
url
.
Parse
(
uh
.
Name
);
err
==
nil
{
if
baseU
RL
,
err
:=
url
.
Parse
(
uh
.
Name
);
err
==
nil
{
uh
.
ReverseProxy
=
NewSingleHostReverseProxy
(
baseU
rl
)
uh
.
ReverseProxy
=
NewSingleHostReverseProxy
(
baseU
RL
)
}
else
{
}
else
{
return
upstreams
,
err
return
upstreams
,
err
}
}
...
@@ -152,8 +153,8 @@ func (u *staticUpstream) From() string {
...
@@ -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
{
hostU
rl
:=
host
.
Name
+
u
.
HealthCheck
.
Path
hostU
RL
:=
host
.
Name
+
u
.
HealthCheck
.
Path
if
r
,
err
:=
http
.
Get
(
hostU
rl
);
err
==
nil
{
if
r
,
err
:=
http
.
Get
(
hostU
RL
);
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 {
...
@@ -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
)
}
}
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