Skip to content

service_client: Accept per-call Context - #2274

Closed
pierreprinetti wants to merge 1 commit into
gophercloud:masterfrom
shiftstack:client_context
Closed

pierreprinetti wants to merge 1 commit into
gophercloud:masterfrom
shiftstack:client_context

Conversation

@pierreprinetti

@pierreprinetti pierreprinetti commented Nov 11, 2021 •

Copy link
Copy Markdown
Member

Adds the ability to pass per-call context.Context.
Adds a WithContext method to the ServiceClient, which will return a new
ServiceClient.

The context added to ServiceClient will be used in addition to the
ProviderClient's Context to make HTTP calls.

Example use:

	ctx := context.Background()
	allPages, err := servers.List(client.WithContext(ctx), servers.ListOpts{}).AllPages()

Most of the complexity of this patch is a consequence of the choice to merge the ServiceClient context with the ProviderClient's, instead of just overwriting it. This is another choice that is up for debate.


Alternative B:

	ctx := context.Background()
	allPages, err := servers.List(client, servers.ListOpts{Context: ctx}).AllPages()

However I see one issue with this alternative: not all verbs currently accept an "options". See for example servers.Get(client *gophercloud.ServiceClient, id string). Such a change would not then be backwards-compatible.


Alternative C:

Duplicate every user-facing function with a WithContext version


Alternative D:

Wait until the next Major release to change every single user-facing function to accept a Context as the first argument.


Any more ideas?

For #1987

@coveralls

coveralls commented Nov 11, 2021 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.06%) to 79.918% when pulling 13da401 on shiftstack:client_context into abb45fc on gophercloud:master.

@theopenlab-ci

theopenlab-ci Bot commented Nov 11, 2021

Copy link
Copy Markdown

Build failed.

@theopenlab-ci

theopenlab-ci Bot commented Nov 11, 2021

Copy link
Copy Markdown

Build failed.

@pierreprinetti
pierreprinetti force-pushed the client_context branch 2 times, most recently from 5733344 to 2ec28d7 Compare November 12, 2021 08:29
@theopenlab-ci

theopenlab-ci Bot commented Nov 12, 2021

Copy link
Copy Markdown

Build failed.

@pierreprinetti
pierreprinetti force-pushed the client_context branch 3 times, most recently from 9d175d0 to 927c51c Compare November 12, 2021 11:12
Adds the ability to pass per-call context.Context.
Adds a WithContext method to the ServiceClient, which will return a new
ServiceClient.

The context added to ServiceClient will be used in addition to the
ProviderClient's Context to make HTTP calls.

Example use:

```Go
	ctx := context.Background()
	allPages, err := servers.List(client.WithContext(ctx), servers.ListOpts{}).AllPages()

	// the original `client` is untouched
```
@theopenlab-ci

theopenlab-ci Bot commented Nov 12, 2021

Copy link
Copy Markdown

Build failed.

@pierreprinetti

Copy link
Copy Markdown
Member Author

The test failures look related; I'm investigating those next week. I am not merging anything anyway until there's some buy-in on the general idea of adding a context at the service level.

@zhsj

zhsj commented Aug 18, 2022

Copy link
Copy Markdown
Contributor

Hi, @pierreprinetti is this still on your radar? I'm still interested in this feature.

@pierreprinetti

Copy link
Copy Markdown
Member Author

Hi, @pierreprinetti is this still on your radar? I'm still interested in this feature.

Yes it is! We definitely want to land support for call-scoped context, however we will probably rather opt for a non-breaking solution like new XxxxWithContext functions

@pierreprinetti

Copy link
Copy Markdown
Member Author

I now believe that this PR is not the way to go.

The ProviderClient's context provides cancellation for the lifespan of authentication. What we need is the ability to cancel a single call independently. There is no need to merge the two contexts.

The next proposal should focus on adding XxxWithContext functions that just pass their ctx to the http.Request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants