protondrive: stop a misbehaving server from making one events poll run forever
Read at most 100 pages of events per poll; the rest are read by the next poll. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b4fa0fc8cc
commit
623c3fee2d
2 files changed
+37
-3
No files matched your search
@@ -87,15 +87,19 @@ func (c *client) latestEventID(ctx context.Context) (string, error) {
|
||||
return resp.EventID, nil
|
||||
}
|
||||
|
||||
// maxEventPages is the most pages of events one poll reads; the rest
|
||||
// are read by the next poll.
|
||||
const maxEventPages = 100
|
||||
|
||||
// pollEvents returns the changes after event cursor, oldest first, and
|
||||
// the event ID to poll from next, reading every page there is.
|
||||
// the event ID to poll from next, reading up to maxEventPages pages.
|
||||
//
|
||||
// If the server no longer knows cursor it returns refresh set, no
|
||||
// changes and the newest event ID: anything cached may be out of date.
|
||||
// On error it returns no changes, and the poll should be retried from
|
||||
// cursor. It wraps errVolumeGone if the volume is gone.
|
||||
func (c *client) pollEvents(ctx context.Context, cursor string) (changes []eventChange, next string, refresh bool, err error) {
|
||||
for {
|
||||
for pages := 1; ; pages++ {
|
||||
opts := &rest.Opts{
|
||||
Method: "GET",
|
||||
Path: c.volumePath("/drive/v2/volumes", "events", cursor),
|
||||
@@ -118,7 +122,7 @@ func (c *client) pollEvents(ctx context.Context, cursor string) (changes []event
|
||||
if page.More && len(page.Events) > 0 && page.EventID == cursor {
|
||||
fs.Debugf(nil, "protondrive: events: page after %s doesn't advance: its events will be read again", cursor)
|
||||
}
|
||||
if !page.More || len(page.Events) == 0 || page.EventID == cursor {
|
||||
if !page.More || len(page.Events) == 0 || page.EventID == cursor || pages >= maxEventPages {
|
||||
return changes, page.EventID, false, nil
|
||||
}
|
||||
cursor = page.EventID
|
||||
|
||||
@@ -3,6 +3,7 @@ package protondrive
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
@@ -226,6 +227,35 @@ func TestPollEventsHostilePages(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPollEventsEndlessPages(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx := context.Background()
|
||||
srv := protontest.New(t)
|
||||
u := srv.AddUser("user", "pass")
|
||||
var pages int
|
||||
c := newTestClientTransport(t, srv, u, clientOptions{EnableCaching: true}, func(next http.RoundTripper) http.RoundTripper {
|
||||
return roundTripFunc(func(r *http.Request) (*http.Response, error) {
|
||||
if !strings.Contains(r.URL.Path, "/events/") || strings.HasSuffix(r.URL.Path, "/latest") {
|
||||
return next.RoundTrip(r)
|
||||
}
|
||||
pages++
|
||||
body := fmt.Sprintf(`{"Code":1000,"Events":[{"EventID":"e%d","EventType":2,"Link":{"LinkID":"a"}}],"EventID":"p%d","More":true}`, pages, pages)
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Header: http.Header{"Content-Type": {"application/json"}},
|
||||
Body: io.NopCloser(strings.NewReader(body)),
|
||||
Request: r,
|
||||
}, nil
|
||||
})
|
||||
})
|
||||
changes, next, refresh, err := c.pollEvents(ctx, "start")
|
||||
require.NoError(t, err)
|
||||
assert.False(t, refresh)
|
||||
assert.Equal(t, maxEventPages, pages)
|
||||
assert.Len(t, changes, maxEventPages)
|
||||
assert.Equal(t, fmt.Sprintf("p%d", maxEventPages), next)
|
||||
}
|
||||
|
||||
// eventsTestClient returns a client for no server whose cache holds
|
||||
// root/{dir/{sub/{deep}, file}, top, other/}, with every listing.
|
||||
func eventsTestClient() *client {
|
||||
|
||||
Reference in new issue
Block a user