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
13036824
Commit
13036824
authored
4 years ago
by
ale
Browse files
Options
Downloads
Patches
Plain Diff
Add a Get method to avoid JSON decoding overhead
Useful for large blob responses.
parent
f2392bd3
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
clientutil/backend.go
+8
-0
8 additions, 0 deletions
clientutil/backend.go
clientutil/balancer.go
+38
-0
38 additions, 0 deletions
clientutil/balancer.go
with
46 additions
and
0 deletions
clientutil/backend.go
+
8
−
0
View file @
13036824
...
...
@@ -2,6 +2,7 @@ package clientutil
import
(
"context"
"net/http"
)
// BackendConfig specifies the configuration of a service backend.
...
...
@@ -39,6 +40,13 @@ type Backend interface {
// *without* a shard ID on a sharded service is an error.
Call
(
context
.
Context
,
string
,
string
,
interface
{},
interface
{})
error
// Make a simple HTTP GET request to the remote backend,
// without parsing the response as JSON.
//
// Useful for streaming large responses, where the JSON
// encoding overhead is undesirable.
Get
(
context
.
Context
,
string
,
string
)
(
*
http
.
Response
,
error
)
// Close all resources associated with the backend.
Close
()
}
...
...
This diff is collapsed.
Click to expand it.
clientutil/balancer.go
+
38
−
0
View file @
13036824
...
...
@@ -168,6 +168,44 @@ func (b *balancedBackend) Call(ctx context.Context, shard, path string, req, res
},
backoff
.
WithContext
(
newExponentialBackOff
(),
ctx
))
}
// Makes a generic HTTP GET request to the backend uri.
func
(
b
*
balancedBackend
)
Get
(
ctx
context
.
Context
,
shard
,
path
string
)
(
*
http
.
Response
,
error
)
{
// 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
nil
,
err
}
innerTimeout
:=
1
*
time
.
Hour
if
deadline
,
ok
:=
ctx
.
Deadline
();
ok
{
innerTimeout
=
time
.
Until
(
deadline
)
/
time
.
Duration
(
seq
.
Len
())
}
if
b
.
requestMaxTimeout
>
0
&&
innerTimeout
>
b
.
requestMaxTimeout
{
innerTimeout
=
b
.
requestMaxTimeout
}
req
,
err
:=
http
.
NewRequest
(
"GET"
,
b
.
getURIForRequest
(
shard
,
path
),
nil
)
if
err
!=
nil
{
return
nil
,
err
}
// Call the backends in the sequence until one succeeds, with an
// exponential backoff policy controlled by the outer Context.
var
resp
*
http
.
Response
err
=
backoff
.
Retry
(
func
()
error
{
innerCtx
,
cancel
:=
context
.
WithTimeout
(
ctx
,
innerTimeout
)
defer
cancel
()
// When do() returns successfully, we already know that the
// response had an HTTP status of 200.
var
rerr
error
resp
,
rerr
=
b
.
do
(
innerCtx
,
seq
,
req
)
return
rerr
},
backoff
.
WithContext
(
newExponentialBackOff
(),
ctx
))
return
resp
,
err
}
// Initialize a new target sequence.
func
(
b
*
balancedBackend
)
makeSequence
(
shard
string
)
(
*
sequence
,
error
)
{
var
tg
targetGenerator
=
b
.
backendTracker
...
...
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