From 83a3c6e0444b04ae95b53991f8e613b9ed96601c Mon Sep 17 00:00:00 2001 From: evpost Date: Wed, 8 Nov 2023 16:36:47 +0300 Subject: [PATCH] fix sync --- internal/client/grpc/client.go | 8 ++++ internal/client/usecase/sync.go | 7 ++- .../controller/grpc_secret_controller.go | 44 ++++++++++++------- 3 files changed, 40 insertions(+), 19 deletions(-) diff --git a/internal/client/grpc/client.go b/internal/client/grpc/client.go index 3b86b56..366c729 100644 --- a/internal/client/grpc/client.go +++ b/internal/client/grpc/client.go @@ -150,6 +150,14 @@ func (c *Grpc) UploadFile(ctx context.Context, fileName string, hash string, syn return fmt.Errorf("UploadFile: %w", err) } } + //nolint:ineffassign,nolintlint + _, err = stream.CloseAndRecv() + if err != nil { + c.log.Err(err).Msg("failed upload") + + return fmt.Errorf("UploadFile: %w", err) + } + c.log.Debug().Msg("UploadFile") return nil diff --git a/internal/client/usecase/sync.go b/internal/client/usecase/sync.go index ed776cb..2db9699 100644 --- a/internal/client/usecase/sync.go +++ b/internal/client/usecase/sync.go @@ -1,4 +1,4 @@ -//nolint:nonamedreturns, durationcheck +//nolint:nonamedreturns,durationcheck,nolintlint package usecase import ( @@ -6,7 +6,6 @@ import ( "encoding/json" "fmt" "sync" - "time" "github.com/google/uuid" "github.com/kripsy/GophKeeper/internal/client/infrastrucrure/ui" @@ -14,7 +13,7 @@ import ( "github.com/kripsy/GophKeeper/internal/utils" ) -const help = time.Duration(4) +// const help = time.Duration(4) func (c *ClientUsecase) sync() { defer c.InMenu() @@ -56,7 +55,7 @@ func (c *ClientUsecase) sync() { return } - time.Sleep(time.Second * help) + // time.Sleep(time.Second * help) if err := c.grpc.ApplyChanges(ctx, syncKey); err != nil { c.ui.PrintErr(ui.SyncErr) c.log.Err(err).Msg("failed apply changes") diff --git a/internal/server/controller/grpc_secret_controller.go b/internal/server/controller/grpc_secret_controller.go index 176c453..e00d50e 100644 --- a/internal/server/controller/grpc_secret_controller.go +++ b/internal/server/controller/grpc_secret_controller.go @@ -17,11 +17,17 @@ import ( "google.golang.org/grpc/status" ) -//nolint:cyclop +//nolint:cyclop,gocognit func (s *GrpcServer) MultipartUploadFile(stream pb.GophKeeperService_MultipartUploadFileServer) error { s.logger.Debug("Start MultipartUploadFile") - ctx := stream.Context() + // streamCtx := stream.Context() + ctx, cancel := context.WithCancel(stream.Context()) + defer func() { + s.logger.Debug("Defer context") + cancel() + }() + userID, ok := utils.ExtractUserIDFromContext(ctx) if !ok { s.logger.Error("cannot get userID from context") @@ -97,7 +103,7 @@ func (s *GrpcServer) MultipartUploadFile(stream pb.GophKeeperService_MultipartUp go func() { defer close(errChanUsecase) - success, err := s.secretUseCase.MultipartUploadFile(stream.Context(), reqChan, bucketName) + success, err := s.secretUseCase.MultipartUploadFile(ctx, reqChan, bucketName) if err != nil { s.logger.Error("Error in s.secretUseCase.MultipartUploadFile", zap.Error(err)) errChanUsecase <- err @@ -112,27 +118,35 @@ func (s *GrpcServer) MultipartUploadFile(stream pb.GophKeeperService_MultipartUp } doneChan <- true }() +loop: + for { + select { + case <-doneChan: + s.logger.Debug("end upload") - select { - case <-doneChan: - s.logger.Debug("end upload") + break loop - case err := <-errChanStream: + case err := <-errChanStream: - if err != nil { - s.logger.Debug("was some error in receive data", zap.Any("msg", err)) + if err != nil { + s.logger.Error("was some error in receive data", zap.Any("msg", err)) - return fmt.Errorf("%w", status.Error(codes.Internal, err.Error())) - } + return fmt.Errorf("%w", status.Error(codes.Internal, err.Error())) + } + s.logger.Debug("Error nil") - case err := <-errChanUsecase: + case err := <-errChanUsecase: - if err != nil { - s.logger.Debug("was some error in usecase", zap.Any("msg", err)) + if err != nil { + s.logger.Error("was some error in usecase", zap.Any("msg", err)) - return fmt.Errorf("%w", status.Error(codes.Internal, err.Error())) + return fmt.Errorf("%w", status.Error(codes.Internal, err.Error())) + } + s.logger.Debug("Error nil") } } + + s.logger.Debug("Send and close") err = stream.SendAndClose(&pb.MultipartUploadFileResponse{ FileId: fileID, })