Skip to content

Commit

Permalink
fix sync
Browse files Browse the repository at this point in the history
  • Loading branch information
kripsy committed Nov 8, 2023
1 parent b67f3ed commit 83a3c6e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
8 changes: 8 additions & 0 deletions internal/client/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions internal/client/usecase/sync.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
//nolint:nonamedreturns, durationcheck
//nolint:nonamedreturns,durationcheck,nolintlint
package usecase

import (
"context"
"encoding/json"
"fmt"
"sync"
"time"

"github.com/google/uuid"
"github.com/kripsy/GophKeeper/internal/client/infrastrucrure/ui"
"github.com/kripsy/GophKeeper/internal/models"
"github.com/kripsy/GophKeeper/internal/utils"
)

const help = time.Duration(4)
// const help = time.Duration(4)

func (c *ClientUsecase) sync() {
defer c.InMenu()
Expand Down Expand Up @@ -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")
Expand Down
44 changes: 29 additions & 15 deletions internal/server/controller/grpc_secret_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand All @@ -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,
})
Expand Down

0 comments on commit 83a3c6e

Please sign in to comment.