Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion internal/ghclient/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import (

ghct "github.com/bored-engineer/github-conditional-http-transport"
ratelimit "github.com/gofri/go-github-ratelimit/v2/github_ratelimit"
ratelimitp "github.com/gofri/go-github-ratelimit/v2/github_ratelimit/github_primary_ratelimit"
ratelimits "github.com/gofri/go-github-ratelimit/v2/github_ratelimit/github_secondary_ratelimit"
"github.com/hashicorp/go-retryablehttp"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
"golang.org/x/oauth2"
)
Expand Down Expand Up @@ -65,7 +68,17 @@ func newTransport(tokenSource oauth2.TokenSource, opts Options) (http.RoundTripp
}

// Wrap with rate limit transport
tr = ratelimit.New(tr)
tr = ratelimit.New(tr, ratelimitp.WithLimitDetectedCallback(primaryRateLimitCallback), ratelimits.WithLimitDetectedCallback(secondaryRateLimitCallback))

return tr, nil
}

// primaryRateLimitCallback is a callback function that is called when the GitHub API primary rate limit is detected. It logs a warning message with the category of the rate limit and the reset time.
func primaryRateLimitCallback(cb *ratelimitp.CallbackContext) {
tflog.Warn(cb.Request.Context(), "GitHub API primary rate limit detected.", map[string]any{"category": cb.Category, "reset_time": cb.ResetTime})
}

// secondaryRateLimitCallback is a callback function that is called when the GitHub API secondary rate limit is detected. It logs a warning message with the reset time.
func secondaryRateLimitCallback(cb *ratelimits.CallbackContext) {
tflog.Warn(cb.Request.Context(), "GitHub API secondary rate limit detected.", map[string]any{"reset_time": cb.ResetTime})
}