Skip to content

Commit e318140

Browse files
aphralGAkshay2191
authored andcommitted
Retry request if no response is sent by management plane (#1381)
1 parent 51cb0f5 commit e318140

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
@@ -658,6 +658,12 @@ func registerClientFlags(fs *flag.FlagSet) {
658658
"Timeout value in seconds, for downloading a file during a config apply.",
659659
)
660660

661+
fs.Duration(
662+
ClientGRPCResponseTimeoutKey,
663+
DefResponseTimeout,
664+
"Duration to wait for a response before retrying request",
665+
)
666+
661667
fs.Int(
662668
ClientGRPCMaxParallelFileOperationsKey,
663669
DefMaxParallelFileOperations,
@@ -1140,6 +1146,7 @@ func resolveClient() *Client {
11401146
MaxMessageSendSize: viperInstance.GetInt(ClientGRPCMaxMessageSendSizeKey),
11411147
MaxFileSize: viperInstance.GetUint32(ClientGRPCMaxFileSizeKey),
11421148
FileChunkSize: viperInstance.GetUint32(ClientGRPCFileChunkSizeKey),
1149+
ResponseTimeout: viperInstance.GetDuration(ClientGRPCResponseTimeoutKey),
11431150
MaxParallelFileOperations: viperInstance.GetInt(ClientGRPCMaxParallelFileOperationsKey),
11441151
},
11451152
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
@@ -42,6 +42,7 @@ var (
4242
ClientGRPCMaxFileSizeKey = pre(ClientRootKey) + "grpc_max_file_size"
4343
ClientGRPCFileChunkSizeKey = pre(ClientRootKey) + "grpc_file_chunk_size"
4444
ClientGRPCMaxParallelFileOperationsKey = pre(ClientRootKey) + "grpc_max_parallel_file_operations"
45+
ClientGRPCResponseTimeoutKey = pre(ClientRootKey) + "grpc_response_timeout"
4546

4647
ClientBackoffInitialIntervalKey = pre(ClientRootKey) + "backoff_initial_interval"
4748
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
@@ -95,7 +95,8 @@ type (
9595

9696
//nolint:lll // max line limit exceeded
9797
GRPC struct {
98-
KeepAlive *KeepAlive `yaml:"keepalive" mapstructure:"keepalive"`
98+
KeepAlive *KeepAlive `yaml:"keepalive" mapstructure:"keepalive"`
99+
ResponseTimeout time.Duration `yaml:"response_timeout" mapstructure:"response_timeout"`
99100
// if MaxMessageSize is size set then we use that value,
100101
// otherwise MaxMessageRecieveSize and MaxMessageSendSize for individual settings
101102
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
@@ -185,12 +185,15 @@ func (fso *FileServiceOperator) UpdateOverview(
185185
"request", request, "parent_correlation_id", correlationID,
186186
)
187187

188-
response, updateError := fso.fileServiceClient.UpdateOverview(newCtx, request)
188+
grpcCtx, cancel := context.WithTimeout(ctx, fso.agentConfig.Client.Grpc.ResponseTimeout)
189+
defer cancel()
190+
191+
response, updateError := fso.fileServiceClient.UpdateOverview(grpcCtx, request)
189192

190193
validatedError := internalgrpc.ValidateGrpcError(updateError)
191194

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

195198
return nil, validatedError
196199
}

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)