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
d4396660
Commit
d4396660
authored
5 years ago
by
ale
Browse files
Options
Downloads
Patches
Plain Diff
Propagate context deadlines across RPC boundaries
parent
8a072922
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/balancer.go
+14
-1
14 additions, 1 deletion
clientutil/balancer.go
serverutil/http.go
+21
-1
21 additions, 1 deletion
serverutil/http.go
with
35 additions
and
2 deletions
clientutil/balancer.go
+
14
−
1
View file @
d4396660
...
...
@@ -221,7 +221,7 @@ func (b *balancedBackend) do(ctx context.Context, seq *sequence, req *http.Reque
client
:=
&
http
.
Client
{
Transport
:
b
.
transportCache
.
getTransport
(
target
),
}
resp
,
err
=
client
.
Do
(
req
.
WithContext
(
ctx
))
resp
,
err
=
client
.
Do
(
propagateDeadline
(
ctx
,
req
))
if
err
==
nil
&&
resp
.
StatusCode
!=
200
{
err
=
remoteErrorFromResponse
(
resp
)
if
!
isStatusTemporary
(
resp
.
StatusCode
)
{
...
...
@@ -235,6 +235,19 @@ func (b *balancedBackend) do(ctx context.Context, seq *sequence, req *http.Reque
return
}
const
deadlineHeader
=
"X-RPC-Deadline"
// Propagate context deadline to the server using a HTTP header.
func
propagateDeadline
(
ctx
context
.
Context
,
req
*
http
.
Request
)
*
http
.
Request
{
req
=
req
.
WithContext
(
ctx
)
if
deadline
,
ok
:=
ctx
.
Deadline
();
ok
{
req
.
Header
.
Set
(
deadlineHeader
,
strconv
.
FormatInt
(
deadline
.
UTC
()
.
UnixNano
(),
10
))
}
else
{
req
.
Header
.
Del
(
deadlineHeader
)
}
return
req
}
var
errNoTargets
=
errors
.
New
(
"no available backends"
)
type
targetGenerator
interface
{
...
...
This diff is collapsed.
Click to expand it.
serverutil/http.go
+
21
−
1
View file @
d4396660
...
...
@@ -11,6 +11,7 @@ import (
_
"net/http/pprof"
"os"
"os/signal"
"strconv"
"syscall"
"time"
...
...
@@ -162,11 +163,30 @@ func addDefaultHandlers(h http.Handler) http.Handler {
// Prometheus instrumentation (requests to /metrics and
// /health are not included).
root
.
Handle
(
"/"
,
promhttp
.
InstrumentHandlerInFlight
(
inFlightRequests
,
promhttp
.
InstrumentHandlerCounter
(
totalRequests
,
h
)))
promhttp
.
InstrumentHandlerCounter
(
totalRequests
,
propagateDeadline
(
h
))))
return
root
}
const
deadlineHeader
=
"X-RPC-Deadline"
// Read an eventual deadline from the HTTP request, and set it as the
// deadline of the request context.
func
propagateDeadline
(
h
http
.
Handler
)
http
.
Handler
{
return
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
req
*
http
.
Request
)
{
if
hdr
:=
req
.
Header
.
Get
(
deadlineHeader
);
hdr
!=
""
{
if
deadlineNano
,
err
:=
strconv
.
ParseInt
(
hdr
,
10
,
64
);
err
==
nil
{
deadline
:=
time
.
Unix
(
0
,
deadlineNano
)
ctx
,
cancel
:=
context
.
WithDeadline
(
req
.
Context
(),
deadline
)
defer
cancel
()
req
=
req
.
WithContext
(
ctx
)
}
}
h
.
ServeHTTP
(
w
,
req
)
})
}
func
guessEndpointName
(
addr
string
)
string
{
_
,
port
,
err
:=
net
.
SplitHostPort
(
addr
)
if
err
!=
nil
{
...
...
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