Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Context abstractions around API and the state cache #15

Closed
diamondburned opened this issue Apr 24, 2020 · 0 comments
Closed

Context abstractions around API and the state cache #15

diamondburned opened this issue Apr 24, 2020 · 0 comments

Comments

@diamondburned
Copy link
Owner

Allow copying the API struct with a non-nil context, which would allow people to inject a Context in.

Example:

ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

var err error

api := session.API.WithContext(ctx)
err = api.Guild(123123) // 500ms each request
err = api.Guild(123123)
err = api.Guild(123123)
err = api.Guild(123123) // cumulative: 2 seconds

if err != nil {
	log.Fatalln("Failed to fetch guild:", err) // context expires, errors
}

Since the state also has an API abstraction, it could also have a similar method:

ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

cstate := state.WithContext(ctx)
cstate.Guild() // will use context with copied API instance.
@diamondburned diamondburned changed the title API abstractions around Context and the state cache Context abstractions around API and the state cache May 3, 2020
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

No branches or pull requests

1 participant