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

Commit 157603b

Browse files
authored
Handle slack RateLimitedError when posting messages (#241)
* Handle slack RateLimitedError when posting messages Signed-off-by: John Watson <[email protected]>
1 parent d2d2b35 commit 157603b

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ require (
2828
github.com/stretchr/testify v1.6.1
2929
github.com/technoweenie/multipartstreamer v1.0.1 // indirect
3030
github.com/whilp/git-urls v0.0.0-20191001220047-6db9661140c0
31+
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba
3132
gomodules.xyz/notify v0.1.0
3233
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
3334
k8s.io/api v0.20.4

go.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -976,8 +976,9 @@ golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxb
976976
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
977977
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
978978
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
979-
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e h1:EHBhcS0mlXEAVwNyO2dLfjToGsyY4j24pTs2ScHnX7s=
980979
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
980+
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba h1:O8mE0/t419eoIwhTFpKVkHiTs/Igowgfkj25AcZrtiE=
981+
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
981982
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
982983
golang.org/x/tools v0.0.0-20180525024113-a5b4c53f6e8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
983984
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

pkg/services/slack.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ import (
1414

1515
log "github.com/sirupsen/logrus"
1616
"github.com/slack-go/slack"
17+
"golang.org/x/time/rate"
1718
)
1819

20+
// No rate limit unless Slack requests it (allows for Slack to control bursting)
21+
var rateLimiter = rate.NewLimiter(rate.Inf, 1)
22+
1923
type SlackNotification struct {
2024
Attachments string `json:"attachments,omitempty"`
2125
Blocks string `json:"blocks,omitempty"`
@@ -110,7 +114,26 @@ func (s *slackService) Send(notification Notification, dest Destination) error {
110114
msgOptions = append(msgOptions, slack.MsgOptionAttachments(attachments...), slack.MsgOptionBlocks(blocks.BlockSet...))
111115
}
112116

113-
_, _, err := sl.PostMessageContext(context.TODO(), dest.Recipient, msgOptions...)
117+
ctx := context.TODO()
118+
var err error
119+
for {
120+
err = rateLimiter.Wait(ctx)
121+
if err != nil {
122+
break
123+
}
124+
_, _, err = sl.PostMessageContext(ctx, dest.Recipient, msgOptions...)
125+
if err != nil {
126+
if rateLimitedError, ok := err.(*slack.RateLimitedError); ok {
127+
rateLimiter.SetLimit(rate.Every(rateLimitedError.RetryAfter))
128+
} else {
129+
break
130+
}
131+
} else {
132+
// No error, so remove rate limit
133+
rateLimiter.SetLimit(rate.Inf)
134+
break
135+
}
136+
}
114137
return err
115138
}
116139

0 commit comments

Comments
 (0)