FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
stackrox/pkg/httputil/context.go at master · stackrox/stackrox · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
stackrox
/
stackrox
Public
Notifications
You must be signed in to change notification settings
Fork
191
Star
1.3k
Code
Issues
54
Pull requests
603
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
stackrox
/
pkg
/
httputil
/
context.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
67 lines (56 loc) · 1.74 KB
Breadcrumbs
stackrox
/
pkg
/
httputil
/
context.go
Copy path
File metadata and controls
67 lines (56 loc) · 1.74 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package
httputil
import
(
"context"
"io"
"net/http"
"github.com/stackrox/rox/pkg/concurrency"
)
type
contextBoundRoundTripper
struct
{
ctx
concurrency.
ErrorWaitable
delegate
http.
RoundTripper
}
// ContextBoundRoundTripper returns an http.RoundTripper that delegates to the rt for performing requests, but ensures
// every request is canceled when `ctx` is canceled.
func
ContextBoundRoundTripper
(
ctx
concurrency.
ErrorWaitable
,
rt
http.
RoundTripper
) http.
RoundTripper
{
return
&
contextBoundRoundTripper
{
ctx
:
ctx
,
delegate
:
rt
,
}
}
type
cancelOnCloseReader
struct
{
io.
ReadCloser
cancel
context.
CancelFunc
}
func
(
r
*
cancelOnCloseReader
)
Close
()
error
{
r
.
cancel
()
return
r
.
ReadCloser
.
Close
()
}
func
(
r
*
contextBoundRoundTripper
)
RoundTrip
(
req
*
http.
Request
) (
*
http.
Response
,
error
) {
if
ctxErr
:=
r
.
ctx
.
Err
();
ctxErr
!=
nil
{
return
nil
,
ctxErr
}
ctxBoundReq
,
cancel
:=
ContextBoundRequest
(
r
.
ctx
,
req
)
resp
,
err
:=
r
.
delegate
.
RoundTrip
(
ctxBoundReq
)
if
err
!=
nil
{
cancel
()
return
nil
,
err
}
if
resp
.
Body
!=
nil
{
resp
.
Body
=
&
cancelOnCloseReader
{
ReadCloser
:
resp
.
Body
,
cancel
:
cancel
,
}
}
else
{
cancel
()
}
return
resp
,
nil
}
// ContextBoundRequest returns a new *http.Request with a context that is canceled when ctx is canceled. No values of
// ctx will be propagated to the context of the resulting request.
// Callers should ensure that the returned cancel function is called eventually, otherwise this function might leak
// Goroutines.
func
ContextBoundRequest
(
ctx
concurrency.
ErrorWaitable
,
req
*
http.
Request
) (
*
http.
Request
, context.
CancelFunc
) {
subCtx
,
cancel
:=
context
.
WithCancel
(
req
.
Context
())
concurrency
.
CancelContextOnSignal
(
subCtx
,
cancel
,
ctx
)
return
req
.
WithContext
(
subCtx
),
cancel
}
Back
|
FazBrowse Home
|
New Git URL