Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
go-common
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ai3
go-common
Commits
0868f264
Commit
0868f264
authored
6 years ago
by
ale
Browse files
Options
Downloads
Patches
Plain Diff
Reduce timeout on calls to backends with multiple targets
Allows for failover in case a target times out.
parent
3f5c3e1b
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
clientutil/balancer.go
+37
-10
37 additions, 10 deletions
clientutil/balancer.go
with
37 additions
and
10 deletions
clientutil/balancer.go
+
37
−
10
View file @
0868f264
...
...
@@ -98,28 +98,35 @@ func newBalancedBackend(config *BackendConfig, resolver resolver) (*balancedBack
// with a JSON-encoded request body. It will attempt to decode the
// response body as JSON.
func
(
b
*
balancedBackend
)
Call
(
ctx
context
.
Context
,
shard
,
path
string
,
req
,
resp
interface
{})
error
{
// Serialize the request body.
data
,
err
:=
json
.
Marshal
(
req
)
if
err
!=
nil
{
return
err
}
var
tg
targetGenerator
=
b
.
backendTracker
if
b
.
sharded
{
if
shard
==
""
{
return
fmt
.
Errorf
(
"call without shard to sharded service %s"
,
b
.
baseURI
.
String
())
}
tg
=
newShardedGenerator
(
shard
,
b
.
baseURI
.
Host
,
b
.
resolver
)
// Create the target sequence for this call. If there are multiple
// targets, reduce the timeout on each individual call accordingly to
// accomodate eventual failover.
seq
,
err
:=
b
.
makeSequence
(
shard
)
if
err
!=
nil
{
return
err
}
innerTimeout
:=
1
*
time
.
Hour
if
deadline
,
ok
:=
ctx
.
Deadline
();
ok
{
innerTimeout
=
time
.
Until
(
deadline
)
/
time
.
Duration
(
seq
.
Len
())
}
seq
:=
newSequence
(
tg
)
b
.
log
.
Printf
(
"%016x: initialized"
,
seq
.
ID
())
// Call the backends in the sequence until one succeeds, with an
// exponential backoff policy controlled by the outer Context.
var
httpResp
*
http
.
Response
err
=
backoff
.
Retry
(
func
()
error
{
req
,
rerr
:=
b
.
newJSONRequest
(
path
,
shard
,
data
)
if
rerr
!=
nil
{
return
rerr
}
httpResp
,
rerr
=
b
.
do
(
ctx
,
seq
,
req
)
innerCtx
,
cancel
:=
context
.
WithTimeout
(
ctx
,
innerTimeout
)
httpResp
,
rerr
=
b
.
do
(
innerCtx
,
seq
,
req
)
cancel
()
return
rerr
},
backoff
.
WithContext
(
newExponentialBackOff
(),
ctx
))
if
err
!=
nil
{
...
...
@@ -127,16 +134,34 @@ func (b *balancedBackend) Call(ctx context.Context, shard, path string, req, res
}
defer
httpResp
.
Body
.
Close
()
// nolint
// Decode the response.
if
httpResp
.
Header
.
Get
(
"Content-Type"
)
!=
"application/json"
{
return
errors
.
New
(
"not a JSON response"
)
}
if
resp
==
nil
{
return
nil
}
return
json
.
NewDecoder
(
httpResp
.
Body
)
.
Decode
(
resp
)
}
// Initialize a new target sequence.
func
(
b
*
balancedBackend
)
makeSequence
(
shard
string
)
(
*
sequence
,
error
)
{
var
tg
targetGenerator
=
b
.
backendTracker
if
b
.
sharded
{
if
shard
==
""
{
return
nil
,
fmt
.
Errorf
(
"call without shard to sharded service %s"
,
b
.
baseURI
.
String
())
}
tg
=
newShardedGenerator
(
shard
,
b
.
baseURI
.
Host
,
b
.
resolver
)
}
seq
:=
newSequence
(
tg
)
if
seq
.
Len
()
==
0
{
return
nil
,
errNoTargets
}
b
.
log
.
Printf
(
"%016x: initialized"
,
seq
.
ID
())
return
seq
,
nil
}
// Return the URI to be used for the request. This is used both in the
// Host HTTP header and as the TLS server name used to pick a server
// certificate (if using TLS).
...
...
@@ -213,6 +238,8 @@ func newSequence(tg targetGenerator) *sequence {
func
(
s
*
sequence
)
ID
()
uint64
{
return
s
.
id
}
func
(
s
*
sequence
)
Len
()
int
{
return
len
(
s
.
targets
)
}
func
(
s
*
sequence
)
reloadTargets
()
{
targets
:=
s
.
tg
.
getTargets
()
if
len
(
targets
)
>
0
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment