Skip to content

Commit 86241b4

Browse files
authored
Merge pull request #3120 from sttts/sttts-roundtripper-wrapper
🐛 Implement RoundTripperWrapper everywhere to allow cancellation
2 parents 8f8fe3d + f965ef7 commit 86241b4

File tree

3 files changed

+47
-17
lines changed

3 files changed

+47
-17
lines changed

pkg/cache/client/round_tripper.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ func (c *ShardRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)
8787
return c.delegate.RoundTrip(req)
8888
}
8989

90+
func (c *ShardRoundTripper) WrappedRoundTripper() http.RoundTripper {
91+
return c.delegate
92+
}
93+
9094
// generatePath formats the request path to target the specified shard.
9195
func generatePath(originalPath string, shard clientshard.Name) (string, error) {
9296
// if the originalPath already has the shard then the path was already modified and no change needed
@@ -151,6 +155,10 @@ func (c *DefaultShardRoundTripper) RoundTrip(req *http.Request) (*http.Response,
151155
return c.delegate.RoundTrip(req)
152156
}
153157

158+
func (c *DefaultShardRoundTripper) WrappedRoundTripper() http.RoundTripper {
159+
return c.delegate
160+
}
161+
154162
// WithShardNameFromObjectRoundTripper wraps an existing config with ShardNameFromObjectRoundTripper.
155163
//
156164
// Note: it is the caller responsibility to make a copy of the rest config.
@@ -221,6 +229,10 @@ func (c *ShardNameFromObjectRoundTripper) RoundTrip(req *http.Request) (*http.Re
221229
return c.delegate.RoundTrip(req)
222230
}
223231

232+
func (c *ShardNameFromObjectRoundTripper) WrappedRoundTripper() http.RoundTripper {
233+
return c.delegate
234+
}
235+
224236
// WithCacheServiceRoundTripper wraps an existing config's with CacheServiceRoundTripper.
225237
func WithCacheServiceRoundTripper(cfg *rest.Config) *rest.Config {
226238
cfg.Wrap(func(rt http.RoundTripper) http.RoundTripper {
@@ -260,3 +272,7 @@ func (c *CacheServiceRoundTripper) RoundTrip(req *http.Request) (*http.Response,
260272
}
261273
return c.delegate.RoundTrip(req)
262274
}
275+
276+
func (c *CacheServiceRoundTripper) WrappedRoundTripper() http.RoundTripper {
277+
return c.delegate
278+
}

pkg/metadata/dynamic.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ func (t *metadataTransport) RoundTrip(req *http.Request) (*http.Response, error)
7171
return t.RoundTripper.RoundTrip(req)
7272
}
7373

74+
func (t *metadataTransport) WrappedRoundTripper() http.RoundTripper {
75+
return t.RoundTripper
76+
}
77+
7478
func partialType(req *http.Request) (string, error) {
7579
// strip off /clusters/<lcluster>
7680
baseReq := *req

pkg/server/bootstrap/identity.go

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -226,33 +226,43 @@ func apiExportIdentityProvider(config *rest.Config, localShardKubeClusterClient
226226
}
227227
}
228228

229-
type roundTripperFunc func(*http.Request) (*http.Response, error)
229+
type roundTripperFunc struct {
230+
delegate http.RoundTripper
231+
fn func(*http.Request) (*http.Response, error)
232+
}
230233

231-
var _ http.RoundTripper = roundTripperFunc(nil)
234+
var _ http.RoundTripper = roundTripperFunc{}
232235

233236
func (rt roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) {
234-
return rt(r)
237+
return rt.fn(r)
238+
}
239+
240+
func (rt roundTripperFunc) WrappedRoundTripper() http.RoundTripper {
241+
return rt.delegate
235242
}
236243

237244
// injectKcpIdentities injects the KCP identities into the request URLs.
238245
func injectKcpIdentities(ids *identities) func(rt http.RoundTripper) http.RoundTripper {
239246
return func(rt http.RoundTripper) http.RoundTripper {
240-
return roundTripperFunc(func(origReq *http.Request) (*http.Response, error) {
241-
urlPath, err := decorateWildcardPathsWithResourceIdentities(origReq.URL.Path, ids)
242-
if err != nil {
243-
return nil, err
244-
}
245-
if urlPath == origReq.URL.Path {
246-
return rt.RoundTrip(origReq)
247-
}
247+
return roundTripperFunc{
248+
delegate: rt,
249+
fn: func(origReq *http.Request) (*http.Response, error) {
250+
urlPath, err := decorateWildcardPathsWithResourceIdentities(origReq.URL.Path, ids)
251+
if err != nil {
252+
return nil, err
253+
}
254+
if urlPath == origReq.URL.Path {
255+
return rt.RoundTrip(origReq)
256+
}
248257

249-
req := *origReq // shallow copy
250-
req.URL = &url.URL{}
251-
*req.URL = *origReq.URL
252-
req.URL.Path = urlPath
258+
req := *origReq // shallow copy
259+
req.URL = &url.URL{}
260+
*req.URL = *origReq.URL
261+
req.URL.Path = urlPath
253262

254-
return rt.RoundTrip(&req)
255-
})
263+
return rt.RoundTrip(&req)
264+
},
265+
}
256266
}
257267
}
258268

0 commit comments

Comments
 (0)