6
6
"fmt"
7
7
"io"
8
8
"strings"
9
+ "time"
9
10
10
11
remoteexecution "github.com/bitrise-io/bitrise-build-cache-cli/proto/build/bazel/remote/execution/v2"
11
12
"github.com/dustin/go-humanize"
@@ -25,22 +26,25 @@ type FileDigest struct {
25
26
}
26
27
27
28
func (c * Client ) GetCapabilities (ctx context.Context ) error {
28
- ctx = metadata .NewOutgoingContext (ctx , c .getMethodCallMetadata ())
29
+ timeoutCtx , cancel := context .WithTimeout (ctx , 10 * time .Second )
30
+ defer cancel ()
31
+ callCtx := metadata .NewOutgoingContext (timeoutCtx , c .getMethodCallMetadata ())
29
32
30
- _ , err := c .capabilitiesClient .GetCapabilities (ctx , & remoteexecution.GetCapabilitiesRequest {})
33
+ _ , err := c .capabilitiesClient .GetCapabilities (callCtx , & remoteexecution.GetCapabilitiesRequest {})
31
34
if err != nil {
32
35
return fmt .Errorf ("get capabilities: %w" , err )
33
36
}
34
37
35
38
return nil
36
39
}
37
40
38
- func (c * Client ) Put (ctx context.Context , params PutParams ) (io.WriteCloser , error ) {
41
+ func (c * Client ) InitiatePut (ctx context.Context , params PutParams ) (io.WriteCloser , error ) {
39
42
md := metadata .Join (c .getMethodCallMetadata (), metadata .Pairs (
40
43
"x-flare-blob-validation-sha256" , params .Sha256Sum ,
41
44
"x-flare-blob-validation-level" , "error" ,
42
45
"x-flare-no-skip-duplicate-writes" , "true" ,
43
46
))
47
+ // Timeout is the responsibility of the caller
44
48
ctx = metadata .NewOutgoingContext (ctx , md )
45
49
46
50
stream , err := c .bitriseKVClient .Put (ctx )
@@ -58,9 +62,10 @@ func (c *Client) Put(ctx context.Context, params PutParams) (io.WriteCloser, err
58
62
}, nil
59
63
}
60
64
61
- func (c * Client ) Get (ctx context.Context , name string ) (io.ReadCloser , error ) {
65
+ func (c * Client ) InitiateGet (ctx context.Context , name string ) (io.ReadCloser , error ) {
62
66
resourceName := fmt .Sprintf ("kv/%s" , name )
63
67
68
+ // Timeout is the responsibility of the caller
64
69
ctx = metadata .NewOutgoingContext (ctx , c .getMethodCallMetadata ())
65
70
66
71
readReq := & bytestream.ReadRequest {
@@ -82,14 +87,16 @@ func (c *Client) Get(ctx context.Context, name string) (io.ReadCloser, error) {
82
87
func (c * Client ) Delete (ctx context.Context , name string ) error {
83
88
resourceName := fmt .Sprintf ("kv/%s" , name )
84
89
85
- ctx = metadata .NewOutgoingContext (ctx , c .getMethodCallMetadata ())
90
+ timeoutCtx , cancel := context .WithTimeout (ctx , 10 * time .Second )
91
+ defer cancel ()
92
+ callCtx := metadata .NewOutgoingContext (timeoutCtx , c .getMethodCallMetadata ())
86
93
87
94
readReq := & bytestream.ReadRequest {
88
95
ResourceName : resourceName ,
89
96
ReadOffset : 0 ,
90
97
ReadLimit : 0 ,
91
98
}
92
- _ , err := c .bitriseKVClient .Delete (ctx , readReq )
99
+ _ , err := c .bitriseKVClient .Delete (callCtx , readReq )
93
100
if err != nil {
94
101
return fmt .Errorf ("initiate delete: %w" , err )
95
102
}
@@ -98,8 +105,6 @@ func (c *Client) Delete(ctx context.Context, name string) error {
98
105
}
99
106
100
107
func (c * Client ) FindMissing (ctx context.Context , digests []* FileDigest ) ([]* FileDigest , error ) {
101
- ctx = metadata .NewOutgoingContext (ctx , c .getMethodCallMetadata ())
102
-
103
108
var missingBlobs []* FileDigest
104
109
blobDigests := convertToBlobDigests (digests )
105
110
req := & remoteexecution.FindMissingBlobsRequest {
@@ -119,16 +124,25 @@ func (c *Client) FindMissing(ctx context.Context, digests []*FileDigest) ([]*Fil
119
124
}
120
125
req .BlobDigests = blobDigests [startIndex :endIndex ]
121
126
127
+ timeoutCtx , cancel := context .WithTimeout (ctx , 20 * time .Second )
128
+ callCtx := metadata .NewOutgoingContext (timeoutCtx , c .getMethodCallMetadata ())
129
+
122
130
c .logger .Debugf ("Calling FindMissingBlobs for chunk: digests[%d:%d]" , startIndex , endIndex )
123
- resp , err := c .casClient .FindMissingBlobs (ctx , req )
131
+ resp , err := c .casClient .FindMissingBlobs (callCtx , req )
124
132
133
+ cancel ()
125
134
if err != nil {
126
135
return nil , fmt .Errorf ("find missing blobs[%d:%d]: %w" , startIndex , endIndex , err )
127
136
}
128
137
missingBlobs = append (missingBlobs , convertToFileDigests (resp .GetMissingBlobDigests ())... )
129
138
}
130
139
} else {
131
- resp , err := c .casClient .FindMissingBlobs (ctx , req )
140
+ timeoutCtx , cancel := context .WithTimeout (ctx , 20 * time .Second )
141
+ callCtx := metadata .NewOutgoingContext (timeoutCtx , c .getMethodCallMetadata ())
142
+
143
+ resp , err := c .casClient .FindMissingBlobs (callCtx , req )
144
+
145
+ cancel ()
132
146
133
147
if err != nil {
134
148
return nil , fmt .Errorf ("find missing blobs: %w" , err )
0 commit comments