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
Łukasz Nowak
caddy
Commits
593aec9a
Commit
593aec9a
authored
May 28, 2015
by
Austin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
changes per comment
parent
6b173b51
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
15 additions
and
32 deletions
+15
-32
middleware/proxy/policy.go
middleware/proxy/policy.go
+0
-16
middleware/proxy/policy_test.go
middleware/proxy/policy_test.go
+0
-4
middleware/proxy/upstream.go
middleware/proxy/upstream.go
+12
-10
middleware/proxy/upstream_test.go
middleware/proxy/upstream_test.go
+3
-2
No files found.
middleware/proxy/policy.go
View file @
593aec9a
...
@@ -11,7 +11,6 @@ type HostPool []*UpstreamHost
...
@@ -11,7 +11,6 @@ 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.
type
Policy
interface
{
type
Policy
interface
{
Select
(
pool
HostPool
)
*
UpstreamHost
Select
(
pool
HostPool
)
*
UpstreamHost
Name
()
string
}
}
// Random is a policy that selects up hosts from a pool at random.
// Random is a policy that selects up hosts from a pool at random.
...
@@ -40,11 +39,6 @@ func (r *Random) Select(pool HostPool) *UpstreamHost {
...
@@ -40,11 +39,6 @@ func (r *Random) Select(pool HostPool) *UpstreamHost {
return
randHost
return
randHost
}
}
// Name returns the name of the policy.
func
(
r
*
Random
)
Name
()
string
{
return
"random"
}
// LeastConn is a policy that selects the host with the least connections.
// LeastConn is a policy that selects the host with the least connections.
type
LeastConn
struct
{}
type
LeastConn
struct
{}
...
@@ -80,11 +74,6 @@ func (r *LeastConn) Select(pool HostPool) *UpstreamHost {
...
@@ -80,11 +74,6 @@ func (r *LeastConn) Select(pool HostPool) *UpstreamHost {
return
bestHost
return
bestHost
}
}
// Name returns the name of the policy.
func
(
r
*
LeastConn
)
Name
()
string
{
return
"least_conn"
}
// RoundRobin is a policy that selects hosts 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
...
@@ -104,8 +93,3 @@ func (r *RoundRobin) Select(pool HostPool) *UpstreamHost {
...
@@ -104,8 +93,3 @@ func (r *RoundRobin) Select(pool HostPool) *UpstreamHost {
}
}
return
host
return
host
}
}
// Name returns the name of the policy.
func
(
r
*
RoundRobin
)
Name
()
string
{
return
"round_robin"
}
middleware/proxy/policy_test.go
View file @
593aec9a
...
@@ -10,10 +10,6 @@ func (r *customPolicy) Select(pool HostPool) *UpstreamHost {
...
@@ -10,10 +10,6 @@ func (r *customPolicy) Select(pool HostPool) *UpstreamHost {
return
pool
[
0
]
return
pool
[
0
]
}
}
func
(
r
*
customPolicy
)
Name
()
string
{
return
"custom"
}
func
testPool
()
HostPool
{
func
testPool
()
HostPool
{
pool
:=
[]
*
UpstreamHost
{
pool
:=
[]
*
UpstreamHost
{
&
UpstreamHost
{
&
UpstreamHost
{
...
...
middleware/proxy/upstream.go
View file @
593aec9a
...
@@ -12,7 +12,7 @@ import (
...
@@ -12,7 +12,7 @@ import (
"github.com/mholt/caddy/config/parse"
"github.com/mholt/caddy/config/parse"
)
)
var
supportedPolicies
map
[
string
]
Policy
=
make
(
map
[
string
]
Policy
)
var
supportedPolicies
map
[
string
]
func
()
Policy
=
make
(
map
[
string
]
func
()
Policy
)
type
staticUpstream
struct
{
type
staticUpstream
struct
{
from
string
from
string
...
@@ -27,15 +27,17 @@ type staticUpstream struct {
...
@@ -27,15 +27,17 @@ type staticUpstream struct {
}
}
}
}
func
init
()
{
RegisterPolicy
(
"random"
,
func
()
Policy
{
return
&
Random
{}
})
RegisterPolicy
(
"least_conn"
,
func
()
Policy
{
return
&
LeastConn
{}
})
RegisterPolicy
(
"round_robin"
,
func
()
Policy
{
return
&
RoundRobin
{}
})
}
// 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
RegisterPolicy
(
&
Random
{})
RegisterPolicy
(
&
LeastConn
{})
RegisterPolicy
(
&
RoundRobin
{})
for
c
.
Next
()
{
for
c
.
Next
()
{
upstream
:=
&
staticUpstream
{
upstream
:=
&
staticUpstream
{
from
:
""
,
from
:
""
,
...
@@ -60,11 +62,11 @@ func NewStaticUpstreams(c parse.Dispenser) ([]Upstream, error) {
...
@@ -60,11 +62,11 @@ func NewStaticUpstreams(c parse.Dispenser) ([]Upstream, error) {
return
upstreams
,
c
.
ArgErr
()
return
upstreams
,
c
.
ArgErr
()
}
}
policy
,
ok
:=
supportedPolicies
[
c
.
Val
()]
if
policyCreateFunc
,
ok
:=
supportedPolicies
[
c
.
Val
()];
ok
{
if
!
ok
{
upstream
.
Policy
=
policyCreateFunc
()
}
else
{
return
upstreams
,
c
.
ArgErr
()
return
upstreams
,
c
.
ArgErr
()
}
}
upstream
.
Policy
=
policy
case
"fail_timeout"
:
case
"fail_timeout"
:
if
!
c
.
NextArg
()
{
if
!
c
.
NextArg
()
{
return
upstreams
,
c
.
ArgErr
()
return
upstreams
,
c
.
ArgErr
()
...
@@ -150,8 +152,8 @@ func NewStaticUpstreams(c parse.Dispenser) ([]Upstream, error) {
...
@@ -150,8 +152,8 @@ func NewStaticUpstreams(c parse.Dispenser) ([]Upstream, error) {
}
}
// RegisterPolicy adds a custom policy to the proxy.
// RegisterPolicy adds a custom policy to the proxy.
func
RegisterPolicy
(
policy
Policy
)
{
func
RegisterPolicy
(
name
string
,
policy
func
()
Policy
)
{
supportedPolicies
[
policy
.
Name
()
]
=
policy
supportedPolicies
[
name
]
=
policy
}
}
func
(
u
*
staticUpstream
)
From
()
string
{
func
(
u
*
staticUpstream
)
From
()
string
{
...
...
middleware/proxy/upstream_test.go
View file @
593aec9a
...
@@ -43,9 +43,10 @@ func TestSelect(t *testing.T) {
...
@@ -43,9 +43,10 @@ func TestSelect(t *testing.T) {
}
}
func
TestRegisterPolicy
(
t
*
testing
.
T
)
{
func
TestRegisterPolicy
(
t
*
testing
.
T
)
{
name
:=
"custom"
customPolicy
:=
&
customPolicy
{}
customPolicy
:=
&
customPolicy
{}
RegisterPolicy
(
customPolicy
)
RegisterPolicy
(
name
,
func
()
Policy
{
return
customPolicy
}
)
if
_
,
ok
:=
supportedPolicies
[
customPolicy
.
Name
()
];
!
ok
{
if
_
,
ok
:=
supportedPolicies
[
name
];
!
ok
{
t
.
Error
(
"Expected supportedPolicies to have a custom policy."
)
t
.
Error
(
"Expected supportedPolicies to have a custom policy."
)
}
}
...
...
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