Skip to content

Commit

Permalink
Reduce load on service endpoint by excluding versions from the respon…
Browse files Browse the repository at this point in the history
…se (#119)

* Add filter to exclude versions from the service endpoint

* fix flaky test that relied on the managed service to be sorted
  • Loading branch information
leklund authored Dec 1, 2022
1 parent d4ec0f6 commit 0cb708d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
10 changes: 8 additions & 2 deletions pkg/api/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@ func GetNextLink(resp *http.Response) (*url.URL, error) {
return nil, fmt.Errorf(`rel="next": no match`)
}

unescapedURI, err := url.QueryUnescape(rawuri)
// if QueryUnescape() returns an error, just use the rawuri
if err != nil {
unescapedURI = rawuri
}

if resp.Request != nil && resp.Request.URL != nil {
return resp.Request.URL.Parse(rawuri)
return resp.Request.URL.Parse(unescapedURI)
}

return url.Parse(rawuri)
return url.Parse(unescapedURI)
}

func uriFromLinks(links []string, k, v string) (string, bool) {
Expand Down
6 changes: 6 additions & 0 deletions pkg/api/link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ func TestGetNextLink(t *testing.T) {
links: []string{`<abc/def?page=2>; rel="next"; datetime="Mon, 03 Sep 2007 14:52:48 GMT"`},
want: `https://zombo.com/retained/path/including_last_element/abc/def?page=2`,
},
{
name: `rel 5 with []`,
req: `https://zombo.com/with_brackets?filter[foo]=bar`,
links: []string{`</with_brackets?filter%5Bfoo%5D=bar&page=2>; rel="next"; datetime="Mon, 03 Sep 2007 14:52:48 GMT"`},
want: `https://zombo.com/with_brackets?filter[foo]=bar&page=2`,
},
} {
t.Run(testcase.name, func(t *testing.T) {
var (
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/service_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (c *ServiceCache) Refresh(ctx context.Context) error {
begin := time.Now()

var (
uri = fmt.Sprintf("https://api.fastly.com/service?page=1&per_page=%d", maxServicePageSize)
uri = fmt.Sprintf("https://api.fastly.com/service?page=1&per_page=%d&filter%%5Binclude_versions%%5D=false", maxServicePageSize)
total = 0
nextgen = map[string]Service{}
)
Expand Down
9 changes: 7 additions & 2 deletions pkg/rt/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestManager(t *testing.T) {
manager.StopAll() // stop s1
assertStringSliceEqual(t, []string{}, sortedServiceIDs(manager))

if want, have := []string{
want := []string{
`level=info service_id=101010 type=default subscriber=create`,
`level=info service_id=2f2f2f type=default subscriber=create`,
`level=info service_id=3a3b3c type=default subscriber=create`,
Expand All @@ -89,7 +89,12 @@ func TestManager(t *testing.T) {
`level=info service_id=101010 type=origin_inspector subscriber=create`,
`level=info service_id=101010 type=default subscriber=stop`,
`level=info service_id=101010 type=origin_inspector subscriber=stop`,
}, strings.Split(strings.TrimSpace(logbuf.String()), "\n"); !cmp.Equal(want, have) {
}
have := strings.Split(strings.TrimSpace(logbuf.String()), "\n")
sort.Strings(want)
sort.Strings(have)

if !cmp.Equal(want, have) {
t.Error(cmp.Diff(want, have))
}
}
Expand Down

0 comments on commit 0cb708d

Please sign in to comment.