Skip to content

Commit

Permalink
Fixed lint
Browse files Browse the repository at this point in the history
  • Loading branch information
aalur committed Mar 11, 2024
1 parent c3635e5 commit 11beec5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
11 changes: 5 additions & 6 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,11 @@ func (s *SlackClient) PostMessage(request SlackPostMessageRequest, url string, t
return nil
}

func NewApp(queueSize int, httpClient *http.Client, metrics *Metrics, channelOverride, slackPostMessageUrl, slackToken string) *App {
func NewApp(queueSize int, httpClient *http.Client, metrics *Metrics, channelOverride, slackPostMessageURL, slackToken string) *App {
return &App{
slackQueue: make(chan SlackPostMessageRequest, queueSize),
messenger: &SlackClient{client: httpClient},
SlackPostMessageURL: slackPostMessageUrl,
SlackPostMessageURL: slackPostMessageURL,
SlackToken: slackToken,
metrics: metrics,
channelOverride: channelOverride,
Expand All @@ -164,12 +164,11 @@ func (app *App) Shutdown() {
}

//nolint:gocognit // but could probably use a refactor.
func (app *App) processQueue(ctx context.Context, maxRetries int, initialBackoffMs time.Duration, burst int, slackRequestRateMs time.Duration) {
func (app *App) processQueue(ctx context.Context, maxRetries int, initialBackoff time.Duration, burst int, slackRequestRate time.Duration) {
// This is the rate limiter, which will block until it is allowed to continue on r.Wait(ctx).
// I kept the rate at 1 per second, as doing more than that will cause Slack to reject the messages anyways. We can burst however.
// Do note that this is best effort, in case of failures, we will exponentially backoff and retry, which will cause the rate to be lower than 1 per second due to obvious reasons.
// r := rate.NewLimiter(rate.Every(slackRequestRateMs*time.Millisecond), burst)
r := rate.NewLimiter(rate.Every(slackRequestRateMs*time.Millisecond), burst)
r := rate.NewLimiter(rate.Every(slackRequestRate), burst)

for {
select {
Expand Down Expand Up @@ -235,7 +234,7 @@ func (app *App) processQueue(ctx context.Context, maxRetries int, initialBackoff

if retryCount < maxRetries {
retryCount++
backoffDuration := initialBackoffMs * time.Duration(math.Pow(2, float64(retryCount-1))) * time.Millisecond
backoffDuration := initialBackoff * time.Duration(math.Pow(2, float64(retryCount-1)))
time.Sleep(backoffDuration)
} else {
log.S(log.Error, "Message failed after retries", log.Any("err", err), log.Int("retryCount", retryCount))
Expand Down
10 changes: 5 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,20 @@ func getSlackTokens() []string {
func main() {
var (
maxRetries = 2
initialBackoffMs = 1000 * time.Millisecond
initialBackoff = 1000 * time.Millisecond
slackPostMessageURL = "https://slack.com/api/chat.postMessage"
maxQueueSize = 100
burst = 3
metricsPort = ":9090"
applicationPort = ":8080"
channelOverride string
slackRequestRateMs = 1000 * time.Millisecond
slackRequestRate = 1000 * time.Millisecond
)

// Define the flags with the default values // TODO: move the ones that can change to dflag
flag.IntVar(&maxRetries, "maxRetries", maxRetries, "Maximum number of retries for posting a message")
flag.Duration("initialBackoffMs", initialBackoffMs, "Initial backoff in milliseconds for retries")
flag.Duration("slackRequestRateMs", slackRequestRateMs, "Rate limit for slack requests in milliseconds")
flag.Duration("initialBackoffMs", initialBackoff, "Initial backoff in milliseconds for retries")
flag.Duration("slackRequestRateMs", slackRequestRate, "Rate limit for slack requests in milliseconds")
flag.StringVar(&slackPostMessageURL, "slackURL", slackPostMessageURL, "Slack Post Message API URL")
flag.IntVar(&maxQueueSize, "queueSize", maxQueueSize, "Maximum number of messages in the queue")
flag.IntVar(&burst, "burst", burst, "Maximum number of burst to allow")
Expand Down Expand Up @@ -160,7 +160,7 @@ func main() {
defer serverCancel()

log.Infof("Starting main app logic")
go app.processQueue(ctx, maxRetries, initialBackoffMs, burst, slackRequestRateMs)
go app.processQueue(ctx, maxRetries, initialBackoff, burst, slackRequestRate)
log.Infof("Starting receiver server")
// Check error return of app.StartServer in go routine anon function:
go func() {
Expand Down

0 comments on commit 11beec5

Please sign in to comment.