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
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 {
...
@@ -13,6 +13,12 @@ type Policy interface {
Select
(
pool
HostPool
)
*
UpstreamHost
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.
// Random is a policy that selects up hosts from a pool at random.
type
Random
struct
{}
type
Random
struct
{}
...
...
middleware/proxy/policy_test.go
View file @
9bdd9bde
...
@@ -4,6 +4,12 @@ import (
...
@@ -4,6 +4,12 @@ import (
"testing"
"testing"
)
)
type
customPolicy
struct
{}
func
(
r
*
customPolicy
)
Select
(
pool
HostPool
)
*
UpstreamHost
{
return
pool
[
0
]
}
func
testPool
()
HostPool
{
func
testPool
()
HostPool
{
pool
:=
[]
*
UpstreamHost
{
pool
:=
[]
*
UpstreamHost
{
&
UpstreamHost
{
&
UpstreamHost
{
...
@@ -55,3 +61,12 @@ func TestLeastConnPolicy(t *testing.T) {
...
@@ -55,3 +61,12 @@ func TestLeastConnPolicy(t *testing.T) {
t
.
Error
(
"Expected least connection host to be first or second host."
)
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 (
...
@@ -12,6 +12,8 @@ import (
"github.com/mholt/caddy/config/parse"
"github.com/mholt/caddy/config/parse"
)
)
var
supportedPolicies
map
[
string
]
func
()
Policy
=
make
(
map
[
string
]
func
()
Policy
)
type
staticUpstream
struct
{
type
staticUpstream
struct
{
from
string
from
string
Hosts
HostPool
Hosts
HostPool
...
@@ -53,14 +55,10 @@ func NewStaticUpstreams(c parse.Dispenser) ([]Upstream, error) {
...
@@ -53,14 +55,10 @@ func NewStaticUpstreams(c parse.Dispenser) ([]Upstream, error) {
if
!
c
.
NextArg
()
{
if
!
c
.
NextArg
()
{
return
upstreams
,
c
.
ArgErr
()
return
upstreams
,
c
.
ArgErr
()
}
}
switch
c
.
Val
()
{
case
"random"
:
if
policyCreateFunc
,
ok
:=
supportedPolicies
[
c
.
Val
()];
ok
{
upstream
.
Policy
=
&
Random
{}
upstream
.
Policy
=
policyCreateFunc
()
case
"round_robin"
:
}
else
{
upstream
.
Policy
=
&
RoundRobin
{}
case
"least_conn"
:
upstream
.
Policy
=
&
LeastConn
{}
default
:
return
upstreams
,
c
.
ArgErr
()
return
upstreams
,
c
.
ArgErr
()
}
}
case
"fail_timeout"
:
case
"fail_timeout"
:
...
@@ -147,6 +145,11 @@ func NewStaticUpstreams(c parse.Dispenser) ([]Upstream, error) {
...
@@ -147,6 +145,11 @@ func NewStaticUpstreams(c parse.Dispenser) ([]Upstream, error) {
return
upstreams
,
nil
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
{
func
(
u
*
staticUpstream
)
From
()
string
{
return
u
.
from
return
u
.
from
}
}
...
...
middleware/proxy/upstream_test.go
View file @
9bdd9bde
...
@@ -41,3 +41,13 @@ func TestSelect(t *testing.T) {
...
@@ -41,3 +41,13 @@ func TestSelect(t *testing.T) {
t
.
Error
(
"Expected select to not return nil"
)
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