Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(infinity): make pool max idle connections configurable via INFINITY_POOL_MAX_IDLE (#20831)

qinling0210bd3ef98d3e9a
diff --git a/internal/engine/infinity/client.go b/internal/engine/infinity/client.go
index 6b1d732e68..8a6bd2078b 100644
--- a/internal/engine/infinity/client.go
+++ b/internal/engine/infinity/client.go
@@ -67,9 +67,9 @@ type infinityClient struct {
// to this rather than becoming unbounded.
const defaultPoolMaxSize = 4
-// defaultMaxIdleConnections caps how many established connections the pool
-// keeps idle for reuse, independent of the pool's hard open cap (MaxOpen).
-// It bounds idle socket retention on the Infinity server when MaxOpen is large.
+// defaultMaxIdleConnections is the idle cap (MaxIdle) for the Go Infinity
+// connection pool. Invalid or missing INFINITY_POOL_MAX_IDLE values fall back
+// to this rather than becoming unbounded.
const defaultMaxIdleConnections = 2
// defaultOperationTimeout bounds the total duration of a single pool operation
@@ -96,6 +96,26 @@ func resolvePoolMaxSize() int {
return v
}
+// resolvePoolMaxIdle reads INFINITY_POOL_MAX_IDLE and falls back to defaultMaxIdleConnections.
+// A value < 1 is rejected so idle retention stays bounded rather than unbounded.
+// The result is clamped to maxOpen so MaxIdle never exceeds the pool's hard open cap.
+func resolvePoolMaxIdle(maxOpen int) int {
+ idle := defaultMaxIdleConnections
+ if raw := os.Getenv("INFINITY_POOL_MAX_IDLE"); raw != "" {
+ v, err := strconv.Atoi(raw)
+ if err != nil || v < 1 {
+ common.Warn("INFINITY_POOL_MAX_IDLE must be a positive integer; using default",
+ zap.String("value", raw), zap.Int("default", defaultMaxIdleConnections))
+ } else {
+ idle = v
+ }
+ }
+ if idle > maxOpen {
+ idle = maxOpen
+ }
+ return idle
+}
+
// NewInfinityClient creates a new Infinity client using the SDK
type poolExhaustedError struct {
caller string
@@ -145,15 +165,10 @@ func NewInfinityClient(cfg config.InfinityConfig) (*infinityClient, error) {
common.Info("Connecting to Infinity")
uri := infinity.NetworkAddress{IP: host, Port: port}
maxOpen := resolvePoolMaxSize()
+ maxIdle := resolvePoolMaxIdle(maxOpen)
var pool *infinity.ConnectionPool
var err error
for i := 0; i < 24; i++ {
- // Cap idle connections retained for reuse; never exceed the pool's
- // hard open cap (MaxOpen).
- maxIdle := maxOpen
- if maxIdle > defaultMaxIdleConnections {
- maxIdle = defaultMaxIdleConnections
- }
poolCfg := infinity.DefaultConnectionPoolConfig(uri)
// Don't pre-open connections at process startup (InitialSize=0);
// connections are created lazily on first use. Keep MaxIdle so that
diff --git a/internal/engine/infinity/client_test.go b/internal/engine/infinity/client_test.go
index 7cf2c2b262..2491c4908f 100644
--- a/internal/engine/infinity/client_test.go
+++ b/internal/engine/infinity/client_test.go
@@ -40,6 +40,57 @@ func TestPoolExhaustedError_Contract(t *testing.T) {
}
}
+// TestResolvePoolMaxSize covers the INFINITY_POOL_MAX_SIZE knob: unset/invalid
+// values fall back to the default rather than becoming unbounded.
+func TestResolvePoolMaxSize(t *testing.T) {
+ cases := []struct {
+ name string
+ raw string
+ want int
+ }{
+ {"unset", "", defaultPoolMaxSize},
+ {"valid", "16", 16},
+ {"zero rejected", "0", defaultPoolMaxSize},
+ {"negative rejected", "-3", defaultPoolMaxSize},
+ {"non-numeric rejected", "many", defaultPoolMaxSize},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ t.Setenv("INFINITY_POOL_MAX_SIZE", tc.raw)
+ if got := resolvePoolMaxSize(); got != tc.want {
+ t.Fatalf("resolvePoolMaxSize() = %d, want %d", got, tc.want)
+ }
+ })
+ }
+}
+
+// TestResolvePoolMaxIdle covers the INFINITY_POOL_MAX_IDLE knob: unset/invalid
+// values fall back to the default, and the result is always clamped to MaxOpen.
+func TestResolvePoolMaxIdle(t *testing.T) {
+ cases := []struct {
+ name string
+ raw string
+ maxOpen int
+ want int
+ }{
+ {"unset falls back to default", "", 8, defaultMaxIdleConnections},
+ {"valid value used", "5", 8, 5},
+ {"clamped to maxOpen", "5", 3, 3},
+ {"default clamped to small maxOpen", "", 1, 1},
+ {"zero rejected", "0", 8, defaultMaxIdleConnections},
+ {"negative rejected", "-2", 8, defaultMaxIdleConnections},
+ {"non-numeric rejected", "lots", 8, defaultMaxIdleConnections},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ t.Setenv("INFINITY_POOL_MAX_IDLE", tc.raw)
+ if got := resolvePoolMaxIdle(tc.maxOpen); got != tc.want {
+ t.Fatalf("resolvePoolMaxIdle(%d) = %d, want %d", tc.maxOpen, got, tc.want)
+ }
+ })
+ }
+}
+
// TestEnsureDeadline covers the core of the bounded-latency fix: callers that
// pass context.Background() (no deadline) must get one applied, while callers
// that already carry a deadline are left untouched and their cancel is not