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
9bdd9bde
Commit
9bdd9bde
authored
May 29, 2015
by
Matt Holt
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #94 from acmacalister/feature/custom-policies
added custom policy support
parents
130301e3
dd946f8a
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
42 additions
and
8 deletions
+42
-8
middleware/proxy/policy.go
middleware/proxy/policy.go
+6
-0
middleware/proxy/policy_test.go
middleware/proxy/policy_test.go
+15
-0
middleware/proxy/upstream.go
middleware/proxy/upstream.go
+11
-8
middleware/proxy/upstream_test.go
middleware/proxy/upstream_test.go
+10
-0
No files found.
middleware/proxy/policy.go
View file @
9bdd9bde
...
...
@@ -13,6 +13,12 @@ type Policy interface {
Select
(
pool
HostPool
)
*
UpstreamHost
}
func
init
()
{
RegisterPolicy
(
"random"
,
func
()
Policy
{
return
&
Random
{}
})
RegisterPolicy
(
"least_conn"
,
func
()
Policy
{
return
&
LeastConn
{}
})
RegisterPolicy
(
"round_robin"
,
func
()
Policy
{
return
&
RoundRobin
{}
})
}
// Random is a policy that selects up hosts from a pool at random.
type
Random
struct
{}
...
...
middleware/proxy/policy_test.go
View file @
9bdd9bde
...
...
@@ -4,6 +4,12 @@ import (
"testing"
)
type
customPolicy
struct
{}
func
(
r
*
customPolicy
)
Select
(
pool
HostPool
)
*
UpstreamHost
{
return
pool
[
0
]
}
func
testPool
()
HostPool
{
pool
:=
[]
*
UpstreamHost
{
&
UpstreamHost
{
...
...
@@ -55,3 +61,12 @@ func TestLeastConnPolicy(t *testing.T) {
t
.
Error
(
"Expected least connection host to be first or second host."
)
}
}
func
TestCustomPolicy
(
t
*
testing
.
T
)
{
pool
:=
testPool
()
customPolicy
:=
&
customPolicy
{}
h
:=
customPolicy
.
Select
(
pool
)
if
h
!=
pool
[
0
]
{
t
.
Error
(
"Expected custom policy host to be the first host."
)
}
}
middleware/proxy/upstream.go
View file @
9bdd9bde
...
...
@@ -12,6 +12,8 @@ import (
"github.com/mholt/caddy/config/parse"
)
var
supportedPolicies
map
[
string
]
func
()
Policy
=
make
(
map
[
string
]
func
()
Policy
)
type
staticUpstream
struct
{
from
string
Hosts
HostPool
...
...
@@ -53,14 +55,10 @@ func NewStaticUpstreams(c parse.Dispenser) ([]Upstream, error) {
if
!
c
.
NextArg
()
{
return
upstreams
,
c
.
ArgErr
()
}
switch
c
.
Val
()
{
case
"random"
:
upstream
.
Policy
=
&
Random
{}
case
"round_robin"
:
upstream
.
Policy
=
&
RoundRobin
{}
case
"least_conn"
:
upstream
.
Policy
=
&
LeastConn
{}
default
:
if
policyCreateFunc
,
ok
:=
supportedPolicies
[
c
.
Val
()];
ok
{
upstream
.
Policy
=
policyCreateFunc
()
}
else
{
return
upstreams
,
c
.
ArgErr
()
}
case
"fail_timeout"
:
...
...
@@ -147,6 +145,11 @@ func NewStaticUpstreams(c parse.Dispenser) ([]Upstream, error) {
return
upstreams
,
nil
}
// RegisterPolicy adds a custom policy to the proxy.
func
RegisterPolicy
(
name
string
,
policy
func
()
Policy
)
{
supportedPolicies
[
name
]
=
policy
}
func
(
u
*
staticUpstream
)
From
()
string
{
return
u
.
from
}
...
...
middleware/proxy/upstream_test.go
View file @
9bdd9bde
...
...
@@ -41,3 +41,13 @@ func TestSelect(t *testing.T) {
t
.
Error
(
"Expected select to not return nil"
)
}
}
func
TestRegisterPolicy
(
t
*
testing
.
T
)
{
name
:=
"custom"
customPolicy
:=
&
customPolicy
{}
RegisterPolicy
(
name
,
func
()
Policy
{
return
customPolicy
})
if
_
,
ok
:=
supportedPolicies
[
name
];
!
ok
{
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