Skip to content

Commit

Permalink
return context error if the context was canceled mid-way
Browse files Browse the repository at this point in the history
fixes #1850
  • Loading branch information
harshavardhana committed Jul 11, 2023
1 parent b27c007 commit d55f662
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
45 changes: 45 additions & 0 deletions api-list.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ func (c *Client) listObjectsV2(ctx context.Context, bucketName string, opts List
// Initiate list objects goroutine here.
go func(objectStatCh chan<- ObjectInfo) {
defer close(objectStatCh)

defer func() {
if contextCanceled(ctx) {
objectStatCh <- ObjectInfo{
Err: ctx.Err(),
}
}
}()

// Save continuationToken for next request.
var continuationToken string
for {
Expand Down Expand Up @@ -306,6 +315,14 @@ func (c *Client) listObjects(ctx context.Context, bucketName string, opts ListOb
go func(objectStatCh chan<- ObjectInfo) {
defer close(objectStatCh)

defer func() {
if contextCanceled(ctx) {
objectStatCh <- ObjectInfo{
Err: ctx.Err(),
}
}
}()

marker := opts.StartAfter
for {
// Get list of objects a maximum of 1000 per request.
Expand All @@ -321,6 +338,7 @@ func (c *Client) listObjects(ctx context.Context, bucketName string, opts ListOb
for _, object := range result.Contents {
// Save the marker.
marker = object.Key
object.ETag = trimEtag(object.ETag)
select {
// Send object content.
case objectStatCh <- object:
Expand Down Expand Up @@ -395,6 +413,14 @@ func (c *Client) listObjectVersions(ctx context.Context, bucketName string, opts
go func(resultCh chan<- ObjectInfo) {
defer close(resultCh)

defer func() {
if contextCanceled(ctx) {
resultCh <- ObjectInfo{
Err: ctx.Err(),
}
}
}()

var (
keyMarker = ""
versionIDMarker = ""
Expand Down Expand Up @@ -739,6 +765,16 @@ func (c *Client) ListIncompleteUploads(ctx context.Context, bucketName, objectPr
return c.listIncompleteUploads(ctx, bucketName, objectPrefix, recursive)
}

// contextCanceled returns whether a context is canceled.
func contextCanceled(ctx context.Context) bool {
select {
case <-ctx.Done():
return true
default:
return false
}
}

// listIncompleteUploads lists all incomplete uploads.
func (c *Client) listIncompleteUploads(ctx context.Context, bucketName, objectPrefix string, recursive bool) <-chan ObjectMultipartInfo {
// Allocate channel for multipart uploads.
Expand Down Expand Up @@ -767,6 +803,15 @@ func (c *Client) listIncompleteUploads(ctx context.Context, bucketName, objectPr
}
go func(objectMultipartStatCh chan<- ObjectMultipartInfo) {
defer close(objectMultipartStatCh)

defer func() {
if contextCanceled(ctx) {
objectMultipartStatCh <- ObjectMultipartInfo{
Err: ctx.Err(),
}
}
}()

// object and upload ID marker for future requests.
var objectMarker string
var uploadIDMarker string
Expand Down
3 changes: 0 additions & 3 deletions api-put-object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ func Test_SSEHeaders(t *testing.T) {
c, err := New("s3.amazonaws.com", &Options{
Transport: rt,
})

if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -147,7 +146,6 @@ func Test_SSEHeaders(t *testing.T) {
uploadID: "upId",
sse: opts.ServerSideEncryption,
})

if err != nil {
t.Error(err)
}
Expand All @@ -167,5 +165,4 @@ func Test_SSEHeaders(t *testing.T) {
}
})
}

}
12 changes: 8 additions & 4 deletions functional_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -2471,7 +2471,8 @@ func testTrailingChecksums() {
PO minio.PutObjectOptions
}{
// Currently there is no way to override the checksum type.
{header: "x-amz-checksum-crc32c",
{
header: "x-amz-checksum-crc32c",
hasher: crc32.New(crc32.MakeTable(crc32.Castagnoli)),
ChecksumCRC32C: "set",
PO: minio.PutObjectOptions{
Expand All @@ -2481,7 +2482,8 @@ func testTrailingChecksums() {
PartSize: 5 << 20,
},
},
{header: "x-amz-checksum-crc32c",
{
header: "x-amz-checksum-crc32c",
hasher: crc32.New(crc32.MakeTable(crc32.Castagnoli)),
ChecksumCRC32C: "set",
PO: minio.PutObjectOptions{
Expand All @@ -2491,7 +2493,8 @@ func testTrailingChecksums() {
PartSize: 6_645_654, // Rather arbitrary size
},
},
{header: "x-amz-checksum-crc32c",
{
header: "x-amz-checksum-crc32c",
hasher: crc32.New(crc32.MakeTable(crc32.Castagnoli)),
ChecksumCRC32C: "set",
PO: minio.PutObjectOptions{
Expand All @@ -2501,7 +2504,8 @@ func testTrailingChecksums() {
PartSize: 5 << 20,
},
},
{header: "x-amz-checksum-crc32c",
{
header: "x-amz-checksum-crc32c",
hasher: crc32.New(crc32.MakeTable(crc32.Castagnoli)),
ChecksumCRC32C: "set",
PO: minio.PutObjectOptions{
Expand Down

0 comments on commit d55f662

Please sign in to comment.