Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing MaxRetries #6

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading