summaryrefslogtreecommitdiff
path: root/lib/chef/search
diff options
context:
space:
mode:
authorSteven Danna <steve@chef.io>2017-07-31 01:13:38 +0100
committerSteven Danna <steve@chef.io>2017-07-31 09:10:21 +0100
commit5b4f08e1c6fc83a12b896b3949676ceff6436f52 (patch)
tree17f3162e94ab68a4c45a2f65deeda48bbebef4c9 /lib/chef/search
parent82b3157c671835c00bcf68772c8b3563e5db6e26 (diff)
downloadchef-5b4f08e1c6fc83a12b896b3949676ceff6436f52.tar.gz
Set explicit page size for every search requestssd/always-forward
The previous code would advance the search start parameter by either the user-provided rows parameter or (as a fallback) the number of rows returned in that page. Most code that calls the search() function does not set rows, so the fallback is used often. Since the search index may have stale results, it is possible to get responses with a completely empty page. For example: { "start": 0, "total": 1, "rows": [] } In this case, if no rows parameter was passed, the search function would infinitely recurse (eventually running out of stack). To avoid this, we set rows to 1000 if no user-provided value was set. This is the default page size of Chef Server so for most users it will not be changing the default behavior. Signed-off-by: Steven Danna <steve@chef.io>
Diffstat (limited to 'lib/chef/search')
-rw-r--r--lib/chef/search/query.rb7
1 files changed, 6 insertions, 1 deletions
diff --git a/lib/chef/search/query.rb b/lib/chef/search/query.rb
index 6f494819ba..a2663d111d 100644
--- a/lib/chef/search/query.rb
+++ b/lib/chef/search/query.rb
@@ -71,6 +71,11 @@ class Chef
args_h = args_h.reject { |k, v| k == :fuzz }
end
+ # Set default rows parameter to 1000. This is the default in
+ # Chef Server, but we set it explicitly here so that we can
+ # confidently advance our start parameter.
+ args_h[:rows] ||= 1000
+
response = call_rest_service(type, query: query, **args_h)
if block
@@ -87,7 +92,7 @@ class Chef
# args_h[:rows] to avoid asking the search backend for
# overlapping pages (which could result in duplicates).
#
- next_start = response["start"] + (args_h[:rows] || response["rows"].length)
+ next_start = response["start"] + args_h[:rows]
unless next_start >= response["total"]
args_h[:start] = next_start
search(type, query, args_h, &block)