forked from parca-dev/parca
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.go
More file actions
99 lines (87 loc) · 2.54 KB
/
Copy pathreader.go
File metadata and controls
99 lines (87 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2022-2025 The Parca Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package debuginfo
import (
"bytes"
"context"
"fmt"
"io"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
debuginfopb "github.com/parca-dev/parca/gen/proto/go/parca/debuginfo/v1alpha1"
)
type UploadReader struct {
stream debuginfopb.DebuginfoService_UploadServer
cur io.Reader
size uint64
}
func (r *UploadReader) Read(p []byte) (int, error) {
if r.cur == nil {
var err error
r.cur, err = r.next()
if err == io.EOF {
return 0, io.EOF
}
if err != nil {
return 0, fmt.Errorf("get first upload chunk: %w", err)
}
}
i, err := r.cur.Read(p)
if err != nil && err != io.EOF {
return 0, fmt.Errorf("read upload chunk (%d bytes read so far): %w", r.size, err)
}
if err == io.EOF {
r.cur, err = r.next()
if err == io.EOF {
return 0, io.EOF
}
if err != nil {
return 0, fmt.Errorf("get next upload chunk (%d bytes read so far): %w", r.size, err)
}
i, err = r.cur.Read(p)
if err != nil {
return 0, fmt.Errorf("read next upload chunk (%d bytes read so far): %w", r.size, err)
}
}
r.size += uint64(i)
return i, nil
}
func (r *UploadReader) next() (io.Reader, error) {
err := contextError(r.stream.Context())
if err != nil {
return nil, err
}
req, err := r.stream.Recv()
if err == io.EOF {
return nil, io.EOF
}
if err != nil {
return nil, fmt.Errorf("receive from stream: %w", err)
}
return bytes.NewBuffer(req.GetChunkData()), nil
}
func contextError(ctx context.Context) error {
switch ctx.Err() {
case context.Canceled:
return status.Error(codes.Canceled, "request is canceled")
case context.DeadlineExceeded:
return status.Error(codes.DeadlineExceeded, "deadline is exceeded")
default:
return nil
}
}
// ObjectSize implements the objstore.ObjectSizer interface to provide the total size of bytes read so far.
// This is used by objstore.TryToGetSize to optimize multipart uploads by pre-allocating the correct buffer size.
func (r *UploadReader) ObjectSize() (int64, error) {
return int64(r.size), nil
}