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

Implement rate limit handling for the enterprise API client #12

Merged
merged 5 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions pkg/connector/enterprise_roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
ent "github.com/conductorone/baton-sdk/pkg/types/entitlement"
"github.com/conductorone/baton-sdk/pkg/types/grant"
resources "github.com/conductorone/baton-sdk/pkg/types/resource"

enterprise "github.com/conductorone/baton-slack/pkg/slack"
)

Expand Down
28 changes: 20 additions & 8 deletions pkg/connector/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/conductorone/baton-sdk/pkg/pagination"
"github.com/slack-go/slack"
"google.golang.org/protobuf/types/known/timestamppb"

enterprise "github.com/conductorone/baton-slack/pkg/slack"
)

func parsePageToken(i string, resourceID *v2.ResourceId) (*pagination.Bag, error) {
Expand All @@ -31,14 +33,24 @@ func parsePageToken(i string, resourceID *v2.ResourceId) (*pagination.Bag, error
func annotationsForError(err error) (annotations.Annotations, error) {
annos := annotations.Annotations{}
var rateLimitErr *slack.RateLimitedError
if !errors.As(err, &rateLimitErr) {
return annos, err
if errors.As(err, &rateLimitErr) {
annos.WithRateLimiting(&v2.RateLimitDescription{
Limit: 0,
Remaining: 0,
ResetAt: timestamppb.New(time.Now().Add(rateLimitErr.RetryAfter)),
})
return annos, nil
}

var enterpriseRateLimitErr *enterprise.RateLimitError
if errors.As(err, &enterpriseRateLimitErr) {
annos.WithRateLimiting(&v2.RateLimitDescription{
Limit: 0,
Remaining: 0,
ResetAt: timestamppb.New(time.Now().Add(enterpriseRateLimitErr.RetryAfter)),
})
return annos, nil
}

annos.WithRateLimiting(&v2.RateLimitDescription{
Limit: 0,
Remaining: 0,
ResetAt: timestamppb.New(time.Now().Add(rateLimitErr.RetryAfter)),
})
return annos, nil
return annos, err
}
22 changes: 20 additions & 2 deletions pkg/slack/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"time"

"github.com/slack-go/slack"
)
Expand Down Expand Up @@ -218,9 +220,8 @@ func (c *Client) GetUserGroups(ctx context.Context, teamID string) ([]slack.User
}

var res struct {
Ok bool `json:"ok"`
BaseResponse
UserGroups []slack.UserGroup `json:"usergroups"`
Error string `json:"error"`
}

err = c.doRequest(ctx, userGroupsUrl, &res, http.MethodPost, nil, values)
Expand All @@ -235,6 +236,14 @@ func (c *Client) GetUserGroups(ctx context.Context, teamID string) ([]slack.User
return res.UserGroups, nil
}

type RateLimitError struct {
RetryAfter time.Duration
}

func (r *RateLimitError) Error() string {
return fmt.Sprintf("rate limited, retry after: %s", r.RetryAfter.String())
}

func (c *Client) doRequest(ctx context.Context, url string, res interface{}, method string, payload []byte, values url.Values) error {
reqBody := strings.NewReader(values.Encode())

Expand All @@ -256,5 +265,14 @@ func (c *Client) doRequest(ctx context.Context, url string, res interface{}, met
return err
}

if resp.StatusCode == http.StatusTooManyRequests {
retryAfter := resp.Header.Get("Retry-After")
retryAfterSec, err := strconv.Atoi(retryAfter)
if err != nil {
return fmt.Errorf("error parsing retry after header: %w", err)
}
return &RateLimitError{RetryAfter: time.Second * time.Duration(retryAfterSec)}
}

return nil
}
Loading