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

Try Getting Time from Server for List Calls #567

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions storage/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ func (s *S3) listObjectsV2(ctx context.Context, url *url.URL) <-chan *Object {
go func() {
defer close(objCh)
objectFound := false

var now time.Time
var serverDate string

err := s.api.ListObjectsV2PagesWithContext(ctx, &listInput, func(p *s3.ListObjectsV2Output, lastPage bool) bool {
for _, c := range p.CommonPrefixes {
Expand All @@ -330,10 +330,20 @@ func (s *S3) listObjectsV2(ctx context.Context, url *url.URL) <-chan *Object {

objectFound = true
}

// track the instant object iteration began,
// so it can be used to bypass objects created after this instant
if now.IsZero() {
now = time.Now().UTC()
if serverDate != "" {
n, err := http.ParseTime(serverDate)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

http.ParseTime function is not able to accurately represent the last modified time of an S3 object, because the latter has a much higher precision. This is causing the tests to fail.

For example:

  • Parsed time in header : 2023-07-01 19:43:58 +0000 UTC
  • Last Modified time of S3 object: 2023-07-01 19:43:58.058 +0000 UTC

I will let you know if I am able to find a solution to the time comparison issue.

if err != nil {
now = time.Now().UTC()
} else {
now = n
}
} else {
now = time.Now().UTC()
}
}

for _, c := range p.Contents {
Expand Down Expand Up @@ -370,7 +380,7 @@ func (s *S3) listObjectsV2(ctx context.Context, url *url.URL) <-chan *Object {
}

return !lastPage
})
}, request.WithGetResponseHeader("date", &serverDate))

if err != nil {
objCh <- &Object{Err: err}
Expand Down
30 changes: 30 additions & 0 deletions storage/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,13 @@ func TestS3ListURL(t *testing.T) {
}

mockApi.Handlers.Send.Clear()
mockApi.Handlers.Send.PushBack(func(r *request.Request) {
header := http.Header{}
r.HTTPResponse = &http.Response{
StatusCode: http.StatusOK,
Header: header,
}
})
mockApi.Handlers.Unmarshal.Clear()
mockApi.Handlers.UnmarshalMeta.Clear()
mockApi.Handlers.ValidateResponse.Clear()
Expand Down Expand Up @@ -329,6 +336,13 @@ func TestS3ListNoItemFound(t *testing.T) {
}

mockApi.Handlers.Send.Clear()
mockApi.Handlers.Send.PushBack(func(r *request.Request) {
header := http.Header{}
r.HTTPResponse = &http.Response{
StatusCode: http.StatusOK,
Header: header,
}
})
mockApi.Handlers.Unmarshal.Clear()
mockApi.Handlers.UnmarshalMeta.Clear()
mockApi.Handlers.ValidateResponse.Clear()
Expand Down Expand Up @@ -367,6 +381,14 @@ func TestS3ListContextCancelled(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()

mockApi.Handlers.Complete.Clear()
mockApi.Handlers.Complete.PushBack(func(r *request.Request) {
header := http.Header{}
r.HTTPResponse = &http.Response{
StatusCode: http.StatusOK,
Header: header,
}
})
mockApi.Handlers.Unmarshal.Clear()
mockApi.Handlers.UnmarshalMeta.Clear()
mockApi.Handlers.ValidateResponse.Clear()
Expand Down Expand Up @@ -1164,6 +1186,14 @@ func TestS3ListObjectsAPIVersions(t *testing.T) {
mockS3 := &S3{api: mockApi}

mockApi.Handlers.Send.Clear()
mockApi.Handlers.Send.PushBack(func(r *request.Request) {
header := http.Header{}
header.Set("Date", "Fri, 12 May 2023 20:36:22 GMT")
r.HTTPResponse = &http.Response{
StatusCode: http.StatusOK,
Header: header,
}
})
mockApi.Handlers.Unmarshal.Clear()
mockApi.Handlers.UnmarshalMeta.Clear()
mockApi.Handlers.ValidateResponse.Clear()
Expand Down