Skip to content

Commit e3a8833

Browse files
committed
chore: address review comments
1 parent 784d3f6 commit e3a8833

File tree

2 files changed

+112
-108
lines changed

2 files changed

+112
-108
lines changed
Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,37 @@
33
// This source code is licensed under the Apache License, Version 2.0 license found in the
44
// LICENSE file in the root directory of this source tree.
55

6-
package v1
6+
package files
77

88
import (
99
"errors"
1010
"fmt"
1111
"io"
1212

1313
"google.golang.org/grpc"
14-
)
1514

16-
var (
17-
ErrInvalidHeader = errors.New("invalid header")
18-
ErrUnexpectedContent = errors.New("unexpected content")
19-
ErrSend = errors.New("send error")
20-
ErrWrite = errors.New("write error")
21-
ErrFailedRead = errors.New("failed to read")
15+
"github.com/nginx/agent/v3/api/grpc/mpi/v1"
2216
)
2317

2418
// SendChunkedFile reads the src into [FileDataChunkContent]s, and sends a valid sequence of
2519
// [FileDataChunk]s down the stream.
2620
func SendChunkedFile(
27-
meta *MessageMeta,
28-
header FileDataChunk_Header,
21+
meta *v1.MessageMeta,
22+
header v1.FileDataChunk_Header,
2923
src io.Reader,
30-
dst grpc.ServerStreamingServer[FileDataChunk],
24+
dst grpc.ServerStreamingServer[v1.FileDataChunk],
3125
) error {
3226
chunkCount := int(header.Header.GetChunks())
3327
chunkSize := int(header.Header.GetChunkSize())
3428
total := int(header.Header.GetFileMeta().GetSize())
3529
if chunkSize == 0 || chunkCount == 0 || total == 0 {
36-
return fmt.Errorf("%w: %v", ErrInvalidHeader, header.Header)
30+
return fmt.Errorf("zero in header: %+v", header.Header)
3731
}
38-
if err := dst.Send(&FileDataChunk{
32+
if err := dst.Send(&v1.FileDataChunk{
3933
Meta: meta,
4034
Chunk: &header,
4135
}); err != nil {
42-
return fmt.Errorf("%w: %s (header)", ErrSend, err)
36+
return fmt.Errorf("%w: send error (header)", err)
4337
}
4438
// allocate the buffer we need for reading from io.Reader
4539
// this is set to the size of the chunks we need to send.
@@ -54,18 +48,18 @@ func SendChunkedFile(
5448
total -= n
5549
if err != nil && total != 0 {
5650
// partial read
57-
return fmt.Errorf("%w: %s", ErrFailedRead, err)
51+
return fmt.Errorf("%w: failed read", err)
5852
}
59-
if err = dst.Send(&FileDataChunk{
53+
if err = dst.Send(&v1.FileDataChunk{
6054
Meta: meta,
61-
Chunk: &FileDataChunk_Content{
62-
Content: &FileDataChunkContent{
55+
Chunk: &v1.FileDataChunk_Content{
56+
Content: &v1.FileDataChunkContent{
6357
ChunkId: uint32(i),
6458
Data: buf[0:n],
6559
},
6660
},
6761
}); err != nil {
68-
return fmt.Errorf("%w: %s (content)", ErrSend, err)
62+
return fmt.Errorf("%w: send error (content)", err)
6963
}
7064
}
7165

@@ -75,19 +69,19 @@ func SendChunkedFile(
7569
// RecvChunkedFile receives [FileDataChunkContent]s from the stream and writes the file contents
7670
// to the dst.
7771
func RecvChunkedFile(
78-
src grpc.ServerStreamingClient[FileDataChunk],
72+
src grpc.ServerStreamingClient[v1.FileDataChunk],
7973
dst io.Writer,
80-
) (header *FileMeta, err error) {
74+
) (header *v1.FileMeta, err error) {
8175
// receive the header first
8276
chunk, err := src.Recv()
8377
if err != nil {
84-
return header, fmt.Errorf("%w: header error %s", ErrFailedRead, err)
78+
return header, fmt.Errorf("%w: header error", err)
8579
}
8680

8781
// validate and extract header info
8882
headerChunk := chunk.GetHeader()
8983
if headerChunk == nil {
90-
return header, fmt.Errorf("%w: invalid header chunk", ErrInvalidHeader)
84+
return header, errors.New("no header chunk")
9185
}
9286

9387
header = headerChunk.GetFileMeta()
@@ -96,14 +90,14 @@ func RecvChunkedFile(
9690
total := int(header.GetSize())
9791

9892
if chunkSize == 0 || chunkCount == 0 || total == 0 {
99-
return header, fmt.Errorf("%w: %v", ErrInvalidHeader, headerChunk)
93+
return header, fmt.Errorf("zero in header: %v", headerChunk)
10094
}
10195

10296
return header, recvContents(src, dst, chunkCount, chunkSize, total)
10397
}
10498

10599
func recvContents(
106-
src grpc.ServerStreamingClient[FileDataChunk],
100+
src grpc.ServerStreamingClient[v1.FileDataChunk],
107101
dst io.Writer,
108102
chunkCount int,
109103
chunkSize int,
@@ -113,43 +107,39 @@ func recvContents(
113107
for i := 0; i < chunkCount; i++ {
114108
chunk, err := src.Recv()
115109
if err != nil {
116-
return fmt.Errorf("%w: content error %s", ErrFailedRead, err)
110+
return fmt.Errorf("%w: failed to read content", err)
117111
}
118112

119113
if err = validateRecvChunk(chunk, chunkSize, chunkCount-1, i); err != nil {
120114
return err
121115
}
122116
data := chunk.GetContent().GetData()
123117
if _, err = dst.Write(data); err != nil {
124-
return fmt.Errorf("%w: %s", ErrWrite, err)
118+
return fmt.Errorf("%w: failed write", err)
125119
}
126120
totalSize -= len(data)
127121
if 0 > totalSize {
128-
return fmt.Errorf("%w: %d more data than expected",
129-
ErrUnexpectedContent, 0-totalSize)
122+
return fmt.Errorf("unexpected content: %d more data than expected", 0-totalSize)
130123
}
131124
}
132125
if totalSize > 0 {
133-
return fmt.Errorf("%w: unexpected end of content, %d left",
134-
ErrUnexpectedContent, totalSize)
126+
return fmt.Errorf("unexpected content: unexpected end of content, %d left", totalSize)
135127
}
136128

137129
return nil
138130
}
139131

140-
func validateRecvChunk(chunk *FileDataChunk, chunkSize, lastChunkIndex, i int) error {
132+
func validateRecvChunk(chunk *v1.FileDataChunk, chunkSize, lastChunkIndex, i int) error {
141133
content := chunk.GetContent()
142134
if content == nil {
143-
return fmt.Errorf("%w: no content", ErrUnexpectedContent)
135+
return fmt.Errorf("no content")
144136
}
145137
if content.GetChunkId() != uint32(i) {
146-
return fmt.Errorf("%w: unexpected chunk id %d, expected %d",
147-
ErrUnexpectedContent, content.GetChunkId(), i)
138+
return fmt.Errorf("unexpected chunk id %d, expected %d", content.GetChunkId(), i)
148139
}
149140
data := content.GetData()
150141
if len(data) != chunkSize && i != lastChunkIndex {
151-
return fmt.Errorf("%w: content chunk size %d, expected %d",
152-
ErrUnexpectedContent, len(data), chunkSize)
142+
return fmt.Errorf("content chunk size %d, expected %d", len(data), chunkSize)
153143
}
154144

155145
return nil

0 commit comments

Comments
 (0)