Skip to content

Commit

Permalink
Merge pull request #6 from caseybraithwaite/fixing-max-retries
Browse files Browse the repository at this point in the history
Fixing MaxRetries
  • Loading branch information
itsalix authored Feb 16, 2024
2 parents c9f5a8d + 8f98d07 commit 218fc42
Showing 1 changed file with 17 additions and 25 deletions.
42 changes: 17 additions & 25 deletions src/out/out.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,16 @@ func interpolate(text string) string {

func configure(request *structs.PutInput) (structs.Configuration, error) {
cfg := structs.Configuration{
Channel: request.Source.Channel,
BotToken: request.Source.BotToken,
Debug: request.Source.Debug,
Channel: request.Source.Channel,
BotToken: request.Source.BotToken,
Debug: request.Source.Debug,
MaxRetries: request.Source.MaxRetries,
}

if request.Params.Channel != "" {
cfg.Channel = request.Params.Channel
}

// Default max retries to 1 if not set
if request.Source.MaxRetries == 0 {
cfg.MaxRetries = 1
} else {
cfg.MaxRetries = request.Source.MaxRetries
}

stringBlocks := ""
if request.Params.BlocksFile != "" {
b, err := readFile(request.Params.BlocksFile)
Expand Down Expand Up @@ -117,30 +111,29 @@ func configure(request *structs.PutInput) (structs.Configuration, error) {
}

func sendMessage(api *slack.Client, channel string, maxRetries int, options []slack.MsgOption) (string, error) {
for i := 0; i < maxRetries; i++ {
for i := 0; i <= maxRetries; i++ {
_, timestamp, err := api.PostMessage(
channel,
options...,
)

if err == nil {
return timestamp, nil
} else {
} else if maxRetries == 0 {
return "", err
} else if i != maxRetries {
if rateLimitedError, ok := err.(*slack.RateLimitedError); ok {
if rateLimitedError.Retryable() {
log.Printf("hit rate limit - retrying after %d\n", rateLimitedError.RetryAfter)
time.Sleep(rateLimitedError.RetryAfter)
}
} else if strings.Contains(strings.ToLower(err.Error()), "internal server error") {
log.Println("internal server error - retrying after 3s")
time.Sleep(time.Second * 3)
} else {
return "", err
log.Printf("error whilst sending message to slack: %s - retrying after 3s\n", err)
time.Sleep(time.Second * 3)
}
}
}

return "", fmt.Errorf("couldn't send message - hit max retries")
return "", fmt.Errorf("max retries hit")
}

func updateMessage(api *slack.Client, channel string, timestamp string, maxRetries int, options []slack.MsgOption) (string, error) {
Expand All @@ -153,22 +146,21 @@ func updateMessage(api *slack.Client, channel string, timestamp string, maxRetri

if err == nil {
return timestamp, nil
} else {
} else if maxRetries == 0 {
return "", err
} else if i != maxRetries {
if rateLimitedError, ok := err.(*slack.RateLimitedError); ok {
if rateLimitedError.Retryable() {
log.Printf("hit rate limit - retrying after %d\n", rateLimitedError.RetryAfter)
time.Sleep(rateLimitedError.RetryAfter)
}
} else if strings.Contains(strings.ToLower(err.Error()), "internal server error") {
log.Println("internal server error - retrying after 3s")
time.Sleep(time.Second * 3)
} else {
return "", err
log.Printf("error whilst sending message to slack: %s - retrying after 3s\n", err)
time.Sleep(time.Second * 3)
}
}
}

return "", fmt.Errorf("couldn't update message - hit max retries")
return "", fmt.Errorf("max retries hit")
}

func execute(cfg *structs.Configuration) (string, error) {
Expand Down

0 comments on commit 218fc42

Please sign in to comment.