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
a661007a
Commit
a661007a
authored
Sep 24, 2016
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
proxy: Fix retry logic for when no hosts are available
parent
0c0142c8
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
11 deletions
+20
-11
caddyhttp/proxy/proxy.go
caddyhttp/proxy/proxy.go
+20
-7
caddyhttp/proxy/proxy_test.go
caddyhttp/proxy/proxy_test.go
+0
-4
No files found.
caddyhttp/proxy/proxy.go
View file @
a661007a
...
...
@@ -94,17 +94,33 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
// outreq is the request that makes a roundtrip to the backend
outreq
:=
createUpstreamRequest
(
r
)
// since Select() should give us "up" hosts, keep retrying
// hosts until timeout (or until we get a nil host).
// The keepRetrying function will return true if we should
// loop and try to select another host, or false if we
// should break and stop retrying.
start
:=
time
.
Now
()
keepRetrying
:=
func
()
bool
{
// if we've tried long enough, break
if
time
.
Now
()
.
Sub
(
start
)
>=
upstream
.
GetTryDuration
()
{
return
false
}
// otherwise, wait and try the next available host
time
.
Sleep
(
upstream
.
GetTryInterval
())
return
true
}
var
backendErr
error
for
{
// since Select() should give us "up" hosts, keep retrying
// hosts until timeout (or until we get a nil host).
host
:=
upstream
.
Select
(
r
)
if
host
==
nil
{
if
backendErr
==
nil
{
backendErr
=
errors
.
New
(
"no hosts available upstream"
)
}
return
http
.
StatusBadGateway
,
backendErr
if
!
keepRetrying
()
{
break
}
continue
}
if
rr
,
ok
:=
w
.
(
*
httpserver
.
ResponseRecorder
);
ok
&&
rr
.
Replacer
!=
nil
{
rr
.
Replacer
.
Set
(
"upstream"
,
host
.
Name
)
...
...
@@ -170,12 +186,9 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
}
// if we've tried long enough, break
if
time
.
Now
()
.
Sub
(
start
)
>=
upstream
.
GetTryDuration
()
{
if
!
keepRetrying
()
{
break
}
// otherwise, wait and try the next available host
time
.
Sleep
(
upstream
.
GetTryInterval
())
}
return
http
.
StatusBadGateway
,
backendErr
...
...
caddyhttp/proxy/proxy_test.go
View file @
a661007a
...
...
@@ -24,10 +24,6 @@ import (
"golang.org/x/net/websocket"
)
func
init
()
{
tryDuration
=
50
*
time
.
Millisecond
// prevent tests from hanging
}
func
TestReverseProxy
(
t
*
testing
.
T
)
{
log
.
SetOutput
(
ioutil
.
Discard
)
defer
log
.
SetOutput
(
os
.
Stderr
)
...
...
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