Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return context error if the context was canceled mid-way #1852

Merged
merged 1 commit into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 49 additions & 4 deletions api-list.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,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(),
}
}
close(objectStatCh)
}()

// Save continuationToken for next request.
var continuationToken string
for {
Expand Down Expand Up @@ -304,7 +312,14 @@ func (c *Client) listObjects(ctx context.Context, bucketName string, opts ListOb

// Initiate list objects goroutine here.
go func(objectStatCh chan<- ObjectInfo) {
defer close(objectStatCh)
defer func() {
if contextCanceled(ctx) {
objectStatCh <- ObjectInfo{
Err: ctx.Err(),
}
}
close(objectStatCh)
}()

marker := opts.StartAfter
for {
Expand All @@ -321,6 +336,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 @@ -393,7 +409,14 @@ func (c *Client) listObjectVersions(ctx context.Context, bucketName string, opts

// Initiate list objects goroutine here.
go func(resultCh chan<- ObjectInfo) {
defer close(resultCh)
defer func() {
if contextCanceled(ctx) {
resultCh <- ObjectInfo{
Err: ctx.Err(),
}
}
close(resultCh)
}()

var (
keyMarker = ""
Expand Down Expand Up @@ -699,6 +722,10 @@ func (o *ListObjectsOptions) Set(key, value string) {
// for object := range api.ListObjects(ctx, "mytestbucket", minio.ListObjectsOptions{Prefix: "starthere", Recursive:true}) {
// fmt.Println(object)
// }
//
// If caller cancels the context, then the last entry on the 'chan ObjectInfo' will be the context.Error()
// caller must drain the channel entirely and wait until channel is closed before proceeding, without
// waiting on the channel to be closed completely you might leak goroutines.
func (c *Client) ListObjects(ctx context.Context, bucketName string, opts ListObjectsOptions) <-chan ObjectInfo {
if opts.WithVersions {
return c.listObjectVersions(ctx, bucketName, opts)
Expand Down Expand Up @@ -739,6 +766,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 @@ -766,7 +803,15 @@ func (c *Client) listIncompleteUploads(ctx context.Context, bucketName, objectPr
return objectMultipartStatCh
}
go func(objectMultipartStatCh chan<- ObjectMultipartInfo) {
defer close(objectMultipartStatCh)
defer func() {
if contextCanceled(ctx) {
objectMultipartStatCh <- ObjectMultipartInfo{
Err: ctx.Err(),
}
}
close(objectMultipartStatCh)
}()

// 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