generated from hashicorp/terraform-provider-scaffolding-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from Essent/feat/retry-rate-limit
feat: retry rate limit
- Loading branch information
Showing
7 changed files
with
116 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package slackExt | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/slack-go/slack" | ||
) | ||
|
||
type Client interface { | ||
GetUserInfo(ctx context.Context, user string) (*slack.User, error) | ||
GetUserByEmail(ctx context.Context, email string) (*slack.User, error) | ||
GetUsersContext(ctx context.Context) ([]slack.User, error) | ||
GetUserGroups(ctx context.Context, options ...slack.GetUserGroupsOption) ([]slack.UserGroup, error) | ||
} | ||
|
||
func New(base *slack.Client) Client { | ||
return &clientRateLimit{&clientImpl{base}} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package slackExt | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/slack-go/slack" | ||
) | ||
|
||
type clientImpl struct { | ||
base *slack.Client | ||
} | ||
|
||
func (c *clientImpl) GetUserInfo(ctx context.Context, user string) (*slack.User, error) { | ||
return c.base.GetUserInfoContext(ctx, user) | ||
} | ||
|
||
func (c *clientImpl) GetUserByEmail(ctx context.Context, email string) (*slack.User, error) { | ||
return c.base.GetUserByEmailContext(ctx, email) | ||
} | ||
|
||
func (c *clientImpl) GetUsersContext(ctx context.Context) ([]slack.User, error) { | ||
return c.base.GetUsersContext(ctx) | ||
} | ||
|
||
func (c *clientImpl) GetUserGroups(ctx context.Context, options ...slack.GetUserGroupsOption) ([]slack.UserGroup, error) { | ||
return c.base.GetUserGroupsContext(ctx, options...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package slackExt | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/slack-go/slack" | ||
) | ||
|
||
type clientRateLimit struct { | ||
base Client | ||
} | ||
|
||
func rateLimit[R any](ctx context.Context, f func() (R, error), getZeroValue func() R) (result R, err error) { | ||
for { | ||
result, err = f() | ||
|
||
if err == nil { | ||
return result, nil | ||
} | ||
|
||
if rateLimitedError, ok := err.(*slack.RateLimitedError); ok { | ||
select { | ||
case <-time.After(rateLimitedError.RetryAfter): | ||
case <-ctx.Done(): | ||
return getZeroValue(), ctx.Err() | ||
} | ||
} else { | ||
return getZeroValue(), err | ||
} | ||
} | ||
} | ||
|
||
func (c *clientRateLimit) GetUserInfo(ctx context.Context, user string) (result *slack.User, err error) { | ||
return rateLimit(ctx, func() (*slack.User, error) { | ||
return c.base.GetUserInfo(ctx, user) | ||
}, func() *slack.User { return nil }) | ||
} | ||
|
||
func (c *clientRateLimit) GetUserByEmail(ctx context.Context, email string) (*slack.User, error) { | ||
return rateLimit(ctx, func() (*slack.User, error) { | ||
return c.base.GetUserByEmail(ctx, email) | ||
}, func() *slack.User { return nil }) | ||
} | ||
|
||
func (c *clientRateLimit) GetUsersContext(ctx context.Context) ([]slack.User, error) { | ||
return c.base.GetUsersContext(ctx) | ||
} | ||
|
||
func (c *clientRateLimit) GetUserGroups(ctx context.Context, options ...slack.GetUserGroupsOption) ([]slack.UserGroup, error) { | ||
return rateLimit(ctx, func() ([]slack.UserGroup, error) { | ||
return c.base.GetUserGroups(ctx, options...) | ||
}, func() []slack.UserGroup { return []slack.UserGroup{} }) | ||
} |