From 11beec59da540a522b4c4029e8e0eed46ae9a9ab Mon Sep 17 00:00:00 2001 From: aalur Date: Mon, 11 Mar 2024 11:29:05 -0700 Subject: [PATCH] Fixed lint --- app.go | 11 +++++------ main.go | 10 +++++----- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/app.go b/app.go index e420ec5..a625d6c 100644 --- a/app.go +++ b/app.go @@ -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, @@ -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 { @@ -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)) diff --git a/main.go b/main.go index 293490b..372cc3b 100644 --- a/main.go +++ b/main.go @@ -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") @@ -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() {