@@ -21,6 +21,7 @@ import (
2121 "strings"
2222 "sync"
2323
24+ "golang.org/x/sync/errgroup"
2425 "google.golang.org/grpc"
2526
2627 "github.com/nginx/agent/v3/internal/model"
@@ -300,28 +301,36 @@ func (fms *FileManagerService) ConfigUpdate(ctx context.Context,
300301}
301302
302303func (fms * FileManagerService ) ConfigUpload (ctx context.Context , configUploadRequest * mpi.ConfigUploadRequest ) error {
303- var updatingFilesError error
304+ uploadFiles := configUploadRequest .GetOverview ().GetFiles ()
305+ if len (uploadFiles ) == 0 {
306+ return nil
307+ }
304308
305- for _ , file := range configUploadRequest .GetOverview ().GetFiles () {
306- err := fms .fileServiceOperator .UpdateFile (
307- ctx ,
308- configUploadRequest .GetOverview ().GetConfigVersion ().GetInstanceId (),
309- file ,
310- )
311- if err != nil {
312- slog .ErrorContext (
313- ctx ,
314- "Failed to update file" ,
315- "instance_id" , configUploadRequest .GetOverview ().GetConfigVersion ().GetInstanceId (),
316- "file_name" , file .GetFileMeta ().GetName (),
317- "error" , err ,
309+ errGroup , errGroupCtx := errgroup .WithContext (ctx )
310+ errGroup .SetLimit (fms .agentConfig .Client .Grpc .MaxParallelFileOperations )
311+
312+ for _ , file := range uploadFiles {
313+ errGroup .Go (func () error {
314+ err := fms .fileServiceOperator .UpdateFile (
315+ errGroupCtx ,
316+ configUploadRequest .GetOverview ().GetConfigVersion ().GetInstanceId (),
317+ file ,
318318 )
319+ if err != nil {
320+ slog .ErrorContext (
321+ errGroupCtx ,
322+ "Failed to update file" ,
323+ "instance_id" , configUploadRequest .GetOverview ().GetConfigVersion ().GetInstanceId (),
324+ "file_name" , file .GetFileMeta ().GetName (),
325+ "error" , err ,
326+ )
327+ }
319328
320- updatingFilesError = errors . Join ( updatingFilesError , err )
321- }
329+ return err
330+ })
322331 }
323332
324- return updatingFilesError
333+ return errGroup . Wait ()
325334}
326335
327336// DetermineFileActions compares two sets of files to determine the file action for each file. Returns a map of files
@@ -585,29 +594,45 @@ func (fms *FileManagerService) executeFileActions(ctx context.Context) (actionEr
585594}
586595
587596func (fms * FileManagerService ) downloadUpdatedFilesToTempLocation (ctx context.Context ) (updateError error ) {
597+ var downloadFiles []* model.FileCache
588598 for _ , fileAction := range fms .fileActions {
589- tempFilePath := tempFilePath (fileAction .File .GetFileMeta ().GetName ())
590-
591- switch fileAction .Action {
592- case model .ExternalFile :
593- updateError = fms .handleExternalFileDownload (ctx , fileAction , tempFilePath )
594- case model .Add , model .Update :
595- slog .DebugContext (
596- ctx ,
597- "Downloading file to temp location" ,
598- "file" , tempFilePath ,
599- )
600- updateError = fms .fileUpdate (ctx , fileAction .File , tempFilePath )
601- case model .Delete , model .Unchanged :
602- continue
599+ if fileAction .Action == model .ExternalFile || fileAction .Action == model .Add ||
600+ fileAction .Action == model .Update {
601+ downloadFiles = append (downloadFiles , fileAction )
603602 }
603+ }
604604
605- if updateError != nil {
606- return updateError
607- }
605+ if len ( downloadFiles ) == 0 {
606+ slog . DebugContext ( ctx , "No updated files to download" )
607+ return nil
608608 }
609+ errGroup , errGroupCtx := errgroup .WithContext (ctx )
610+ errGroup .SetLimit (fms .agentConfig .Client .Grpc .MaxParallelFileOperations )
609611
610- return nil
612+ for _ , fileAction := range downloadFiles {
613+ errGroup .Go (func () error {
614+ tempFilePath := tempFilePath (fileAction .File .GetFileMeta ().GetName ())
615+
616+ switch fileAction .Action {
617+ case model .ExternalFile :
618+ return fms .handleExternalFileDownload (errGroupCtx , fileAction , tempFilePath )
619+ case model .Add , model .Update :
620+ slog .DebugContext (
621+ errGroupCtx ,
622+ "Downloading file to temp location" ,
623+ "file" , tempFilePath ,
624+ )
625+
626+ return fms .fileUpdate (errGroupCtx , fileAction .File , tempFilePath )
627+ case model .Delete , model .Unchanged : // had to add for linter
628+ return nil
629+ default :
630+ return nil
631+ }
632+ })
633+ }
634+
635+ return errGroup .Wait ()
611636}
612637
613638func (fms * FileManagerService ) moveOrDeleteFiles (ctx context.Context , actionError error ) error {
0 commit comments