mirror of
https://github.com/lukaszraczylo/graphql-monitoring-proxy.git
synced 2026-10-22 06:38:13 +00:00
fixup! fixup! Update go.mod and go.sum
This commit is contained in:
+57
-54
@@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/goccy/go-json"
|
"github.com/goccy/go-json"
|
||||||
@@ -16,58 +17,61 @@ type RateLimitConfig struct {
|
|||||||
Req int `json:"req"`
|
Req int `json:"req"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var rateLimits map[string]RateLimitConfig
|
var (
|
||||||
var ratelimit_intervals = map[string]time.Duration{
|
rateLimits map[string]RateLimitConfig
|
||||||
"milli": time.Millisecond,
|
ratelimitIntervals = map[string]time.Duration{
|
||||||
"micro": time.Microsecond,
|
"milli": time.Millisecond,
|
||||||
"nano": time.Nanosecond,
|
"micro": time.Microsecond,
|
||||||
"second": time.Second,
|
"nano": time.Nanosecond,
|
||||||
"minute": time.Minute,
|
"second": time.Second,
|
||||||
"hour": time.Hour,
|
"minute": time.Minute,
|
||||||
"day": time.Hour * 24,
|
"hour": time.Hour,
|
||||||
}
|
"day": 24 * time.Hour,
|
||||||
|
}
|
||||||
|
configPaths = []string{"/go/src/app/ratelimit.json", "./ratelimit.json", "./static/app/default-ratelimit.json"}
|
||||||
|
mu sync.RWMutex
|
||||||
|
)
|
||||||
|
|
||||||
func loadRatelimitConfig() error {
|
func loadRatelimitConfig() error {
|
||||||
paths := []string{"/go/src/app/ratelimit.json", "./ratelimit.json", "./static/app/default-ratelimit.json"}
|
for _, path := range configPaths {
|
||||||
|
if err := loadConfigFromPath(path); err == nil {
|
||||||
for _, path := range paths {
|
cfg.Logger.Debug(&libpack_logger.LogMessage{
|
||||||
err := loadConfigFromPath(path)
|
Message: "Failed to load config",
|
||||||
if err == nil {
|
Pairs: map[string]interface{}{"path": path, "error": err},
|
||||||
|
})
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
cfg.Logger.Debug(&libpack_logger.LogMessage{
|
|
||||||
Message: "Failed to load config",
|
|
||||||
Pairs: map[string]interface{}{"path": path, "error": err},
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg.Logger.Error(&libpack_logger.LogMessage{
|
cfg.Logger.Error(&libpack_logger.LogMessage{
|
||||||
Message: "Rate limit config not found",
|
Message: "Rate limit config not found",
|
||||||
Pairs: map[string]interface{}{"paths": paths},
|
Pairs: map[string]interface{}{"paths": configPaths},
|
||||||
})
|
})
|
||||||
|
|
||||||
return os.ErrNotExist
|
return os.ErrNotExist
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadConfigFromPath(path string) error {
|
func loadConfigFromPath(path string) error {
|
||||||
file, err := os.Open(path)
|
file, err := os.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
config := struct {
|
var config struct {
|
||||||
RateLimit map[string]RateLimitConfig `json:"ratelimit"`
|
RateLimit map[string]RateLimitConfig `json:"ratelimit"`
|
||||||
}{}
|
}
|
||||||
|
|
||||||
decoder := json.NewDecoder(file)
|
if err := json.Unmarshal(file, &config); err != nil {
|
||||||
if err := decoder.Decode(&config); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mu.Lock()
|
||||||
|
defer mu.Unlock()
|
||||||
|
|
||||||
|
rateLimits = make(map[string]RateLimitConfig, len(config.RateLimit))
|
||||||
for key, value := range config.RateLimit {
|
for key, value := range config.RateLimit {
|
||||||
value.RateCounterTicker = goratecounter.NewRateCounter().WithConfig(goratecounter.RateCounterConfig{
|
value.RateCounterTicker = goratecounter.NewRateCounter().WithConfig(goratecounter.RateCounterConfig{
|
||||||
Interval: time.Duration(value.Req) * ratelimit_intervals[value.Interval],
|
Interval: time.Duration(value.Req) * ratelimitIntervals[value.Interval],
|
||||||
})
|
})
|
||||||
|
|
||||||
if cfg.LogLevel == "debug" {
|
if cfg.LogLevel == "debug" {
|
||||||
@@ -76,15 +80,14 @@ func loadConfigFromPath(path string) error {
|
|||||||
Pairs: map[string]interface{}{
|
Pairs: map[string]interface{}{
|
||||||
"role": key,
|
"role": key,
|
||||||
"interval_provided": value.Interval,
|
"interval_provided": value.Interval,
|
||||||
"interval_used": ratelimit_intervals[value.Interval],
|
"interval_used": ratelimitIntervals[value.Interval],
|
||||||
"ratelimit": value.Req,
|
"ratelimit": value.Req,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
config.RateLimit[key] = value
|
rateLimits[key] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
rateLimits = config.RateLimit
|
|
||||||
cfg.Logger.Debug(&libpack_logger.LogMessage{
|
cfg.Logger.Debug(&libpack_logger.LogMessage{
|
||||||
Message: "Rate limit config loaded",
|
Message: "Rate limit config loaded",
|
||||||
Pairs: map[string]interface{}{"ratelimit": rateLimits},
|
Pairs: map[string]interface{}{"ratelimit": rateLimits},
|
||||||
@@ -92,7 +95,10 @@ func loadConfigFromPath(path string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func rateLimitedRequest(userID string, userRole string) (shouldAllow bool) {
|
func rateLimitedRequest(userID, userRole string) bool {
|
||||||
|
mu.RLock()
|
||||||
|
defer mu.RUnlock()
|
||||||
|
|
||||||
if rateLimits == nil {
|
if rateLimits == nil {
|
||||||
cfg.Logger.Debug(&libpack_logger.LogMessage{
|
cfg.Logger.Debug(&libpack_logger.LogMessage{
|
||||||
Message: "Rate limit config not found",
|
Message: "Rate limit config not found",
|
||||||
@@ -101,19 +107,10 @@ func rateLimitedRequest(userID string, userRole string) (shouldAllow bool) {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch role config once to avoid multiple map lookups
|
|
||||||
roleConfig, ok := rateLimits[userRole]
|
roleConfig, ok := rateLimits[userRole]
|
||||||
if !ok {
|
if !ok || roleConfig.RateCounterTicker == nil {
|
||||||
cfg.Logger.Debug(&libpack_logger.LogMessage{
|
cfg.Logger.Debug(&libpack_logger.LogMessage{
|
||||||
Message: "Rate limit role not found",
|
Message: "Rate limit role or ticker not found",
|
||||||
Pairs: map[string]interface{}{"user_role": userRole},
|
|
||||||
})
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
if roleConfig.RateCounterTicker == nil {
|
|
||||||
cfg.Logger.Debug(&libpack_logger.LogMessage{
|
|
||||||
Message: "Rate limit ticker not found",
|
|
||||||
Pairs: map[string]interface{}{"user_role": userRole},
|
Pairs: map[string]interface{}{"user_role": userRole},
|
||||||
})
|
})
|
||||||
return true
|
return true
|
||||||
@@ -122,23 +119,29 @@ func rateLimitedRequest(userID string, userRole string) (shouldAllow bool) {
|
|||||||
roleConfig.RateCounterTicker.Incr(1)
|
roleConfig.RateCounterTicker.Incr(1)
|
||||||
tickerRate := roleConfig.RateCounterTicker.GetRate()
|
tickerRate := roleConfig.RateCounterTicker.GetRate()
|
||||||
|
|
||||||
logDetails := map[string]interface{}{
|
if cfg.LogLevel == "debug" {
|
||||||
"user_role": userRole,
|
cfg.Logger.Debug(&libpack_logger.LogMessage{
|
||||||
"user_id": userID,
|
Message: "Rate limit ticker",
|
||||||
"rate": tickerRate,
|
Pairs: map[string]interface{}{
|
||||||
"config_rate": roleConfig.Req,
|
"user_role": userRole,
|
||||||
"interval": roleConfig.Interval,
|
"user_id": userID,
|
||||||
|
"rate": tickerRate,
|
||||||
|
"config_rate": roleConfig.Req,
|
||||||
|
"interval": roleConfig.Interval,
|
||||||
|
},
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg.Logger.Debug(&libpack_logger.LogMessage{
|
|
||||||
Message: "Rate limit ticker",
|
|
||||||
Pairs: map[string]interface{}{"log_details": logDetails},
|
|
||||||
})
|
|
||||||
|
|
||||||
if tickerRate > float64(roleConfig.Req) {
|
if tickerRate > float64(roleConfig.Req) {
|
||||||
cfg.Logger.Debug(&libpack_logger.LogMessage{
|
cfg.Logger.Debug(&libpack_logger.LogMessage{
|
||||||
Message: "Rate limit exceeded",
|
Message: "Rate limit exceeded",
|
||||||
Pairs: map[string]interface{}{"log_details": logDetails},
|
Pairs: map[string]interface{}{
|
||||||
|
"user_role": userRole,
|
||||||
|
"user_id": userID,
|
||||||
|
"rate": tickerRate,
|
||||||
|
"config_rate": roleConfig.Req,
|
||||||
|
"interval": roleConfig.Interval,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user