Skip to content

Commit

Permalink
refactor code to be cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
Zibbp committed Jan 23, 2025
1 parent 0ac361b commit f0ff6c7
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions internal/platform/twitch_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,33 +246,44 @@ func (c *TwitchConnection) twitchMakeHTTPRequest(method, url string, queryParams
rateLimitRemaining := 0
rateLimitReset := time.Time{}

// Parse Ratelimit-Limit
if rateLimitStr := resp.Header.Get("Ratelimit-Limit"); rateLimitStr != "" {
if value, err := strconv.Atoi(rateLimitStr); err == nil {
rateLimit = value
} else {
value, err := strconv.Atoi(rateLimitStr)
if err != nil {
fmt.Printf("Error parsing Ratelimit-Limit: %v\n", err)
} else {
rateLimit = value
}
}

// Parse Ratelimit-Remaining
if rateLimitRemainingStr := resp.Header.Get("Ratelimit-Remaining"); rateLimitRemainingStr != "" {
if value, err := strconv.Atoi(rateLimitRemainingStr); err == nil {
rateLimitRemaining = value
} else {
value, err := strconv.Atoi(rateLimitRemainingStr)
if err != nil {
fmt.Printf("Error parsing Ratelimit-Remaining: %v\n", err)
} else {
rateLimitRemaining = value
}
}
if resp.Header.Get("Ratelimit-Reset") != "" {
unixTimeInt, err := strconv.ParseInt(resp.Header.Get("Ratelimit-Reset"), 10, 64)

// Parse Ratelimit-Reset
if rateLimitResetStr := resp.Header.Get("Ratelimit-Reset"); rateLimitResetStr != "" {
unixTime, err := strconv.ParseInt(rateLimitResetStr, 10, 64)
if err != nil {
return nil, fmt.Errorf("failed to parse ratelimit reset time: %v", err)
return nil, fmt.Errorf("failed to parse Ratelimit-Reset: %v", err)
}
unixTime := time.Unix(unixTimeInt, 0)
rateLimitReset = unixTime
rateLimitReset = time.Unix(unixTime, 0)
}

// Check rate limit usage
if rateLimit > 0 && rateLimitRemaining > 0 {
usagePercentage := float64(rateLimit-rateLimitRemaining) / float64(rateLimit) * 100
if usagePercentage > 75 {
log.Warn().Int("rate_limit", rateLimit).Int("rate_limit_remaining", rateLimitRemaining).Str("rate_limit_expires", rateLimitReset.String()).Msgf("twitch rate limit usage is over 75%% - %d/%d remaining", rateLimitRemaining, rateLimit)
log.Warn().
Int("rate_limit", rateLimit).
Int("rate_limit_remaining", rateLimitRemaining).
Str("rate_limit_expires", rateLimitReset.String()).
Msgf("rate limit usage is over 75%% - %d/%d remaining", rateLimitRemaining, rateLimit)
}
}

Expand Down

0 comments on commit f0ff6c7

Please sign in to comment.