Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
f3ba7f95
Commit
f3ba7f95
authored
Mar 12, 2018
by
Dr. Stefan Schimanski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
apiserver: cancel context on timeout in WithTimeoutForNonLongRunningRequests
parent
44ffcdd9
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
6 deletions
+16
-6
timeout.go
staging/src/k8s.io/apiserver/pkg/server/filters/timeout.go
+16
-6
No files found.
staging/src/k8s.io/apiserver/pkg/server/filters/timeout.go
View file @
f3ba7f95
...
@@ -18,6 +18,7 @@ package filters
...
@@ -18,6 +18,7 @@ package filters
import
(
import
(
"bufio"
"bufio"
"context"
"encoding/json"
"encoding/json"
"fmt"
"fmt"
"net"
"net"
...
@@ -54,14 +55,23 @@ func WithTimeoutForNonLongRunningRequests(handler http.Handler, requestContextMa
...
@@ -54,14 +55,23 @@ func WithTimeoutForNonLongRunningRequests(handler http.Handler, requestContextMa
if
longRunning
(
req
,
requestInfo
)
{
if
longRunning
(
req
,
requestInfo
)
{
return
nil
,
nil
,
nil
return
nil
,
nil
,
nil
}
}
metricFn
:=
func
()
{
ctx
,
cancel
:=
context
.
WithCancel
(
ctx
)
if
err
:=
requestContextMapper
.
Update
(
req
,
ctx
);
err
!=
nil
{
return
time
.
After
(
timeout
),
func
()
{},
apierrors
.
NewInternalError
(
fmt
.
Errorf
(
"failed to update context during timeout"
))
}
postTimeoutFn
:=
func
()
{
cancel
()
metrics
.
Record
(
req
,
requestInfo
,
""
,
http
.
StatusGatewayTimeout
,
0
,
0
)
metrics
.
Record
(
req
,
requestInfo
,
""
,
http
.
StatusGatewayTimeout
,
0
,
0
)
}
}
return
time
.
After
(
timeout
),
metric
Fn
,
apierrors
.
NewTimeoutError
(
fmt
.
Sprintf
(
"request did not complete within %s"
,
timeout
),
0
)
return
time
.
After
(
timeout
),
postTimeout
Fn
,
apierrors
.
NewTimeoutError
(
fmt
.
Sprintf
(
"request did not complete within %s"
,
timeout
),
0
)
}
}
return
WithTimeout
(
handler
,
timeoutFunc
)
return
WithTimeout
(
handler
,
timeoutFunc
)
}
}
type
timeoutFunc
=
func
(
*
http
.
Request
)
(
timeout
<-
chan
time
.
Time
,
postTimeoutFunc
func
(),
err
*
apierrors
.
StatusError
)
// WithTimeout returns an http.Handler that runs h with a timeout
// WithTimeout returns an http.Handler that runs h with a timeout
// determined by timeoutFunc. The new http.Handler calls h.ServeHTTP to handle
// determined by timeoutFunc. The new http.Handler calls h.ServeHTTP to handle
// each request, but if a call runs for longer than its time limit, the
// each request, but if a call runs for longer than its time limit, the
...
@@ -71,17 +81,17 @@ func WithTimeoutForNonLongRunningRequests(handler http.Handler, requestContextMa
...
@@ -71,17 +81,17 @@ func WithTimeoutForNonLongRunningRequests(handler http.Handler, requestContextMa
// http.ErrHandlerTimeout. If timeoutFunc returns a nil timeout channel, no
// http.ErrHandlerTimeout. If timeoutFunc returns a nil timeout channel, no
// timeout will be enforced. recordFn is a function that will be invoked whenever
// timeout will be enforced. recordFn is a function that will be invoked whenever
// a timeout happens.
// a timeout happens.
func
WithTimeout
(
h
http
.
Handler
,
timeoutFunc
func
(
*
http
.
Request
)
(
timeout
<-
chan
time
.
Time
,
recordFn
func
(),
err
*
apierrors
.
StatusError
)
)
http
.
Handler
{
func
WithTimeout
(
h
http
.
Handler
,
timeoutFunc
timeoutFunc
)
http
.
Handler
{
return
&
timeoutHandler
{
h
,
timeoutFunc
}
return
&
timeoutHandler
{
h
,
timeoutFunc
}
}
}
type
timeoutHandler
struct
{
type
timeoutHandler
struct
{
handler
http
.
Handler
handler
http
.
Handler
timeout
func
(
*
http
.
Request
)
(
<-
chan
time
.
Time
,
func
(),
*
apierrors
.
StatusError
)
timeout
timeoutFunc
}
}
func
(
t
*
timeoutHandler
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
(
t
*
timeoutHandler
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
after
,
record
Fn
,
err
:=
t
.
timeout
(
r
)
after
,
postTimeout
Fn
,
err
:=
t
.
timeout
(
r
)
if
after
==
nil
{
if
after
==
nil
{
t
.
handler
.
ServeHTTP
(
w
,
r
)
t
.
handler
.
ServeHTTP
(
w
,
r
)
return
return
...
@@ -97,7 +107,7 @@ func (t *timeoutHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
...
@@ -97,7 +107,7 @@ func (t *timeoutHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
case
<-
done
:
case
<-
done
:
return
return
case
<-
after
:
case
<-
after
:
record
Fn
()
postTimeout
Fn
()
tw
.
timeout
(
err
)
tw
.
timeout
(
err
)
}
}
}
}
...
...
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