Skip to content

Commit bd04da7

Browse files
committed
Merge branch 'v3' into chore/support-mulitple-config-updates
2 parents 28692b1 + b6e143c commit bd04da7

File tree

17 files changed

+327
-163
lines changed

17 files changed

+327
-163
lines changed

internal/backoff/backoff.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import (
4848
// Information from https://pkg.go.dev/github.com/cenkalti/backoff/v4#section-readme
4949
func WaitUntil(
5050
ctx context.Context,
51-
backoffSettings *config.CommonSettings,
51+
backoffSettings *config.BackOff,
5252
operation backoff.Operation,
5353
) error {
5454
eb := backoff.NewExponentialBackOff()
@@ -68,7 +68,7 @@ func WaitUntil(
6868
// nolint: ireturn
6969
func WaitUntilWithData[T any](
7070
ctx context.Context,
71-
backoffSettings *config.CommonSettings,
71+
backoffSettings *config.BackOff,
7272
operation backoff.OperationWithData[T],
7373
) (T, error) {
7474
backoffWithContext := Context(ctx, backoffSettings)
@@ -77,7 +77,7 @@ func WaitUntilWithData[T any](
7777
}
7878

7979
// nolint: ireturn
80-
func Context(ctx context.Context, backoffSettings *config.CommonSettings) backoff.BackOffContext {
80+
func Context(ctx context.Context, backoffSettings *config.BackOff) backoff.BackOffContext {
8181
eb := backoff.NewExponentialBackOff()
8282
eb.InitialInterval = backoffSettings.InitialInterval
8383
eb.MaxInterval = backoffSettings.MaxInterval

internal/backoff/backoff_test.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,16 @@ func TestWaitUntil(t *testing.T) {
8282

8383
for _, test := range tests {
8484
invocations = 0
85-
settings := &config.CommonSettings{
86-
InitialInterval: test.initialInterval,
87-
MaxInterval: test.maxInterval,
88-
MaxElapsedTime: test.maxElapsedTime,
89-
RandomizationFactor: config.DefBackoffRandomizationFactor,
90-
Multiplier: config.DefBackoffMultiplier,
85+
settings := &config.Client{
86+
Backoff: &config.BackOff{
87+
InitialInterval: test.initialInterval,
88+
MaxInterval: test.maxInterval,
89+
MaxElapsedTime: test.maxElapsedTime,
90+
RandomizationFactor: config.DefBackoffRandomizationFactor,
91+
Multiplier: config.DefBackoffMultiplier,
92+
},
9193
}
92-
result := WaitUntil(test.context, settings, test.operation)
94+
result := WaitUntil(test.context, settings.Backoff, test.operation)
9395

9496
if test.expectedError {
9597
assert.Errorf(t, result, test.name)
@@ -164,7 +166,7 @@ func TestWaitUntilWithData(t *testing.T) {
164166
}
165167

166168
for _, test := range tests {
167-
settings := &config.CommonSettings{
169+
settings := &config.BackOff{
168170
InitialInterval: test.initialInterval,
169171
MaxInterval: test.maxInterval,
170172
MaxElapsedTime: test.maxElapsedTime,
@@ -185,7 +187,7 @@ func TestWaitUntilWithData(t *testing.T) {
185187
}
186188

187189
func TestContext(t *testing.T) {
188-
settings := &config.CommonSettings{
190+
settings := &config.BackOff{
189191
InitialInterval: 10 * time.Millisecond,
190192
MaxInterval: 10 * time.Millisecond,
191193
MaxElapsedTime: 10 * time.Millisecond,

internal/collector/otel_collector_plugin.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,9 @@ func (oc *Collector) Close(ctx context.Context) error {
203203
oc.service.Shutdown()
204204
oc.cancel()
205205

206-
settings := oc.config.Common
206+
settings := oc.config.Client.Backoff
207207
settings.MaxElapsedTime = maxTimeToWaitForShutdown
208-
err := backoff.WaitUntil(ctx, oc.config.Common, func() error {
208+
err := backoff.WaitUntil(ctx, settings, func() error {
209209
if oc.service.GetState() == otelcol.StateClosed {
210210
return nil
211211
}

internal/command/command_service.go

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"log/slog"
1212
"sync"
1313
"sync/atomic"
14-
"time"
1514

1615
"github.com/cenkalti/backoff/v4"
1716

@@ -29,7 +28,6 @@ import (
2928
var _ commandService = (*CommandService)(nil)
3029

3130
const (
32-
retryInterval = 5 * time.Second
3331
createConnectionMaxElapsedTime = 0
3432
)
3533

@@ -98,7 +96,7 @@ func (cs *CommandService) UpdateDataPlaneStatus(
9896
Resource: resource,
9997
}
10098

101-
backOffCtx, backoffCancel := context.WithTimeout(ctx, cs.agentConfig.Common.MaxElapsedTime)
99+
backOffCtx, backoffCancel := context.WithTimeout(ctx, cs.agentConfig.Client.Backoff.MaxElapsedTime)
102100
defer backoffCancel()
103101

104102
sendDataPlaneStatus := func() (*mpi.UpdateDataPlaneStatusResponse, error) {
@@ -122,7 +120,7 @@ func (cs *CommandService) UpdateDataPlaneStatus(
122120

123121
response, err := backoff.RetryWithData(
124122
sendDataPlaneStatus,
125-
backoffHelpers.Context(backOffCtx, cs.agentConfig.Common),
123+
backoffHelpers.Context(backOffCtx, cs.agentConfig.Client.Backoff),
126124
)
127125
if err != nil {
128126
return err
@@ -148,12 +146,12 @@ func (cs *CommandService) UpdateDataPlaneHealth(ctx context.Context, instanceHea
148146
InstanceHealths: instanceHealths,
149147
}
150148

151-
backOffCtx, backoffCancel := context.WithTimeout(ctx, cs.agentConfig.Common.MaxElapsedTime)
149+
backOffCtx, backoffCancel := context.WithTimeout(ctx, cs.agentConfig.Client.Backoff.MaxElapsedTime)
152150
defer backoffCancel()
153151

154152
response, err := backoff.RetryWithData(
155153
cs.dataPlaneHealthCallback(ctx, request),
156-
backoffHelpers.Context(backOffCtx, cs.agentConfig.Common),
154+
backoffHelpers.Context(backOffCtx, cs.agentConfig.Client.Backoff),
157155
)
158156
if err != nil {
159157
return err
@@ -167,7 +165,7 @@ func (cs *CommandService) UpdateDataPlaneHealth(ctx context.Context, instanceHea
167165
func (cs *CommandService) SendDataPlaneResponse(ctx context.Context, response *mpi.DataPlaneResponse) error {
168166
slog.DebugContext(ctx, "Sending data plane response", "response", response)
169167

170-
backOffCtx, backoffCancel := context.WithTimeout(ctx, cs.agentConfig.Common.MaxElapsedTime)
168+
backOffCtx, backoffCancel := context.WithTimeout(ctx, cs.agentConfig.Client.Backoff.MaxElapsedTime)
171169
defer backoffCancel()
172170

173171
err := cs.handleConfigApplyResponse(ctx, response)
@@ -177,7 +175,7 @@ func (cs *CommandService) SendDataPlaneResponse(ctx context.Context, response *m
177175

178176
return backoff.Retry(
179177
cs.sendDataPlaneResponseCallback(ctx, response),
180-
backoffHelpers.Context(backOffCtx, cs.agentConfig.Common),
178+
backoffHelpers.Context(backOffCtx, cs.agentConfig.Client.Backoff),
181179
)
182180
}
183181

@@ -192,12 +190,12 @@ func (cs *CommandService) CancelSubscription(ctx context.Context) {
192190
}
193191

194192
func (cs *CommandService) subscribe(ctx context.Context) {
195-
commonSettings := &config.CommonSettings{
196-
InitialInterval: cs.agentConfig.Common.InitialInterval,
197-
MaxInterval: cs.agentConfig.Common.MaxInterval,
193+
commonSettings := &config.BackOff{
194+
InitialInterval: cs.agentConfig.Client.Backoff.InitialInterval,
195+
MaxInterval: cs.agentConfig.Client.Backoff.MaxInterval,
198196
MaxElapsedTime: createConnectionMaxElapsedTime,
199-
RandomizationFactor: cs.agentConfig.Common.RandomizationFactor,
200-
Multiplier: cs.agentConfig.Common.Multiplier,
197+
RandomizationFactor: cs.agentConfig.Client.Backoff.RandomizationFactor,
198+
Multiplier: cs.agentConfig.Client.Backoff.Multiplier,
201199
}
202200

203201
for {
@@ -231,12 +229,12 @@ func (cs *CommandService) CreateConnection(
231229
Resource: resource,
232230
}
233231

234-
commonSettings := &config.CommonSettings{
235-
InitialInterval: cs.agentConfig.Common.InitialInterval,
236-
MaxInterval: cs.agentConfig.Common.MaxInterval,
232+
commonSettings := &config.BackOff{
233+
InitialInterval: cs.agentConfig.Client.Backoff.InitialInterval,
234+
MaxInterval: cs.agentConfig.Client.Backoff.MaxInterval,
237235
MaxElapsedTime: createConnectionMaxElapsedTime,
238-
RandomizationFactor: cs.agentConfig.Common.RandomizationFactor,
239-
Multiplier: cs.agentConfig.Common.Multiplier,
236+
RandomizationFactor: cs.agentConfig.Client.Backoff.RandomizationFactor,
237+
Multiplier: cs.agentConfig.Client.Backoff.Multiplier,
240238
}
241239

242240
slog.DebugContext(ctx, "Sending create connection request", "request", request)

internal/config/config.go

Lines changed: 91 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"path/filepath"
1515
"slices"
1616
"strings"
17-
"time"
1817

1918
selfsignedcerts "github.com/nginx/agent/v3/pkg/tls"
2019
uuidLibrary "github.com/nginx/agent/v3/pkg/uuid"
@@ -101,7 +100,6 @@ func ResolveConfig() (*Config, error) {
101100
AllowedDirectories: allowedDirs,
102101
Collector: collector,
103102
Command: resolveCommand(),
104-
Common: resolveCommon(),
105103
Watchers: resolveWatchers(),
106104
Features: viperInstance.GetStringSlice(FeaturesKey),
107105
}
@@ -155,7 +153,6 @@ func registerFlags() {
155153
"collection or error monitoring",
156154
)
157155

158-
fs.Duration(ClientTimeoutKey, time.Minute, "Client timeout")
159156
fs.StringSlice(AllowedDirectoriesKey,
160157
DefaultAllowedDirectories(),
161158
"A comma-separated list of paths that you want to grant NGINX Agent read/write access to")
@@ -178,24 +175,6 @@ func registerFlags() {
178175
"How often the NGINX Agent will check for file changes.",
179176
)
180177

181-
fs.Int(
182-
ClientMaxMessageSizeKey,
183-
DefMaxMessageSize,
184-
"The value used, if not 0, for both max_message_send_size and max_message_receive_size",
185-
)
186-
187-
fs.Int(
188-
ClientMaxMessageReceiveSizeKey,
189-
DefMaxMessageRecieveSize,
190-
"Updates the client grpc setting MaxRecvMsgSize with the specific value in MB.",
191-
)
192-
193-
fs.Int(
194-
ClientMaxMessageSendSizeKey,
195-
DefMaxMessageSendSize,
196-
"Updates the client grpc setting MaxSendMsgSize with the specific value in MB.",
197-
)
198-
199178
fs.StringSlice(
200179
FeaturesKey,
201180
DefaultFeatures(),
@@ -204,6 +183,7 @@ func registerFlags() {
204183

205184
registerCommandFlags(fs)
206185
registerCollectorFlags(fs)
186+
registerClientFlags(fs)
207187

208188
fs.SetNormalizeFunc(normalizeFunc)
209189

@@ -218,6 +198,76 @@ func registerFlags() {
218198
})
219199
}
220200

201+
func registerClientFlags(fs *flag.FlagSet) {
202+
// HTTP Flags
203+
fs.Duration(
204+
ClientHTTPTimeoutKey,
205+
DefHTTPTimeout,
206+
"The client HTTP Timeout, value in seconds")
207+
208+
// Backoff Flags
209+
fs.Duration(
210+
ClientBackoffInitialIntervalKey,
211+
DefBackoffInitialInterval,
212+
"The client backoff initial interval, value in seconds")
213+
214+
fs.Duration(
215+
ClientBackoffMaxIntervalKey,
216+
DefBackoffMaxInterval,
217+
"The client backoff max interval, value in seconds")
218+
219+
fs.Duration(
220+
ClientBackoffMaxElapsedTimeKey,
221+
DefBackoffMaxElapsedTime,
222+
"The client backoff max elapsed time, value in seconds")
223+
224+
fs.Float64(
225+
ClientBackoffRandomizationFactorKey,
226+
DefBackoffRandomizationFactor,
227+
"The client backoff randomization factor, value float")
228+
229+
fs.Float64(
230+
ClientBackoffMultiplierKey,
231+
DefBackoffMultiplier,
232+
"The client backoff multiplier, value float")
233+
234+
// GRPC Flags
235+
fs.Duration(
236+
ClientKeepAliveTimeoutKey,
237+
DefGRPCKeepAliveTimeout,
238+
"Updates the client grpc setting, KeepAlive Timeout with the specific value in seconds.",
239+
)
240+
241+
fs.Duration(
242+
ClientKeepAliveTimeKey,
243+
DefGRPCKeepAliveTime,
244+
"Updates the client grpc setting, KeepAlive Time with the specific value in seconds.",
245+
)
246+
247+
fs.Bool(
248+
ClientKeepAlivePermitWithoutStreamKey,
249+
DefGRPCKeepAlivePermitWithoutStream,
250+
"Update the client grpc setting, KeepAlive PermitWithoutStream value")
251+
252+
fs.Int(
253+
ClientGRPCMaxMessageSizeKey,
254+
DefMaxMessageSize,
255+
"The value used, if not 0, for both max_message_send_size and max_message_receive_size",
256+
)
257+
258+
fs.Int(
259+
ClientGRPCMaxMessageReceiveSizeKey,
260+
DefMaxMessageRecieveSize,
261+
"Updates the client grpc setting MaxRecvMsgSize with the specific value in MB.",
262+
)
263+
264+
fs.Int(
265+
ClientGRPCMaxMessageSendSizeKey,
266+
DefMaxMessageSendSize,
267+
"Updates the client grpc setting MaxSendMsgSize with the specific value in MB.",
268+
)
269+
}
270+
221271
func registerCommandFlags(fs *flag.FlagSet) {
222272
fs.String(
223273
CommandServerHostKey,
@@ -418,12 +468,26 @@ func resolveDataPlaneConfig() *DataPlaneConfig {
418468

419469
func resolveClient() *Client {
420470
return &Client{
421-
Timeout: viperInstance.GetDuration(ClientTimeoutKey),
422-
Time: viperInstance.GetDuration(ClientTimeKey),
423-
PermitWithoutStream: viperInstance.GetBool(ClientPermitWithoutStreamKey),
424-
MaxMessageSize: viperInstance.GetInt(ClientMaxMessageSizeKey),
425-
MaxMessageRecieveSize: viperInstance.GetInt(ClientMaxMessageReceiveSizeKey),
426-
MaxMessageSendSize: viperInstance.GetInt(ClientMaxMessageSendSizeKey),
471+
HTTP: &HTTP{
472+
Timeout: viperInstance.GetDuration(ClientHTTPTimeoutKey),
473+
},
474+
Grpc: &GRPC{
475+
KeepAlive: &KeepAlive{
476+
Timeout: viperInstance.GetDuration(ClientKeepAliveTimeoutKey),
477+
Time: viperInstance.GetDuration(ClientKeepAliveTimeKey),
478+
PermitWithoutStream: viperInstance.GetBool(ClientKeepAlivePermitWithoutStreamKey),
479+
},
480+
MaxMessageSize: viperInstance.GetInt(ClientGRPCMaxMessageSizeKey),
481+
MaxMessageReceiveSize: viperInstance.GetInt(ClientGRPCMaxMessageReceiveSizeKey),
482+
MaxMessageSendSize: viperInstance.GetInt(ClientGRPCMaxMessageSendSizeKey),
483+
},
484+
Backoff: &BackOff{
485+
InitialInterval: viperInstance.GetDuration(ClientBackoffInitialIntervalKey),
486+
MaxInterval: viperInstance.GetDuration(ClientBackoffMaxIntervalKey),
487+
MaxElapsedTime: viperInstance.GetDuration(ClientBackoffMaxElapsedTimeKey),
488+
RandomizationFactor: viperInstance.GetFloat64(ClientBackoffRandomizationFactorKey),
489+
Multiplier: viperInstance.GetFloat64(ClientBackoffMultiplierKey),
490+
},
427491
}
428492
}
429493

@@ -686,16 +750,6 @@ func arePrometheusExportTLSSettingsSet() bool {
686750
viperInstance.IsSet(CollectorPrometheusExporterTLSServerNameKey)
687751
}
688752

689-
func resolveCommon() *CommonSettings {
690-
return &CommonSettings{
691-
InitialInterval: DefBackoffInitialInterval,
692-
MaxInterval: DefBackoffMaxInterval,
693-
MaxElapsedTime: DefBackoffMaxElapsedTime,
694-
RandomizationFactor: DefBackoffRandomizationFactor,
695-
Multiplier: DefBackoffMultiplier,
696-
}
697-
}
698-
699753
func resolveWatchers() *Watchers {
700754
return &Watchers{
701755
InstanceWatcher: InstanceWatcher{

0 commit comments

Comments
 (0)