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
8070548e
Commit
8070548e
authored
Dec 08, 2016
by
Kris
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Context() to enable per-request cancellation
parent
79f497bc
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
49 additions
and
4 deletions
+49
-4
request.go
pkg/client/restclient/request.go
+19
-4
request_test.go
pkg/client/restclient/request_test.go
+30
-0
No files found.
pkg/client/restclient/request.go
View file @
8070548e
...
@@ -18,6 +18,7 @@ package restclient
...
@@ -18,6 +18,7 @@ package restclient
import
(
import
(
"bytes"
"bytes"
"context"
"encoding/hex"
"encoding/hex"
"fmt"
"fmt"
"io"
"io"
...
@@ -105,16 +106,14 @@ type Request struct {
...
@@ -105,16 +106,14 @@ type Request struct {
resource
string
resource
string
resourceName
string
resourceName
string
subresource
string
subresource
string
selector
labels
.
Selector
timeout
time
.
Duration
timeout
time
.
Duration
// output
// output
err
error
err
error
body
io
.
Reader
body
io
.
Reader
// The constructed request and the response
// This is only used for per-request timeouts, deadlines, and cancellations.
req
*
http
.
Request
ctx
context
.
Context
resp
*
http
.
Response
backoffMgr
BackoffManager
backoffMgr
BackoffManager
throttle
flowcontrol
.
RateLimiter
throttle
flowcontrol
.
RateLimiter
...
@@ -566,6 +565,13 @@ func (r *Request) Body(obj interface{}) *Request {
...
@@ -566,6 +565,13 @@ func (r *Request) Body(obj interface{}) *Request {
return
r
return
r
}
}
// Context adds a context to the request. Contexts are only used for
// timeouts, deadlines, and cancellations.
func
(
r
*
Request
)
Context
(
ctx
context
.
Context
)
*
Request
{
r
.
ctx
=
ctx
return
r
}
// URL returns the current working URL.
// URL returns the current working URL.
func
(
r
*
Request
)
URL
()
*
url
.
URL
{
func
(
r
*
Request
)
URL
()
*
url
.
URL
{
p
:=
r
.
pathPrefix
p
:=
r
.
pathPrefix
...
@@ -651,6 +657,9 @@ func (r *Request) Watch() (watch.Interface, error) {
...
@@ -651,6 +657,9 @@ func (r *Request) Watch() (watch.Interface, error) {
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
if
r
.
ctx
!=
nil
{
req
=
req
.
WithContext
(
r
.
ctx
)
}
req
.
Header
=
r
.
headers
req
.
Header
=
r
.
headers
client
:=
r
.
client
client
:=
r
.
client
if
client
==
nil
{
if
client
==
nil
{
...
@@ -720,6 +729,9 @@ func (r *Request) Stream() (io.ReadCloser, error) {
...
@@ -720,6 +729,9 @@ func (r *Request) Stream() (io.ReadCloser, error) {
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
if
r
.
ctx
!=
nil
{
req
=
req
.
WithContext
(
r
.
ctx
)
}
req
.
Header
=
r
.
headers
req
.
Header
=
r
.
headers
client
:=
r
.
client
client
:=
r
.
client
if
client
==
nil
{
if
client
==
nil
{
...
@@ -794,6 +806,9 @@ func (r *Request) request(fn func(*http.Request, *http.Response)) error {
...
@@ -794,6 +806,9 @@ func (r *Request) request(fn func(*http.Request, *http.Response)) error {
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
if
r
.
ctx
!=
nil
{
req
=
req
.
WithContext
(
r
.
ctx
)
}
req
.
Header
=
r
.
headers
req
.
Header
=
r
.
headers
r
.
backoffMgr
.
Sleep
(
r
.
backoffMgr
.
CalculateBackoff
(
r
.
URL
()))
r
.
backoffMgr
.
Sleep
(
r
.
backoffMgr
.
CalculateBackoff
(
r
.
URL
()))
...
...
pkg/client/restclient/request_test.go
View file @
8070548e
...
@@ -18,6 +18,7 @@ package restclient
...
@@ -18,6 +18,7 @@ package restclient
import
(
import
(
"bytes"
"bytes"
"context"
"errors"
"errors"
"fmt"
"fmt"
"io"
"io"
...
@@ -1621,3 +1622,32 @@ func testRESTClient(t testing.TB, srv *httptest.Server) *RESTClient {
...
@@ -1621,3 +1622,32 @@ func testRESTClient(t testing.TB, srv *httptest.Server) *RESTClient {
}
}
return
client
return
client
}
}
func
TestDoContext
(
t
*
testing
.
T
)
{
receivedCh
:=
make
(
chan
struct
{})
block
:=
make
(
chan
struct
{})
testServer
:=
httptest
.
NewServer
(
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
req
*
http
.
Request
)
{
close
(
receivedCh
)
<-
block
w
.
WriteHeader
(
http
.
StatusOK
)
}))
defer
testServer
.
Close
()
defer
close
(
block
)
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
defer
cancel
()
go
func
()
{
<-
receivedCh
cancel
()
}()
c
:=
testRESTClient
(
t
,
testServer
)
_
,
err
:=
c
.
Verb
(
"GET"
)
.
Context
(
ctx
)
.
Prefix
(
"foo"
)
.
DoRaw
()
if
err
==
nil
{
t
.
Fatal
(
"Expected context cancellation error"
)
}
}
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