(stream grpc.ClientStreamingServer[common.UsersChunk, common.Empty])
| 43 | } |
| 44 | |
| 45 | func (s *Service) SyncUsersChunked(stream grpc.ClientStreamingServer[common.UsersChunk, common.Empty]) error { |
| 46 | chunks := make(map[uint64][]*common.User) |
| 47 | var ( |
| 48 | lastIndex uint64 |
| 49 | sawLast bool |
| 50 | ) |
| 51 | |
| 52 | for { |
| 53 | chunk, err := stream.Recv() |
| 54 | if errors.Is(err, io.EOF) { |
| 55 | break |
| 56 | } |
| 57 | if err != nil { |
| 58 | return status.Errorf(codes.Internal, "failed to receive chunk: %v", err) |
| 59 | } |
| 60 | |
| 61 | chunks[chunk.GetIndex()] = append(chunks[chunk.GetIndex()], chunk.GetUsers()...) |
| 62 | |
| 63 | if chunk.GetLast() { |
| 64 | sawLast = true |
| 65 | lastIndex = chunk.GetIndex() |
| 66 | break |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | users, err := controller.BuildUsersFromChunks(chunks, lastIndex, sawLast) |
| 71 | if err != nil { |
| 72 | return status.Error(codes.InvalidArgument, err.Error()) |
| 73 | } |
| 74 | |
| 75 | // Large chunk: update in-memory then restart (no API calls). |
| 76 | if len(users) > 100 { |
| 77 | if err := s.Backend().UpdateUsersAndRestart(stream.Context(), users); err != nil { |
| 78 | return status.Errorf(codes.Internal, "failed to update users: %v", err) |
| 79 | } |
| 80 | } else { |
| 81 | // Small chunk: update via API without restart. |
| 82 | if err := s.Backend().UpdateUsers(stream.Context(), users); err != nil { |
| 83 | return status.Errorf(codes.Internal, "failed to update users: %v", err) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return stream.SendAndClose(&common.Empty{}) |
| 88 | } |
nothing calls this directly
no test coverage detected