Skip to content

PollToken should cancel early if the context is canceled #6

Closed

Description

@tomlazar

This is kind of a nice issue, but in a console app I am working on the app intercepts the os SIGKILL/SIGINT signals to do all of its own graceful shutdown via context, but currently there is no way to cancel the loop in PollToken.

To make this cancelable, it would be nice to something like this into the loop there.

		select {
		case <-ctx.Done():
			return nil, ErrCanceled
		default:
			timeSleep(checkInterval)
		}

Then the function could be extended like a lot of packages with a second copy of the method, and context propagated down.

func PollTokenContext(ctx context.Context, c httpClient, pollURL string, clientID string, code *CodeResponse) (*api.AccessToken, error) {
... current implementation
}

func PollToken(c httpClient, pollURL string, clientID string, code *CodeResponse) (*api.AccessToken, error) {
  return PollTokenContext(context.Background(), c, pollURL, clientID, code)
}

The same thing could be done to DetectFlow, or it could be embbed within the flow struct so the outer api layout would change less.

Activity

  1. mislav commented on Mar 30, 2021

    @mislav
    Contributor

    That's an excellent suggestion. Thank you!

  2. tomlazar commented on Mar 30, 2021

    @tomlazar
    Author

    I'd love to contribute a PR for this would you like the context to go all the way down from detect flow or just be embedded in that struct?

  3. mislav commented on Apr 12, 2021

    @mislav
    Contributor

    @tomlazar I think that Context should be generally accepted as argument to separate methods, like you proposed, but for the users of the Flow struct, maybe context should also be an additional field on that struct? I'm not sure if adding it to a struct goes against some conventions in how contexts are used in Go. I don't have a lot of experience with them.

    You are very welcome to propose a change!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions