{"record":{"id":"38360452d300f7b1","repo":"gofr-dev/gofr","slug":"maxidleconns-cannot-be-negative","errorCode":null,"errorMessage":"MaxIdleConns cannot be negative","messagePattern":"MaxIdleConns cannot be negative","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/gofr/service/connection_pool.go","lineNumber":11,"sourceCode":"package service\n\nimport (\n\t\"errors\"\n\t\"fmt\"\n\t\"net/http\"\n\t\"time\"\n)\n\nvar (\n\terrNegativeMaxIdleConns        = errors.New(\"MaxIdleConns cannot be negative\")\n\terrNegativeMaxIdleConnsPerHost = errors.New(\"MaxIdleConnsPerHost cannot be negative\")\n\terrNegativeIdleConnTimeout     = errors.New(\"IdleConnTimeout cannot be negative\")\n)\n\n// ConnectionPoolConfig holds the configuration for HTTP connection pool settings.\n// It customizes the HTTP transport layer to optimize connection reuse for high-frequency requests.\n//\n// Note: This configuration must be applied first when using multiple options with AddHTTPService,\n// as it needs to access the underlying HTTP client transport. If applied after wrapper options\n// (CircuitBreaker, Retry, OAuth), it will be silently ignored.\n//\n// Example:\n//\n//\tapp.AddHTTPService(\"api-service\", \"https://api.example.com\",\n//\t    &service.ConnectionPoolConfig{\n//\t        MaxIdleConns:        100,\n//\t        MaxIdleConnsPerHost: 20,\n//\t        IdleConnTimeout:     90 * time.Second,","sourceCodeStart":1,"sourceCodeEnd":29,"githubUrl":"https://github.com/gofr-dev/gofr/blob/187eb24962502e91f1fee856230670958b66e89c/pkg/gofr/service/connection_pool.go#L1-L29","documentation":"errNegativeMaxIdleConns is a validation sentinel: ConnectionPoolConfig.Validate rejects a negative MaxIdleConns because net/http's Transport cannot accept a negative idle-connection pool size. It's wrapped with the offending value by Validate and returned when adding connection-pool options to an HTTP service.","triggerScenarios":"Constructing ConnectionPoolConfig{MaxIdleConns: -1} (or any negative) and calling Validate() directly, or passing it via AddOption/AddHTTPService which invokes Validate before applying the transport settings.","commonSituations":"Computing pool sizes from environment/config where an unset or misparsed variable becomes negative; subtracting in size math (e.g. total - reserved) yielding negative; copy-paste sign errors in defaults.","solutions":["Fix the config source so MaxIdleConns is >= 0 (0 is valid and means default)","Clamp before validation: if v < 0 { v = 0 } or use math.Max(0, v)","Validate configuration at startup so a bad env value fails fast with a clear message","Use errors.Is(err, errNegativeMaxIdleConns) to report which field is invalid in config loaders"],"exampleFix":"// before\npool := &ConnectionPoolConfig{MaxIdleConns: -1}\n// after\nmaxIdle := envInt(\"MAX_IDLE_CONNS\", 100)\nif maxIdle < 0 { maxIdle = 0 }\npool := &ConnectionPoolConfig{MaxIdleConns: maxIdle}","handlingStrategy":"validation","validationCode":"if pool.MaxIdleConns < 0 {\n    return fmt.Errorf(\"MaxIdleConns must be >= 0, got %d\", pool.MaxIdleConns)\n}\nif err := pool.Validate(); err != nil { return err }","typeGuard":"func (c *ConnectionPoolConfig) IsSane() bool {\n    return c.MaxIdleConns >= 0 && c.MaxIdleConnsPerHost >= 0 && c.IdleConnTimeout >= 0\n}","tryCatchPattern":"if err := pool.Validate(); err != nil {\n    if errors.Is(err, errNegativeMaxIdleConns) {\n        pool.MaxIdleConns = 0 // fall back to default and retry once\n    } else {\n        return err\n    }\n}","preventionTips":["Clamp sizes with max(0, v) whenever values come from env or computed math","Call Validate() immediately after loading config, before AddHTTPService","Use errors.Is against the sentinel for precise diagnostics","Remember 0 is valid (means default); only negatives are rejected"],"tags":["http-client","configuration","connection-pool","validation"],"backgroundTag":"invalid-config-value","analyzedSha":"187eb24962502e91f1fee856230670958b66e89c","analyzedAt":"2026-09-01T20:34:54.554Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}