Skip to content

Commit

Permalink
Added retry logic to user groups
Browse files Browse the repository at this point in the history
  • Loading branch information
elliot-graebert-skydio committed May 30, 2024
1 parent 908c78c commit 1c412eb
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
36 changes: 33 additions & 3 deletions slack/resource_usergroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,21 @@ func resourceSlackUserGroupRead(ctx context.Context, d *schema.ResourceData, m i
client := m.(*slack.Client)
id := d.Id()
var diags diag.Diagnostics
userGroups, err := client.GetUserGroupsContext(ctx, slack.GetUserGroupsOptionIncludeUsers(true))

operation := func() (interface{}, error) {
return client.GetUserGroupsContext(ctx, slack.GetUserGroupsOptionIncludeUsers(true))
}
result, err := retryOnRateLimit(operation)

if err != nil {
return diag.FromErr(fmt.Errorf("couldn't get usergroups: %w", err))
}

userGroups, ok := result.([]slack.UserGroup)
if !ok {
return diag.FromErr(fmt.Errorf("unexpected result type: %T", result))
}

for _, userGroup := range userGroups {
if userGroup.ID == id {
return updateUserGroupData(d, userGroup)
Expand All @@ -129,11 +139,21 @@ func resourceSlackUserGroupRead(ctx context.Context, d *schema.ResourceData, m i

func findUserGroupByName(ctx context.Context, name string, includeDisabled bool, m interface{}) (slack.UserGroup, error) {
client := m.(*slack.Client)
userGroups, err := client.GetUserGroupsContext(ctx, slack.GetUserGroupsOptionIncludeDisabled(includeDisabled), slack.GetUserGroupsOptionIncludeUsers(true))

operation := func() (interface{}, error) {
return client.GetUserGroupsContext(ctx, slack.GetUserGroupsOptionIncludeDisabled(includeDisabled), slack.GetUserGroupsOptionIncludeUsers(true))
}
result, err := retryOnRateLimit(operation)

if err != nil {
return slack.UserGroup{}, err
}

userGroups, ok := result.([]slack.UserGroup)
if !ok {
return slack.UserGroup{}, fmt.Errorf("unexpected result type: %T", result)
}

for _, userGroup := range userGroups {
if userGroup.Name == name {
return userGroup, nil
Expand All @@ -145,11 +165,21 @@ func findUserGroupByName(ctx context.Context, name string, includeDisabled bool,

func findUserGroupByID(ctx context.Context, id string, includeDisabled bool, m interface{}) (slack.UserGroup, error) {
client := m.(*slack.Client)
userGroups, err := client.GetUserGroupsContext(ctx, slack.GetUserGroupsOptionIncludeDisabled(includeDisabled), slack.GetUserGroupsOptionIncludeUsers(true))

operation := func() (interface{}, error) {
return client.GetUserGroupsContext(ctx, slack.GetUserGroupsOptionIncludeDisabled(includeDisabled), slack.GetUserGroupsOptionIncludeUsers(true))
}
result, err := retryOnRateLimit(operation)

if err != nil {
return slack.UserGroup{}, err
}

userGroups, ok := result.([]slack.UserGroup)
if !ok {
return slack.UserGroup{}, fmt.Errorf("unexpected result type: %T", result)
}

for _, userGroup := range userGroups {
if userGroup.ID == id {
return userGroup, nil
Expand Down
22 changes: 22 additions & 0 deletions slack/retry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package slack

import (
"fmt"
"time"

"github.com/slack-go/slack"
)

func retryOnRateLimit(operation func() (interface{}, error)) (interface{}, error) {
for {
result, err := operation()
if rateLimitedError, ok := err.(*slack.RateLimitedError); ok {
fmt.Printf("Rate limited. Retrying after %v seconds...\n", rateLimitedError.RetryAfter)
time.Sleep(rateLimitedError.RetryAfter)
continue
} else if err != nil {
return nil, err
}
return result, nil
}
}

0 comments on commit 1c412eb

Please sign in to comment.