Little code cleanup. (#19)

This commit is contained in:
2024-10-10 10:34:23 +01:00
committed by GitHub
parent d919a1df75
commit 6b31e5c4c0
10 changed files with 138 additions and 112 deletions
+4 -1
View File
@@ -10,6 +10,7 @@ import (
libpack_logger "github.com/lukaszraczylo/graphql-monitoring-proxy/logging"
)
// RateLimitConfig holds the rate limit configuration for a role
type RateLimitConfig struct {
RateCounterTicker *goratecounter.RateCounter
Interval time.Duration `json:"interval"`
@@ -21,6 +22,7 @@ var (
rateLimitMu sync.RWMutex
)
// loadRatelimitConfig loads the rate limit configurations from file
func loadRatelimitConfig() error {
paths := []string{"/go/src/app/ratelimit.json", "./ratelimit.json", "./static/app/default-ratelimit.json"}
for _, path := range paths {
@@ -59,7 +61,7 @@ func loadConfigFromPath(path string) error {
Interval: value.Interval,
})
if cfg.LogLevel == "debug" {
if cfg.LogLevel == "DEBUG" {
cfg.Logger.Debug(&libpack_logger.LogMessage{
Message: "Setting ratelimit config for role",
Pairs: map[string]interface{}{
@@ -83,6 +85,7 @@ func loadConfigFromPath(path string) error {
return nil
}
// rateLimitedRequest checks if a request should be rate-limited
func rateLimitedRequest(userID, userRole string) bool {
rateLimitMu.RLock()
roleConfig, ok := rateLimits[userRole]