Skip to content

Commit

Permalink
fix(storage/chunk/client/aws): have GetObject check for canceled cont…
Browse files Browse the repository at this point in the history
…ext (backport k223) (#14423)

Co-authored-by: Robert Fratto <robertfratto@gmail.com>
  • Loading branch information
loki-gh-app[bot] and rfratto authored Oct 8, 2024
1 parent c2f38e1 commit 901525e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/storage/chunk/client/aws/s3_storage_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ func (a *S3ObjectClient) GetObject(ctx context.Context, objectKey string) (io.Re
// Map the key into a bucket
bucket := a.bucketFromKey(objectKey)

var lastErr error
lastErr := ctx.Err()

retries := backoff.New(ctx, a.cfg.BackoffConfig)
for retries.Ongoing() {
Expand Down
25 changes: 25 additions & 0 deletions pkg/storage/chunk/client/aws/s3_storage_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,31 @@ func TestRequestMiddleware(t *testing.T) {
}
}

func TestS3ObjectClient_GetObject_CanceledContext(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, r.Header.Get("echo-me"))
}))
defer ts.Close()

cfg := S3Config{
Endpoint: ts.URL,
BucketNames: "buck-o",
S3ForcePathStyle: true,
Insecure: true,
AccessKeyID: "key",
SecretAccessKey: flagext.SecretWithValue("secret"),
}

client, err := NewS3ObjectClient(cfg, hedging.Config{})
require.NoError(t, err)

ctx, cancel := context.WithCancel(context.Background())
cancel()

_, _, err = client.GetObject(ctx, "key")
require.Error(t, err, "GetObject should fail when given a canceled context")
}

func Test_Hedging(t *testing.T) {
for _, tc := range []struct {
name string
Expand Down

0 comments on commit 901525e

Please sign in to comment.