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
80761796
Commit
80761796
authored
Jun 20, 2016
by
Matt Holt
Committed by
GitHub
Jun 20, 2016
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #891 from andrewhamon/policy-cleanup
Refactor and clean up policy code
parents
6fe5c1a6
a5046297
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
22 deletions
+21
-22
caddyhttp/proxy/policy.go
caddyhttp/proxy/policy.go
+21
-22
No files found.
caddyhttp/proxy/policy.go
View file @
80761796
package
proxy
import
(
"math"
"math/rand"
"sync"
)
...
...
@@ -24,22 +25,23 @@ 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 unavailable host
// Because the number of available hosts isn't known
// up front, the host is selected via reservoir sampling
// https://en.wikipedia.org/wiki/Reservoir_sampling
var
randHost
*
UpstreamHost
count
:=
0
for
_
,
host
:=
range
pool
{
if
!
host
.
Available
()
{
continue
}
// (n % 1 == 0) holds for all n, therefore randHost
// will always get assigned a value if there is
// at least 1 available host
count
++
if
count
==
1
{
if
(
rand
.
Int
()
%
count
)
==
0
{
randHost
=
host
}
else
{
r
:=
rand
.
Int
()
%
count
if
r
==
(
count
-
1
)
{
randHost
=
host
}
}
}
return
randHost
...
...
@@ -54,26 +56,23 @@ type LeastConn struct{}
func
(
r
*
LeastConn
)
Select
(
pool
HostPool
)
*
UpstreamHost
{
var
bestHost
*
UpstreamHost
count
:=
0
leastConn
:=
int64
(
1
<<
63
-
1
)
leastConn
:=
int64
(
math
.
MaxInt64
)
for
_
,
host
:=
range
pool
{
if
!
host
.
Available
()
{
continue
}
hostConns
:=
host
.
Conns
if
hostConns
<
leastConn
{
bestHost
=
host
leastConn
=
hostConns
count
=
1
}
else
if
hostConns
==
leastConn
{
// randomly select host among hosts with least connections
if
host
.
Conns
<
leastConn
{
leastConn
=
host
.
Conns
count
=
0
}
// Among hosts with same least connections, perform a reservoir
// sample: https://en.wikipedia.org/wiki/Reservoir_sampling
if
host
.
Conns
==
leastConn
{
count
++
if
count
==
1
{
if
(
rand
.
Int
()
%
count
)
==
0
{
bestHost
=
host
}
else
{
r
:=
rand
.
Int
()
%
count
if
r
==
(
count
-
1
)
{
bestHost
=
host
}
}
}
}
...
...
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