Skip to content

Commit af21388

Browse files
committed
Remove context attribute from various api structs and pass to methods instead
1 parent 4c2e81e commit af21388

File tree

55 files changed

+256
-255
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+256
-255
lines changed

api/agent/uniter/resource.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,9 @@ func NewResourcesFacadeClient(caller base.APICaller, unitTag names.UnitTag) (*Re
4343
return nil, errors.Trace(err)
4444
}
4545

46-
ctx := caller.Context()
4746
return &ResourcesFacadeClient{
4847
FacadeCaller: facadeCaller,
49-
HTTPDoer: NewUnitHTTPClient(ctx, httpClient, unitTag.String()),
50-
ctx: ctx,
48+
HTTPDoer: NewUnitHTTPClient(httpClient, unitTag.String()),
5149
}, nil
5250
}
5351

@@ -56,19 +54,18 @@ func NewResourcesFacadeClient(caller base.APICaller, unitTag names.UnitTag) (*Re
5654
type ResourcesFacadeClient struct {
5755
FacadeCaller
5856
apihttp.HTTPDoer
59-
ctx context.Context
6057
}
6158

6259
// GetResource opens the resource (metadata/blob), if it exists, via
6360
// the HTTP API and returns it. If it does not exist or hasn't been
6461
// uploaded yet then errors.NotFound is returned.
65-
func (c *ResourcesFacadeClient) GetResource(resourceName string) (resources.Resource, io.ReadCloser, error) {
62+
func (c *ResourcesFacadeClient) GetResource(ctx context.Context, resourceName string) (resources.Resource, io.ReadCloser, error) {
6663
var response *http.Response
6764
req, err := api.NewHTTPDownloadRequest(resourceName)
6865
if err != nil {
6966
return resources.Resource{}, nil, errors.Annotate(err, "failed to build API request")
7067
}
71-
if err := c.Do(c.ctx, req, &response); err != nil {
68+
if err := c.Do(ctx, req, &response); err != nil {
7269
return resources.Resource{}, nil, errors.Annotate(err, "HTTP request failed")
7370
}
7471

@@ -114,17 +111,15 @@ func (c *ResourcesFacadeClient) getResourceInfo(resourceName string) (resources.
114111
type unitHTTPClient struct {
115112
apihttp.HTTPDoer
116113
unitName string
117-
ctx context.Context
118114
}
119115

120116
// NewUnitHTTPClient wraps an HTTP client (a la httprequest.Client)
121117
// with unit information. This allows rewriting of the URL to match
122118
// the relevant unit.
123-
func NewUnitHTTPClient(ctx context.Context, client apihttp.HTTPDoer, unitName string) UnitHTTPClient {
119+
func NewUnitHTTPClient(client apihttp.HTTPDoer, unitName string) UnitHTTPClient {
124120
return &unitHTTPClient{
125121
HTTPDoer: client,
126122
unitName: unitName,
127-
ctx: ctx,
128123
}
129124
}
130125

api/apiclient.go

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ type rpcConnection interface {
6767

6868
// conn is the internal implementation of the Connection interface.
6969
type conn struct {
70-
ctx context.Context
7170
client rpcConnection
7271
conn jsoncodec.JSONConn
7372
clock clock.Clock
@@ -254,7 +253,6 @@ func Open(info *Info, opts DialOpts) (Connection, error) {
254253
}
255254

256255
c := &conn{
257-
ctx: context.Background(),
258256
client: client,
259257
conn: dialResult.conn,
260258
clock: opts.Clock,
@@ -324,12 +322,10 @@ func PerferredHost(info *Info) string {
324322

325323
// loginWithContext wraps conn.Login with code that terminates
326324
// if the context is cancelled.
327-
// TODO(rogpeppe) pass Context into Login (and all API calls) so
328-
// that this becomes unnecessary.
329325
func loginWithContext(ctx context.Context, st *conn, info *Info) error {
330326
result := make(chan error, 1)
331327
go func() {
332-
result <- st.Login(info.Tag, info.Password, info.Nonce, info.Macaroons)
328+
result <- st.Login(ctx, info.Tag, info.Password, info.Nonce, info.Macaroons)
333329
}()
334330
select {
335331
case err := <-result:
@@ -359,20 +355,15 @@ func (t *hostSwitchingTransport) RoundTrip(req *http.Request) (*http.Response, e
359355
return t.fallback.RoundTrip(req)
360356
}
361357

362-
// Context returns the context associated with this conn.
363-
func (c *conn) Context() context.Context {
364-
return c.ctx
365-
}
366-
367358
// ConnectStream implements StreamConnector.ConnectStream. The stream
368359
// returned will apply a 30-second write deadline, so WriteJSON should
369360
// only be called from one goroutine.
370-
func (c *conn) ConnectStream(path string, attrs url.Values) (base.Stream, error) {
361+
func (c *conn) ConnectStream(ctx context.Context, path string, attrs url.Values) (base.Stream, error) {
371362
path, err := apiPath(c.modelTag.Id(), path)
372363
if err != nil {
373364
return nil, errors.Trace(err)
374365
}
375-
conn, err := c.connectStreamWithRetry(path, attrs, nil)
366+
conn, err := c.connectStreamWithRetry(ctx, path, attrs, nil)
376367
if err != nil {
377368
return nil, errors.Trace(err)
378369
}
@@ -384,21 +375,21 @@ func (c *conn) ConnectStream(path string, attrs url.Values) (base.Stream, error)
384375
// endpoint needs one) can be specified in the headers. The stream
385376
// returned will apply a 30-second write deadline, so WriteJSON should
386377
// only be called from one goroutine.
387-
func (c *conn) ConnectControllerStream(path string, attrs url.Values, headers http.Header) (base.Stream, error) {
378+
func (c *conn) ConnectControllerStream(ctx context.Context, path string, attrs url.Values, headers http.Header) (base.Stream, error) {
388379
if !strings.HasPrefix(path, "/") {
389380
return nil, errors.Errorf("path %q is not absolute", path)
390381
}
391382
if strings.HasPrefix(path, modelRoot) {
392383
return nil, errors.Errorf("path %q is model-specific", path)
393384
}
394-
conn, err := c.connectStreamWithRetry(path, attrs, headers)
385+
conn, err := c.connectStreamWithRetry(ctx, path, attrs, headers)
395386
if err != nil {
396387
return nil, errors.Trace(err)
397388
}
398389
return conn, nil
399390
}
400391

401-
func (c *conn) connectStreamWithRetry(path string, attrs url.Values, headers http.Header) (base.Stream, error) {
392+
func (c *conn) connectStreamWithRetry(ctx context.Context, path string, attrs url.Values, headers http.Header) (base.Stream, error) {
402393
if !c.isLoggedIn() {
403394
return nil, errors.New("cannot use ConnectStream without logging in")
404395
}
@@ -415,7 +406,7 @@ func (c *conn) connectStreamWithRetry(path string, attrs url.Values, headers htt
415406
if params.ErrCode(err) != params.CodeDischargeRequired {
416407
return nil, errors.Trace(err)
417408
}
418-
if err := c.bakeryClient.HandleError(c.ctx, c.cookieURL, bakeryError(err)); err != nil {
409+
if err := c.bakeryClient.HandleError(ctx, c.cookieURL, bakeryError(err)); err != nil {
419410
return nil, errors.Trace(err)
420411
}
421412
// Try again with the discharged macaroon.

api/base/caller.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ type APICaller interface {
5252
// BakeryClient returns the bakery client for this connection.
5353
BakeryClient() MacaroonDischarger
5454

55-
// Context returns the standard context for this connection.
56-
Context() context.Context
57-
5855
StreamConnector
5956
ControllerStreamConnector
6057
}
@@ -76,7 +73,7 @@ type StreamConnector interface {
7673
// when making the initial HTTP request.
7774
//
7875
// The path must start with a "/".
79-
ConnectStream(path string, attrs url.Values) (Stream, error)
76+
ConnectStream(ctx context.Context, path string, attrs url.Values) (Stream, error)
8077
}
8178

8279
// ControllerStreamConnector is implemented by the client-facing State object.
@@ -88,7 +85,7 @@ type ControllerStreamConnector interface {
8885
// request.
8986
//
9087
// The path must be absolute and can't start with "/model".
91-
ConnectControllerStream(path string, attrs url.Values, headers http.Header) (Stream, error)
88+
ConnectControllerStream(ctx context.Context, path string, attrs url.Values, headers http.Header) (Stream, error)
9289
}
9390

9491
// Stream represents a streaming connection to the API.
@@ -149,15 +146,10 @@ func NewFacadeCaller(caller APICaller, facadeName string, options ...Option) Fac
149146
// NewFacadeCallerForVersion wraps an APICaller for a given facade
150147
// name and version.
151148
func NewFacadeCallerForVersion(caller APICaller, facadeName string, version int, options ...Option) FacadeCaller {
152-
// Derive the context from the API caller context if it's available. The
153-
// default will be a noop tracer if none is found.
154-
tracer, _ := coretrace.TracerFromContext(caller.Context())
155-
156149
fc := facadeCaller{
157150
facadeName: facadeName,
158151
bestVersion: version,
159152
caller: caller,
160-
tracer: tracer,
161153
}
162154

163155
for _, option := range options {

api/base/caller_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
package base_test
55

66
import (
7-
"context"
8-
97
"github.com/juju/testing"
108
"go.uber.org/mock/gomock"
119
gc "gopkg.in/check.v1"
@@ -28,7 +26,7 @@ func (s *apiCallerSuite) TestNewFacadeCaller(c *gc.C) {
2826

2927
facade := base.NewFacadeCaller(s.apiCaller, "Foo")
3028
c.Assert(facade, gc.NotNil)
31-
c.Check(facade.(Tracer).Tracer(), gc.Equals, coretrace.NoopTracer{})
29+
c.Check(facade.(Tracer).Tracer(), gc.IsNil)
3230
}
3331

3432
func (s *apiCallerSuite) TestNewFacadeCallerWithTracer(c *gc.C) {
@@ -44,7 +42,6 @@ func (s *apiCallerSuite) setupMocks(c *gc.C) *gomock.Controller {
4442

4543
s.apiCaller = mocks.NewMockAPICaller(ctrl)
4644
s.apiCaller.EXPECT().BestFacadeVersion("Foo").Return(1)
47-
s.apiCaller.EXPECT().Context().Return(context.Background()).AnyTimes()
4845

4946
return ctrl
5047
}

api/base/clientfacade.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33

44
package base
55

6-
import (
7-
coretrace "github.com/juju/juju/core/trace"
8-
)
9-
106
// APICallCloser is the same as APICaller, but also provides a Close() method
117
// for when we are done with this connection.
128
type APICallCloser interface {
@@ -45,15 +41,10 @@ var _ ClientFacade = (*clientFacade)(nil)
4541
// It is expected that most client-facing facades will embed a ClientFacade and
4642
// will use a FacadeCaller so this function returns both.
4743
func NewClientFacade(caller APICallCloser, facadeName string, options ...Option) (ClientFacade, FacadeCaller) {
48-
// Derive the context from the API caller context if it's available. The
49-
// default will be a noop tracer if none is found.
50-
tracer, _ := coretrace.TracerFromContext(caller.Context())
51-
5244
fc := facadeCaller{
5345
facadeName: facadeName,
5446
bestVersion: caller.BestFacadeVersion(facadeName),
5547
caller: caller,
56-
tracer: tracer,
5748
}
5849
for _, option := range options {
5950
fc = option(fc)

api/base/testing/apicaller.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ func (APICallerFunc) ModelTag() (names.ModelTag, bool) {
3737
return names.NewModelTag("deadbeef-0bad-400d-8000-4b1d0d06f00d"), true
3838
}
3939

40-
func (APICallerFunc) Context() context.Context {
41-
return context.Background()
42-
}
43-
4440
func (APICallerFunc) Close() error {
4541
return nil
4642
}
@@ -57,11 +53,11 @@ func (APICallerFunc) BakeryClient() base.MacaroonDischarger {
5753
panic("no bakery client available in this test")
5854
}
5955

60-
func (APICallerFunc) ConnectStream(path string, attrs url.Values) (base.Stream, error) {
56+
func (APICallerFunc) ConnectStream(_ context.Context, path string, attrs url.Values) (base.Stream, error) {
6157
return nil, errors.NotImplementedf("stream connection")
6258
}
6359

64-
func (APICallerFunc) ConnectControllerStream(path string, attrs url.Values, headers http.Header) (base.Stream, error) {
60+
func (APICallerFunc) ConnectControllerStream(_ context.Context, path string, attrs url.Values, headers http.Header) (base.Stream, error) {
6561
return nil, errors.NotImplementedf("controller stream connection")
6662
}
6763

api/client/backups/download.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package backups
55

66
import (
7+
"context"
78
"io"
89
"net/http"
910

@@ -19,15 +20,15 @@ type downloadParams struct {
1920
}
2021

2122
// Download returns an io.ReadCloser for the given backup id.
22-
func (c *Client) Download(filename string) (io.ReadCloser, error) {
23+
func (c *Client) Download(ctx context.Context, filename string) (io.ReadCloser, error) {
2324
httpClient, err := c.st.HTTPClient()
2425
if err != nil {
2526
return nil, errors.Trace(err)
2627
}
2728

2829
var resp *http.Response
2930
err = httpClient.Call(
30-
c.st.Context(),
31+
ctx,
3132
&downloadParams{
3233
Body: params.BackupsDownloadArgs{
3334
ID: filename,

api/client/charms/downloader.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,16 @@ import (
1616

1717
// CharmOpener provides the OpenCharm method.
1818
type CharmOpener interface {
19-
OpenCharm(curl string) (io.ReadCloser, error)
19+
OpenCharm(ctx context.Context, curl string) (io.ReadCloser, error)
2020
}
2121

2222
type charmOpener struct {
23-
ctx context.Context
2423
httpClient http.HTTPDoer
2524
}
2625

27-
func (s *charmOpener) OpenCharm(curl string) (io.ReadCloser, error) {
26+
func (s *charmOpener) OpenCharm(ctx context.Context, curl string) (io.ReadCloser, error) {
2827
uri, query := openCharmArgs(curl)
29-
return http.OpenURI(s.ctx, s.httpClient, uri, query)
28+
return http.OpenURI(ctx, s.httpClient, uri, query)
3029
}
3130

3231
// NewCharmOpener returns a charm opener for the specified caller.
@@ -36,7 +35,6 @@ func NewCharmOpener(apiConn base.APICaller) (CharmOpener, error) {
3635
return nil, errors.Trace(err)
3736
}
3837
return &charmOpener{
39-
ctx: apiConn.Context(),
4038
httpClient: httpClient,
4139
}, nil
4240
}

api/client/client/client.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,17 +209,17 @@ func (c *Client) AbortCurrentUpgrade() error {
209209
}
210210

211211
// UploadTools uploads tools at the specified location to the API server over HTTPS.
212-
func (c *Client) UploadTools(r io.ReadSeeker, vers version.Binary, additionalSeries ...string) (tools.List, error) {
212+
func (c *Client) UploadTools(ctx context.Context, r io.ReadSeeker, vers version.Binary, additionalSeries ...string) (tools.List, error) {
213213
endpoint := fmt.Sprintf("/tools?binaryVersion=%s&series=%s", vers, strings.Join(additionalSeries, ","))
214214
contentType := "application/x-tar-gz"
215215
var resp params.ToolsResult
216-
if err := c.httpPost(r, endpoint, contentType, &resp); err != nil {
216+
if err := c.httpPost(ctx, r, endpoint, contentType, &resp); err != nil {
217217
return nil, errors.Trace(err)
218218
}
219219
return resp.ToolsList, nil
220220
}
221221

222-
func (c *Client) httpPost(content io.ReadSeeker, endpoint, contentType string, response interface{}) error {
222+
func (c *Client) httpPost(ctx context.Context, content io.ReadSeeker, endpoint, contentType string, response interface{}) error {
223223
req, err := http.NewRequest("POST", endpoint, content)
224224
if err != nil {
225225
return errors.Annotate(err, "cannot create upload request")
@@ -232,7 +232,7 @@ func (c *Client) httpPost(content io.ReadSeeker, endpoint, contentType string, r
232232
return errors.Trace(err)
233233
}
234234

235-
if err := httpClient.Do(c.facade.RawAPICaller().Context(), req, response); err != nil {
235+
if err := httpClient.Do(ctx, req, response); err != nil {
236236
return errors.Trace(err)
237237
}
238238
return nil

api/client/modelupgrader/upgrader.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (c *Client) UpgradeModel(
8080
}
8181

8282
// UploadTools uploads tools at the specified location to the API server over HTTPS.
83-
func (c *Client) UploadTools(_ context.Context, r io.ReadSeeker, vers version.Binary) (tools.List, error) {
83+
func (c *Client) UploadTools(ctx context.Context, r io.ReadSeeker, vers version.Binary) (tools.List, error) {
8484
req, err := http.NewRequest("POST", fmt.Sprintf("/tools?binaryVersion=%s", vers), r)
8585
if err != nil {
8686
return nil, errors.Annotate(err, "cannot create upload request")
@@ -94,7 +94,7 @@ func (c *Client) UploadTools(_ context.Context, r io.ReadSeeker, vers version.Bi
9494
return nil, errors.Trace(err)
9595
}
9696

97-
if err := httpClient.Do(c.facade.RawAPICaller().Context(), req, &resp); err != nil {
97+
if err := httpClient.Do(ctx, req, &resp); err != nil {
9898
return nil, errors.Trace(err)
9999
}
100100
return resp.ToolsList, nil

0 commit comments

Comments
 (0)