From 0cb708dc1972e1a7966492da26c1d27061cb3360 Mon Sep 17 00:00:00 2001 From: Lukas Eklund Date: Thu, 1 Dec 2022 11:42:57 -0600 Subject: [PATCH] Reduce load on service endpoint by excluding versions from the response (#119) * Add filter to exclude versions from the service endpoint * fix flaky test that relied on the managed service to be sorted --- pkg/api/link.go | 10 ++++++++-- pkg/api/link_test.go | 6 ++++++ pkg/api/service_cache.go | 2 +- pkg/rt/manager_test.go | 9 +++++++-- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/pkg/api/link.go b/pkg/api/link.go index c864da3..f9772b8 100644 --- a/pkg/api/link.go +++ b/pkg/api/link.go @@ -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) { diff --git a/pkg/api/link_test.go b/pkg/api/link_test.go index 48c4847..3ab4e54 100644 --- a/pkg/api/link_test.go +++ b/pkg/api/link_test.go @@ -135,6 +135,12 @@ func TestGetNextLink(t *testing.T) { links: []string{`; 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{`; 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 ( diff --git a/pkg/api/service_cache.go b/pkg/api/service_cache.go index d039045..273d81e 100644 --- a/pkg/api/service_cache.go +++ b/pkg/api/service_cache.go @@ -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{} ) diff --git a/pkg/rt/manager_test.go b/pkg/rt/manager_test.go index c1ecc67..88ba4c5 100644 --- a/pkg/rt/manager_test.go +++ b/pkg/rt/manager_test.go @@ -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`, @@ -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)) } }