Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
Handle slack RateLimitedError when posting messages (#241)
Browse files Browse the repository at this point in the history
* Handle slack RateLimitedError when posting messages

Signed-off-by: John Watson <[email protected]>
  • Loading branch information
dctrwatson authored and alexmt committed Apr 16, 2021
1 parent 813137f commit 6200f01
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require (
github.com/stretchr/testify v1.6.1
github.com/technoweenie/multipartstreamer v1.0.1 // indirect
github.com/whilp/git-urls v0.0.0-20191001220047-6db9661140c0
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba
gomodules.xyz/notify v0.1.0
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
k8s.io/api v0.20.4
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -976,8 +976,9 @@ golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxb
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e h1:EHBhcS0mlXEAVwNyO2dLfjToGsyY4j24pTs2ScHnX7s=
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba h1:O8mE0/t419eoIwhTFpKVkHiTs/Igowgfkj25AcZrtiE=
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20180525024113-a5b4c53f6e8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
25 changes: 24 additions & 1 deletion pkg/services/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ import (

log "github.com/sirupsen/logrus"
"github.com/slack-go/slack"
"golang.org/x/time/rate"
)

// No rate limit unless Slack requests it (allows for Slack to control bursting)
var rateLimiter = rate.NewLimiter(rate.Inf, 1)

type SlackNotification struct {
Attachments string `json:"attachments,omitempty"`
Blocks string `json:"blocks,omitempty"`
Expand Down Expand Up @@ -110,7 +114,26 @@ func (s *slackService) Send(notification Notification, dest Destination) error {
msgOptions = append(msgOptions, slack.MsgOptionAttachments(attachments...), slack.MsgOptionBlocks(blocks.BlockSet...))
}

_, _, err := sl.PostMessageContext(context.TODO(), dest.Recipient, msgOptions...)
ctx := context.TODO()
var err error
for {
err = rateLimiter.Wait(ctx)
if err != nil {
break
}
_, _, err = sl.PostMessageContext(ctx, dest.Recipient, msgOptions...)
if err != nil {
if rateLimitedError, ok := err.(*slack.RateLimitedError); ok {
rateLimiter.SetLimit(rate.Every(rateLimitedError.RetryAfter))
} else {
break
}
} else {
// No error, so remove rate limit
rateLimiter.SetLimit(rate.Inf)
break
}
}
return err
}

Expand Down

0 comments on commit 6200f01

Please sign in to comment.