-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #127 from whywaita/feat/metrics-rate-limit
Add rate limit metrics
- Loading branch information
Showing
8 changed files
with
127 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package gh | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/google/go-github/v35/github" | ||
) | ||
|
||
func storeRateLimit(scope string, rateLimit github.Rate) { | ||
if rateLimit.Reset.IsZero() { | ||
// Not configure rate limit, don't need to store (e.g. GHES) | ||
return | ||
} | ||
|
||
rateLimitLimit.Store(scope, rateLimit.Limit) | ||
rateLimitRemain.Store(scope, rateLimit.Remaining) | ||
} | ||
|
||
func getRateLimitKey(org, repo string) string { | ||
if repo == "" { | ||
return org | ||
} | ||
return fmt.Sprintf("%s/%s", org, repo) | ||
} | ||
|
||
// GetRateLimitRemain get a list of rate limit remaining | ||
// key: scope, value: remain | ||
func GetRateLimitRemain() map[string]int { | ||
m := map[string]int{} | ||
mu := sync.Mutex{} | ||
|
||
rateLimitRemain.Range(func(key, value interface{}) bool { | ||
k, ok := key.(string) | ||
if !ok { | ||
return false | ||
} | ||
v, ok := value.(int) | ||
if !ok { | ||
return false | ||
} | ||
|
||
mu.Lock() | ||
m[k] = v | ||
mu.Unlock() | ||
return true | ||
}) | ||
|
||
return m | ||
} | ||
|
||
// GetRateLimitLimit get a list of rate limit | ||
// key: scope, value: remain | ||
func GetRateLimitLimit() map[string]int { | ||
m := map[string]int{} | ||
mu := sync.Mutex{} | ||
|
||
rateLimitLimit.Range(func(key, value interface{}) bool { | ||
k, ok := key.(string) | ||
if !ok { | ||
return false | ||
} | ||
v, ok := value.(int) | ||
if !ok { | ||
return false | ||
} | ||
|
||
mu.Lock() | ||
m[k] = v | ||
mu.Unlock() | ||
return true | ||
}) | ||
|
||
return m | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters