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
  • Loading branch information
harshavardhana committed Jul 10, 2023
1 parent 8be8154 commit f27fb81
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions api-list.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func (c *Client) listObjectsV2(ctx context.Context, bucketName string, opts List
// If contents are available loop through and send over channel.
for _, object := range result.Contents {
object.ETag = trimEtag(object.ETag)
object.Err = ctx.Err()
select {
// Send object content.
case objectStatCh <- object:
Expand All @@ -128,7 +129,7 @@ func (c *Client) listObjectsV2(ctx context.Context, bucketName string, opts List
for _, obj := range result.CommonPrefixes {
select {
// Send object prefixes.
case objectStatCh <- ObjectInfo{Key: obj.Prefix}:
case objectStatCh <- ObjectInfo{Key: obj.Prefix, Err: ctx.Err()}:
// If receives done from the caller, return here.
case <-ctx.Done():
return
Expand Down Expand Up @@ -268,6 +269,16 @@ func (c *Client) listObjectsV2Query(ctx context.Context, bucketName, objectPrefi
return listBucketResult, nil
}

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

func (c *Client) listObjects(ctx context.Context, bucketName string, opts ListObjectsOptions) <-chan ObjectInfo {
// Allocate new list objects channel.
objectStatCh := make(chan ObjectInfo, 1)
Expand Down Expand Up @@ -321,6 +332,8 @@ 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)
object.Err = ctx.Err()
select {
// Send object content.
case objectStatCh <- object:
Expand All @@ -335,7 +348,7 @@ func (c *Client) listObjects(ctx context.Context, bucketName string, opts ListOb
for _, obj := range result.CommonPrefixes {
select {
// Send object prefixes.
case objectStatCh <- ObjectInfo{Key: obj.Prefix}:
case objectStatCh <- ObjectInfo{Key: obj.Prefix, Err: ctx.Err()}:
// If receives done from the caller, return here.
case <-ctx.Done():
return
Expand Down Expand Up @@ -424,6 +437,7 @@ func (c *Client) listObjectVersions(ctx context.Context, bucketName string, opts
IsDeleteMarker: version.isDeleteMarker,
UserTags: version.UserTags,
UserMetadata: version.UserMetadata,
Err: ctx.Err(),
}
select {
// Send object version info.
Expand All @@ -439,7 +453,7 @@ func (c *Client) listObjectVersions(ctx context.Context, bucketName string, opts
for _, obj := range result.CommonPrefixes {
select {
// Send object prefixes.
case resultCh <- ObjectInfo{Key: obj.Prefix}:
case resultCh <- ObjectInfo{Key: obj.Prefix, Err: ctx.Err()}:
// If receives done from the caller, return here.
case <-ctx.Done():
return
Expand Down Expand Up @@ -783,6 +797,7 @@ func (c *Client) listIncompleteUploads(ctx context.Context, bucketName, objectPr

// Send all multipart uploads.
for _, obj := range result.Uploads {
obj.Err = ctx.Err()
// Calculate total size of the uploaded parts if 'aggregateSize' is enabled.
select {
// Send individual uploads here.
Expand All @@ -797,7 +812,7 @@ func (c *Client) listIncompleteUploads(ctx context.Context, bucketName, objectPr
for _, obj := range result.CommonPrefixes {
select {
// Send delimited prefixes here.
case objectMultipartStatCh <- ObjectMultipartInfo{Key: obj.Prefix, Size: 0}:
case objectMultipartStatCh <- ObjectMultipartInfo{Key: obj.Prefix, Size: 0, Err: ctx.Err()}:
// If context is canceled.
case <-ctx.Done():
return
Expand Down

0 comments on commit f27fb81

Please sign in to comment.