()
| 562 | } |
| 563 | |
| 564 | func (w *Worker) rateLimitLoop() { |
| 565 | defer w.Stop() |
| 566 | |
| 567 | ticker := time.NewTicker(30 * time.Second) |
| 568 | defer ticker.Stop() |
| 569 | |
| 570 | for { |
| 571 | select { |
| 572 | case <-w.quit: |
| 573 | return |
| 574 | case <-w.ctx.Done(): |
| 575 | slog.DebugContext(w.ctx, "context done") |
| 576 | return |
| 577 | case <-ticker.C: |
| 578 | // update credentials rate limits |
| 579 | for _, creds := range cache.GetAllGithubCredentials() { |
| 580 | rateCli, err := github.NewRateLimitClient(w.ctx, creds) |
| 581 | if err != nil { |
| 582 | slog.With(slog.Any("error", err)).ErrorContext(w.ctx, "failed to create rate limit client") |
| 583 | continue |
| 584 | } |
| 585 | rateLimit, err := rateCli.RateLimit(w.ctx) |
| 586 | if err != nil { |
| 587 | slog.With(slog.Any("error", err)).ErrorContext(w.ctx, "failed to get rate limit") |
| 588 | continue |
| 589 | } |
| 590 | if rateLimit != nil { |
| 591 | core := rateLimit.GetCore() |
| 592 | limit := params.GithubRateLimit{ |
| 593 | Limit: core.Limit, |
| 594 | Used: core.Used, |
| 595 | Remaining: core.Remaining, |
| 596 | Reset: core.Reset.Unix(), |
| 597 | } |
| 598 | cache.SetCredentialsRateLimit(creds.ID, limit) |
| 599 | |
| 600 | // Record Prometheus metrics |
| 601 | credID := fmt.Sprintf("%d", creds.ID) |
| 602 | credName := creds.Name |
| 603 | endpoint := creds.Endpoint.Name |
| 604 | if endpoint == "" { |
| 605 | endpoint = creds.BaseURL |
| 606 | } |
| 607 | |
| 608 | labels := map[string]string{ |
| 609 | "credential_name": credName, |
| 610 | "credential_id": credID, |
| 611 | "endpoint": endpoint, |
| 612 | } |
| 613 | |
| 614 | metrics.GithubRateLimitLimit.With(labels).Set(float64(core.Limit)) |
| 615 | metrics.GithubRateLimitRemaining.With(labels).Set(float64(core.Remaining)) |
| 616 | metrics.GithubRateLimitUsed.With(labels).Set(float64(core.Used)) |
| 617 | metrics.GithubRateLimitResetTimestamp.With(labels).Set(float64(core.Reset.Unix())) |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | } |
no test coverage detected