@@ -17,6 +17,7 @@ import (
1717 "strconv"
1818 "sync"
1919
20+ "golang.org/x/sync/errgroup"
2021 "google.golang.org/grpc"
2122
2223 "github.com/nginx/agent/v3/internal/model"
@@ -288,28 +289,36 @@ func (fms *FileManagerService) ConfigUpdate(ctx context.Context,
288289}
289290
290291func (fms * FileManagerService ) ConfigUpload (ctx context.Context , configUploadRequest * mpi.ConfigUploadRequest ) error {
291- var updatingFilesError error
292+ uploadFiles := configUploadRequest .GetOverview ().GetFiles ()
293+ if len (uploadFiles ) == 0 {
294+ return nil
295+ }
292296
293- for _ , file := range configUploadRequest .GetOverview ().GetFiles () {
294- err := fms .fileServiceOperator .UpdateFile (
295- ctx ,
296- configUploadRequest .GetOverview ().GetConfigVersion ().GetInstanceId (),
297- file ,
298- )
299- if err != nil {
300- slog .ErrorContext (
301- ctx ,
302- "Failed to update file" ,
303- "instance_id" , configUploadRequest .GetOverview ().GetConfigVersion ().GetInstanceId (),
304- "file_name" , file .GetFileMeta ().GetName (),
305- "error" , err ,
297+ errGroup , errGroupCtx := errgroup .WithContext (ctx )
298+ errGroup .SetLimit (fms .agentConfig .Client .Grpc .MaxParallelFileOperations )
299+
300+ for _ , file := range uploadFiles {
301+ errGroup .Go (func () error {
302+ err := fms .fileServiceOperator .UpdateFile (
303+ errGroupCtx ,
304+ configUploadRequest .GetOverview ().GetConfigVersion ().GetInstanceId (),
305+ file ,
306306 )
307+ if err != nil {
308+ slog .ErrorContext (
309+ errGroupCtx ,
310+ "Failed to update file" ,
311+ "instance_id" , configUploadRequest .GetOverview ().GetConfigVersion ().GetInstanceId (),
312+ "file_name" , file .GetFileMeta ().GetName (),
313+ "error" , err ,
314+ )
315+ }
307316
308- updatingFilesError = errors . Join ( updatingFilesError , err )
309- }
317+ return err
318+ })
310319 }
311320
312- return updatingFilesError
321+ return errGroup . Wait ()
313322}
314323
315324// DetermineFileActions compares two sets of files to determine the file action for each file. Returns a map of files
@@ -570,25 +579,36 @@ func (fms *FileManagerService) executeFileActions(ctx context.Context) (actionEr
570579}
571580
572581func (fms * FileManagerService ) downloadUpdatedFilesToTempLocation (ctx context.Context ) (updateError error ) {
582+ var downloadFiles []* model.FileCache
573583 for _ , fileAction := range fms .fileActions {
574584 if fileAction .Action == model .Add || fileAction .Action == model .Update {
585+ downloadFiles = append (downloadFiles , fileAction )
586+ }
587+ }
588+
589+ if len (downloadFiles ) == 0 {
590+ slog .DebugContext (ctx , "No updated files to download" )
591+ return nil
592+ }
593+
594+ errGroup , errGroupCtx := errgroup .WithContext (ctx )
595+ errGroup .SetLimit (fms .agentConfig .Client .Grpc .MaxParallelFileOperations )
596+
597+ for _ , fileAction := range downloadFiles {
598+ errGroup .Go (func () error {
575599 tempFilePath := tempFilePath (fileAction .File .GetFileMeta ().GetName ())
576600
577601 slog .DebugContext (
578- ctx ,
602+ errGroupCtx ,
579603 "Downloading file to temp location" ,
580604 "file" , tempFilePath ,
581605 )
582606
583- updateErr := fms .fileUpdate (ctx , fileAction .File , tempFilePath )
584- if updateErr != nil {
585- updateError = updateErr
586- break
587- }
588- }
607+ return fms .fileUpdate (errGroupCtx , fileAction .File , tempFilePath )
608+ })
589609 }
590610
591- return updateError
611+ return errGroup . Wait ()
592612}
593613
594614func (fms * FileManagerService ) moveOrDeleteFiles (ctx context.Context , actionError error ) error {
0 commit comments