Skip to content

Commit

Permalink
feat(slack) add rate limiting
Browse files Browse the repository at this point in the history
  • Loading branch information
rkettelerij committed Jun 24, 2024
1 parent d840d00 commit bf30ef9
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion internal/service/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,33 @@ import (

"github.com/PDOK/uptime-operator/internal/model"
"github.com/slack-go/slack"
"golang.org/x/time/rate"
"sigs.k8s.io/controller-runtime/pkg/log"
)

type Slack struct {
webhookURL string
channelID string

rateLimit *rate.Limiter
}

func NewSlack(webhookURL, channelID string) *Slack {
return &Slack{
webhookURL: webhookURL,
channelID: channelID,

// see https://api.slack.com/apis/rate-limits
rateLimit: rate.NewLimiter(1, 10),
}
}

func (s *Slack) Send(ctx context.Context, message string) {
err := slack.PostWebhook(s.webhookURL, &slack.WebhookMessage{
err := s.rateLimit.Wait(ctx)
if err != nil {
log.FromContext(ctx).Error(err, "waiting in order to avoid hitting slack rate limit failed")
}
err = slack.PostWebhook(s.webhookURL, &slack.WebhookMessage{
Channel: s.channelID,
Text: message,
Username: model.OperatorName,
Expand Down

0 comments on commit bf30ef9

Please sign in to comment.