Skip to content

Commit af2e8cc

Browse files
authored
Retry request if no response is sent by management plane (#1381)
1 parent 98514ea commit af2e8cc

File tree

14 files changed

+69
-20
lines changed

14 files changed

+69
-20
lines changed

internal/command/command_service.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,16 @@ func (cs *CommandService) UpdateDataPlaneStatus(
104104
cs.subscribeClientMutex.Unlock()
105105
return nil, errors.New("command service client is not initialized")
106106
}
107-
response, updateError := cs.commandServiceClient.UpdateDataPlaneStatus(ctx, request)
107+
108+
grpcCtx, cancel := context.WithTimeout(ctx, cs.agentConfig.Client.Grpc.ResponseTimeout)
109+
defer cancel()
110+
111+
response, updateError := cs.commandServiceClient.UpdateDataPlaneStatus(grpcCtx, request)
108112
cs.subscribeClientMutex.Unlock()
109113

110114
validatedError := grpc.ValidateGrpcError(updateError)
111115
if validatedError != nil {
112-
slog.ErrorContext(ctx, "Failed to send update data plane status", "error", validatedError)
116+
slog.ErrorContext(grpcCtx, "Failed to send update data plane status", "error", validatedError)
113117

114118
return nil, validatedError
115119
}
@@ -384,13 +388,16 @@ func (cs *CommandService) dataPlaneHealthCallback(
384388
return nil, errors.New("command service client is not initialized")
385389
}
386390

387-
response, updateError := cs.commandServiceClient.UpdateDataPlaneHealth(ctx, request)
391+
grpcCtx, cancel := context.WithTimeout(ctx, cs.agentConfig.Client.Grpc.ResponseTimeout)
392+
defer cancel()
393+
394+
response, updateError := cs.commandServiceClient.UpdateDataPlaneHealth(grpcCtx, request)
388395
cs.subscribeClientMutex.Unlock()
389396

390397
validatedError := grpc.ValidateGrpcError(updateError)
391398

392399
if validatedError != nil {
393-
slog.ErrorContext(ctx, "Failed to send update data plane health", "error", validatedError)
400+
slog.ErrorContext(grpcCtx, "Failed to send update data plane health", "error", validatedError)
394401

395402
return nil, validatedError
396403
}
@@ -558,13 +565,16 @@ func (cs *CommandService) connectCallback(
558565
request *mpi.CreateConnectionRequest,
559566
) func() (*mpi.CreateConnectionResponse, error) {
560567
return func() (*mpi.CreateConnectionResponse, error) {
568+
grpcCtx, cancel := context.WithTimeout(ctx, cs.agentConfig.Client.Grpc.ResponseTimeout)
569+
defer cancel()
570+
561571
cs.subscribeClientMutex.Lock()
562-
response, connectErr := cs.commandServiceClient.CreateConnection(ctx, request)
572+
response, connectErr := cs.commandServiceClient.CreateConnection(grpcCtx, request)
563573
cs.subscribeClientMutex.Unlock()
564574

565575
validatedError := grpc.ValidateGrpcError(connectErr)
566576
if validatedError != nil {
567-
slog.ErrorContext(ctx, "Failed to create connection", "error", validatedError)
577+
slog.ErrorContext(grpcCtx, "Failed to create connection", "error", validatedError)
568578

569579
return nil, validatedError
570580
}

internal/config/config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,12 @@ func registerClientFlags(fs *flag.FlagSet) {
629629
"Max file size in bytes.",
630630
)
631631

632+
fs.Duration(
633+
ClientGRPCResponseTimeoutKey,
634+
DefResponseTimeout,
635+
"Duration to wait for a response before retrying request",
636+
)
637+
632638
fs.Int(
633639
ClientGRPCMaxParallelFileOperationsKey,
634640
DefMaxParallelFileOperations,
@@ -1111,6 +1117,7 @@ func resolveClient() *Client {
11111117
MaxMessageSendSize: viperInstance.GetInt(ClientGRPCMaxMessageSendSizeKey),
11121118
MaxFileSize: viperInstance.GetUint32(ClientGRPCMaxFileSizeKey),
11131119
FileChunkSize: viperInstance.GetUint32(ClientGRPCFileChunkSizeKey),
1120+
ResponseTimeout: viperInstance.GetDuration(ClientGRPCResponseTimeoutKey),
11141121
MaxParallelFileOperations: viperInstance.GetInt(ClientGRPCMaxParallelFileOperationsKey),
11151122
},
11161123
Backoff: &BackOff{

internal/config/config_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,6 +1184,7 @@ func createConfig() *Config {
11841184
MaxFileSize: 485753,
11851185
FileChunkSize: 48575,
11861186
MaxParallelFileOperations: 10,
1187+
ResponseTimeout: 30 * time.Second,
11871188
},
11881189
Backoff: &BackOff{
11891190
InitialInterval: 200 * time.Millisecond,

internal/config/defaults.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ const (
6666
DefMaxFileSize uint32 = 1048576 // 1MB
6767
DefFileChunkSize uint32 = 524288 // 0.5MB
6868
DefMaxParallelFileOperations = 5
69+
DefResponseTimeout = 10 * time.Second
6970

7071
// Client HTTP Settings
7172
DefHTTPTimeout = 10 * time.Second

internal/config/flags.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ var (
4141
ClientGRPCMaxFileSizeKey = pre(ClientRootKey) + "grpc_max_file_size"
4242
ClientGRPCFileChunkSizeKey = pre(ClientRootKey) + "grpc_file_chunk_size"
4343
ClientGRPCMaxParallelFileOperationsKey = pre(ClientRootKey) + "grpc_max_parallel_file_operations"
44+
ClientGRPCResponseTimeoutKey = pre(ClientRootKey) + "grpc_response_timeout"
4445

4546
ClientBackoffInitialIntervalKey = pre(ClientRootKey) + "backoff_initial_interval"
4647
ClientBackoffMaxIntervalKey = pre(ClientRootKey) + "backoff_max_interval"

internal/config/testdata/nginx-agent.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ client:
5353
max_message_receive_size: 1048575
5454
max_message_send_size: 1048575
5555
max_file_size: 485753
56+
response_timeout: 30s
5657
file_chunk_size: 48575
5758
max_parallel_file_operations: 10
5859
backoff:

internal/config/types.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ type (
9393

9494
//nolint:lll // max line limit exceeded
9595
GRPC struct {
96-
KeepAlive *KeepAlive `yaml:"keepalive" mapstructure:"keepalive"`
96+
KeepAlive *KeepAlive `yaml:"keepalive" mapstructure:"keepalive"`
97+
ResponseTimeout time.Duration `yaml:"response_timeout" mapstructure:"response_timeout"`
9798
// if MaxMessageSize is size set then we use that value,
9899
// otherwise MaxMessageRecieveSize and MaxMessageSendSize for individual settings
99100
MaxMessageSize int `yaml:"max_message_size" mapstructure:"max_message_size"`

internal/file/file_service_operator.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,15 @@ func (fso *FileServiceOperator) UpdateOverview(
182182
"request", request, "parent_correlation_id", correlationID,
183183
)
184184

185-
response, updateError := fso.fileServiceClient.UpdateOverview(newCtx, request)
185+
grpcCtx, cancel := context.WithTimeout(ctx, fso.agentConfig.Client.Grpc.ResponseTimeout)
186+
defer cancel()
187+
188+
response, updateError := fso.fileServiceClient.UpdateOverview(grpcCtx, request)
186189

187190
validatedError := internalgrpc.ValidateGrpcError(updateError)
188191

189192
if validatedError != nil {
190-
slog.ErrorContext(newCtx, "Failed to send update overview", "error", validatedError)
193+
slog.ErrorContext(grpcCtx, "Failed to send update overview", "error", validatedError)
191194

192195
return nil, validatedError
193196
}

test/config/agent/nginx-agent-with-auxiliary-command.conf

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ auxiliary_command:
1919
port: 9095
2020
type: grpc
2121

22-
22+
client:
23+
grpc:
24+
response_timeout: 2s
25+
2326
allowed_directories:
2427
- /etc/nginx
2528
- /usr/local/etc/nginx

test/config/agent/nginx-config-with-grpc-client.conf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ command:
1313
port: 9092
1414
type: grpc
1515

16+
client:
17+
grpc:
18+
response_timeout: 2s
19+
1620
allowed_directories:
1721
- /etc/nginx
1822
- /usr/local/etc/nginx

0 commit comments

Comments
 (0)