Add concurrency limits and configuration options for SteamCache

- Introduced maxConcurrentRequests and maxRequestsPerClient fields in the Config struct to manage request limits.
- Updated the SteamCache implementation to utilize these new configuration options for controlling concurrent requests.
- Enhanced the ServeHTTP method to enforce global and per-client rate limiting using semaphores.
- Modified the root command to accept new flags for configuring concurrency limits via command-line arguments.
- Updated tests to reflect changes in the SteamCache initialization and request handling logic.
This commit is contained in:
2025-09-02 06:50:42 -05:00
parent 7fb1fcf21f
commit 9ca8fa4a5e
6 changed files with 203 additions and 29 deletions

View File

@@ -19,6 +19,9 @@ var (
logLevel string
logFormat string
maxConcurrentRequests int64
maxRequestsPerClient int64
)
var rootCmd = &cobra.Command{
@@ -94,6 +97,17 @@ var rootCmd = &cobra.Command{
Str("config_path", configPath).
Msg("Configuration loaded successfully")
// Use command-line flags if provided, otherwise use config values
finalMaxConcurrentRequests := cfg.MaxConcurrentRequests
if maxConcurrentRequests > 0 {
finalMaxConcurrentRequests = maxConcurrentRequests
}
finalMaxRequestsPerClient := cfg.MaxRequestsPerClient
if maxRequestsPerClient > 0 {
finalMaxRequestsPerClient = maxRequestsPerClient
}
sc := steamcache.New(
cfg.ListenAddress,
cfg.Cache.Memory.Size,
@@ -102,6 +116,8 @@ var rootCmd = &cobra.Command{
cfg.Upstream,
cfg.Cache.Memory.GCAlgorithm,
cfg.Cache.Disk.GCAlgorithm,
finalMaxConcurrentRequests,
finalMaxRequestsPerClient,
)
logger.Logger.Info().
@@ -128,4 +144,7 @@ func init() {
rootCmd.Flags().StringVarP(&logLevel, "log-level", "l", "info", "Logging level: debug, info, error")
rootCmd.Flags().StringVarP(&logFormat, "log-format", "f", "console", "Logging format: json, console")
rootCmd.Flags().Int64Var(&maxConcurrentRequests, "max-concurrent-requests", 0, "Maximum concurrent requests (0 = use config file value)")
rootCmd.Flags().Int64Var(&maxRequestsPerClient, "max-requests-per-client", 0, "Maximum concurrent requests per client IP (0 = use config file value)")
}