Skip to content

Commit 88df28a

Browse files
committed
Fix disabling keepalives using new syntax
1 parent 77fd3cc commit 88df28a

7 files changed

Lines changed: 53 additions & 7 deletions

File tree

config/confighttp/deprecated.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ func applyLegacyClientKeepalive(conf *confmap.Conf, keepalive *configoptional.Op
9393
*keepalive = configoptional.None[KeepaliveClientConfig]()
9494
return
9595
}
96+
// If no legacy fields are present, defer entirely to whatever the new `keepalive`
97+
// section (or its absence) already resolved to. Using both legacy fields and the
98+
// new section simultaneously is rejected by collectLegacyKeepaliveWarnings before
99+
// this function is called, so that case cannot arise here.
100+
if !conf.IsSet("idle_conn_timeout") && !conf.IsSet("max_idle_conns") && !conf.IsSet("max_idle_conns_per_host") {
101+
return
102+
}
96103
if !keepalive.HasValue() {
97104
*keepalive = configoptional.Some(KeepaliveClientConfig{})
98105
}
@@ -155,10 +162,15 @@ func applyLegacyServerKeepalive(conf *confmap.Conf, keepalive *configoptional.Op
155162
*keepalive = configoptional.None[KeepaliveServerConfig]()
156163
return
157164
}
165+
// If no legacy fields are present, defer entirely to whatever the new `keepalive`
166+
// section (or its absence) already resolved to. Using both legacy fields and the
167+
// new section simultaneously is rejected by collectLegacyKeepaliveWarnings before
168+
// this function is called, so that case cannot arise here.
169+
if !conf.IsSet("idle_timeout") {
170+
return
171+
}
158172
if !keepalive.HasValue() {
159173
*keepalive = configoptional.Some(KeepaliveServerConfig{})
160174
}
161-
if conf.IsSet("idle_timeout") {
162-
keepalive.Get().IdleTimeout = legacy.IdleTimeout
163-
}
175+
keepalive.Get().IdleTimeout = legacy.IdleTimeout
164176
}

config/confighttp/deprecated_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,10 @@ func TestApplyLegacyClientKeepalive(t *testing.T) {
163163
}),
164164
},
165165
{
166-
name: "initializes empty keepalive when none and not disabled",
166+
name: "no legacy fields leaves None keepalive unchanged",
167167
input: map[string]any{},
168168
initial: configoptional.None[KeepaliveClientConfig](),
169-
want: configoptional.Some(KeepaliveClientConfig{}),
169+
want: configoptional.None[KeepaliveClientConfig](),
170170
},
171171
}
172172

@@ -214,11 +214,11 @@ func TestApplyLegacyServerKeepalive(t *testing.T) {
214214
want: configoptional.Some(KeepaliveServerConfig{IdleTimeout: 120 * time.Second}),
215215
},
216216
{
217-
name: "initializes empty keepalive when none and enabled",
217+
name: "no legacy fields leaves None keepalive unchanged",
218218
input: map[string]any{},
219219
legacy: legacyServerKeepaliveFields{KeepAlivesEnabled: true},
220220
initial: configoptional.None[KeepaliveServerConfig](),
221-
want: configoptional.Some(KeepaliveServerConfig{}),
221+
want: configoptional.None[KeepaliveServerConfig](),
222222
},
223223
}
224224

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
endpoint: "http://localhost:4318"
2+
keepalive:
3+
enabled: false
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
endpoint: "http://localhost:4318"
2+
disable_keep_alives: true
3+
keepalive:
4+
enabled: false
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
endpoint: "0.0.0.0:4318"
2+
keepalive:
3+
enabled: false
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
endpoint: "0.0.0.0:4318"
2+
keep_alives_enabled: false
3+
keepalive:
4+
enabled: false

config/confighttp/unmarshal_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ func TestClientConfigUnmarshal(t *testing.T) {
6666
configFile: "client/mixed_fields_error.yaml",
6767
expectError: true,
6868
},
69+
{
70+
name: "keepalive_disabled",
71+
configFile: "client/keepalive_disabled.yaml",
72+
want: configoptional.None[KeepaliveClientConfig](),
73+
},
74+
{
75+
name: "legacy_with_keepalive_disabled",
76+
configFile: "client/legacy_with_keepalive_disabled.yaml",
77+
expectError: true,
78+
},
6979
{
7080
name: "no_keepalive_fields",
7181
configFile: "client/no_keepalive_fields.yaml",
@@ -161,6 +171,16 @@ func TestServerConfigUnmarshal(t *testing.T) {
161171
configFile: "server/mixed_fields_error.yaml",
162172
expectError: true,
163173
},
174+
{
175+
name: "keepalive_disabled",
176+
configFile: "server/keepalive_disabled.yaml",
177+
want: configoptional.None[KeepaliveServerConfig](),
178+
},
179+
{
180+
name: "legacy_with_keepalive_disabled",
181+
configFile: "server/legacy_with_keepalive_disabled.yaml",
182+
expectError: true,
183+
},
164184
{
165185
name: "no_keepalive_fields",
166186
configFile: "server/no_keepalive_fields.yaml",

0 commit comments

Comments
 (0)