Skip to content

Conversation

robertolosanno-e2x
Copy link
Contributor

This PR implements rate limiting and automatic retry configuration for the Contentstack Terraform provider to address API rate limit issues that cause terraform operations to fail with 429 errors.

Changes implemented:

  1. Provider Rate Limiting Configuration: Added three new optional provider attributes:

    • rate_limit (float64): Maximum requests per second (defaults to 10.0 to comply with Contentstack API limits)
    • rate_burst (int): Maximum burst size for short bursts above the rate limit (defaults to 10)
    • max_retries (int): Maximum retry attempts for 429 responses with exponential backoff (defaults to 3)
  2. Integration with Enhanced SDK: Updated go.mod to use the enhanced contentstack-go-sdk that includes:

    • Token bucket rate limiting using golang.org/x/time/rate
    • Automatic retry logic with exponential backoff (1s, 2s, 4s, 8s, 16s, capped at 30s)
    • Respect for server Retry-After headers
    • Context-aware retry handling
  3. Documentation Updates: Auto-generated provider documentation now includes all new rate limiting attributes with detailed descriptions.

  4. Example Configurations: Added simplified examples demonstrating:

    • Basic provider configuration (examples/provider/main.tf)
    • Rate limiting configuration (examples/rate-limiting/main.tf)

Rationale:
Contentstack's Management API has a 10 requests/second rate limit. When Terraform makes concurrent requests (during plan/apply operations with multiple resources), it frequently hits 429 errors causing operations to fail. This is particularly problematic for users managing large numbers of content types and global fields (~35+ resources as mentioned in the related issue).

The solution provides configurable rate limiting that prevents hitting API limits, plus automatic retry logic that gracefully handles any 429 responses that do occur, making Terraform operations reliable and robust.

Fixes #3

Related: contentstack-go-sdk#16

NEW FEATURES | UPGRADE NOTES | ENHANCEMENTS | BUG FIXES | EXPERIMENTS

  • NEW FEATURE: Added rate_limit provider attribute to configure API request throttling
  • NEW FEATURE: Added rate_burst provider attribute to allow controlled request bursts
  • NEW FEATURE: Added max_retries provider attribute to configure automatic retry attempts for 429 responses
  • ENHANCEMENT: Integrated with enhanced contentstack-go-sdk featuring automatic exponential backoff retry logic
  • BUG FIX: Resolved 429 "Too Many Requests" errors that caused Terraform operations to fail
  • ENHANCEMENT: Added comprehensive provider documentation for all new rate limiting features
  • NEW FEATURE: Added simplified example configurations demonstrating rate limiting usage
  • ENHANCEMENT: Updated dependency to contentstack-go-sdk with built-in rate limiting and retry capabilities

- Add rate_limit, rate_burst, and max_retries provider attributes
- Update provider schema to support new rate limiting options
- Add automatic retry logic integration with contentstack-go-sdk
- Update provider documentation with new attributes
- Add simplified examples for provider and rate-limiting configurations
- Update go.mod to use enhanced contentstack-go-sdk with rate limiting

This enables reliable Terraform operations by automatically handling
Contentstack API rate limits (10 req/sec) with configurable throttling
and exponential backoff retry logic for 429 responses.

Related: contentstack-go-sdk rate limiting implementation
- Add retryableRoundTripper for seamless SDK integration
- Implement retry policy for 429 responses only
- Add max_retries, retry_wait_min, retry_wait_max provider attributes
- Update examples and documentation
- Use exponential backoff with configurable wait times
- Maintain compatibility with existing rate limiting
@robertolosanno-e2x
Copy link
Contributor Author

Hi @demeyerthom ,
I was wondering if you have a minute to have a look at this PR.
Thanks

Copy link
Member

@demeyerthom demeyerthom left a comment

Choose a reason for hiding this comment

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

Overall looks good! I left 2 comments to fix.

Can you also add a changie file for the changelog? It triggers our deployment processes:

BaseURL: config.BaseURL.Value,
AuthToken: config.AuthToken.Value,
HTTPClient: httpClient,
RateLimit: config.RateLimit.Value,
Copy link
Member

Choose a reason for hiding this comment

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

I think those fields are not part of the config are they?

@@ -0,0 +1,33 @@
terraform {
Copy link
Member

Choose a reason for hiding this comment

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

This bit will not show in the docs. You want to add it to examples/provider/main.tf, and then run go generate to generate the correct docs for it to show

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.

add property to configure rate limit
2 participants