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

Primary and secondary rate-limit handling #52

Merged
merged 41 commits into from
Apr 10, 2024
Merged

Conversation

kfcampbell
Copy link
Member

@kfcampbell kfcampbell commented Mar 5, 2024

Note that parts of testing code were taken from our colleagues at Kiota (as noted here) and parts of implementation code were taken from the google/go-github library (as noted here). I'm grateful to those teams for doing excellent work before us!

This PR adds a rate limit handler to the go-sdk for primary and secondary rate limits. When it detects such a rate limit, it sleeps until the limit has expired, then retries the request. This has been validated using a local GitHub test environment in addition to unit test coverage. In the future, it would be cool to build better retry logic that builds in exponential backoff, jitter, throttling, etc.

It also greatly simplifies the abstractions for API client instantiation. The values I focused on were:

  • Minimal chaining of objects (I was annoyed at constructing a handler, to pass to an array of middleware, to pass to an an HTTP client, to pass to an adapter, to pass to an API client)
  • Functional initialization pattern
  • Sensible defaults when not specified

The current pattern looks like this:

client, err := pkg.NewApiClient(
	pkg.WithUserAgent("my-user-agent"),
	pkg.WithRequestTimeout(5*time.Second),
	pkg.WithBaseUrl("https://api.github.com"),
	pkg.WithAuthorizationToken(os.Getenv("GITHUB_TOKEN")),
)

// equally valid:
//client, err := pkg.NewApiClient()
if err != nil {
	log.Fatalf("error creating client: %v", err)
}

Copy link

github-actions bot commented Mar 5, 2024

👋 Hi! Thank you for this contribution! Just to let you know, our GitHub SDK team does a round of issue and PR reviews twice a week, every Monday and Friday! We have a process in place for prioritizing and responding to your input. Because you are a part of this community please feel free to comment, add to, or pick up any issues/PRs that are labled with Status: Up for grabs. You & others like you are the reason all of this works! So thank you & happy coding! 🚀

Copy link

what-the-diff bot commented Mar 5, 2024

PR Summary

  • Renaming and Updates to the Authentication Test File
    The pkg/authentication/example_test.go file underwent several modifications, including the renaming of the "github.com/microsoft/kiota-http-go" import package to "kiotaHttp". Additionally, the function ExampleApiClient_Octocat was also updated with fresh code. Moreover, two new functions, Example_ResponseHandlerForRateLimiting and Example_HttpClientForRateLimiting, have been integrated into the system to handle functionalities lying under rate limiting.

  • Addition of New Transport File
    A fresh file titled pkg/transport/transport.go has been integrated into the system. This file contains new code related to rate limiting in HTTP requests, showing our efforts to maintain consistent service regardless of traffic patterns or spikes.

pkg/headers/header_contents.go Show resolved Hide resolved
cmd/rate-limit-test/main.go Outdated Show resolved Hide resolved
cmd/rate-limit-test/main.go Outdated Show resolved Hide resolved
@kfcampbell kfcampbell changed the title Rate limit playground Primary and secondary rate-limit handling Apr 2, 2024
@kfcampbell kfcampbell marked this pull request as ready for review April 2, 2024 22:28
Copy link
Collaborator

@nickfloyd nickfloyd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! ➕ For setting up the infrastructure for the local testing ground as well!

@kfcampbell kfcampbell merged commit 1b3a761 into main Apr 10, 2024
4 checks passed
@kfcampbell kfcampbell deleted the rate-limit-playground branch April 10, 2024 22:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants