Skip to content

Commit

Permalink
Add support for manually setting a limit on results
Browse files Browse the repository at this point in the history
* Added a query parameter to allow user to specify max results, rather
  than a hard limit at 5000, for each index.
* Added support for a default max result limit in the config.  If unset,
  it remains at the former default of 5000 results per index.
* Fixed issue where no results would be returned if the limit was
  exceeded - now it simply returns the results up to the limit.
  • Loading branch information
AlexTalks authored and gmcaguilar committed May 27, 2023
1 parent 6b38958 commit 81a61c9
Show file tree
Hide file tree
Showing 8 changed files with 1,522 additions and 9 deletions.
25 changes: 22 additions & 3 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
const (
defaultLinesOfContext uint = 2
maxLinesOfContext uint = 20
maxLimit int = 100000
)

type Stats struct {
Expand Down Expand Up @@ -128,11 +129,25 @@ func parseAsUintValue(sv string, min, max, def uint) uint {
return max
}
if min != 0 && uint(iv) < min {
return max
return min
}
return uint(iv)
}

func parseAsIntValue(sv string, min, max, def int) int {
iv, err := strconv.ParseInt(sv, 10, 64)
if err != nil {
return def
}
if max != 0 && int(iv) > max {
return max
}
if min != 0 && int(iv) < min {
return min
}
return int(iv)
}

func parseRangeInt(v string, i *int) {
*i = 0
if v == "" {
Expand All @@ -159,8 +174,7 @@ func parseRangeValue(rv string) (int, int) {
return b, e
}

func Setup(m *http.ServeMux, idx map[string]*searcher.Searcher) {

func Setup(m *http.ServeMux, idx map[string]*searcher.Searcher, defaultMaxResults int) {
m.HandleFunc("/api/v1/repos", func(w http.ResponseWriter, r *http.Request) {
res := map[string]*config.Repo{}
for name, srch := range idx {
Expand All @@ -181,6 +195,11 @@ func Setup(m *http.ServeMux, idx map[string]*searcher.Searcher) {
opt.ExcludeFileRegexp = r.FormValue("excludeFiles")
opt.IgnoreCase = parseAsBool(r.FormValue("i"))
opt.LiteralSearch = parseAsBool(r.FormValue("literal"))
opt.MaxResults = parseAsIntValue(
r.FormValue("limit"),
-1,
maxLimit,
defaultMaxResults)
opt.LinesOfContext = parseAsUintValue(
r.FormValue("ctx"),
0,
Expand Down
2 changes: 1 addition & 1 deletion cmds/houndd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func runHttp( //nolint
}

m.Handle("/", h)
api.Setup(m, idx)
api.Setup(m, idx, cfg.ResultLimit)
return http.ListenAndServe(addr, m)
}

Expand Down
6 changes: 6 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
defaultBaseUrl = "{url}/blob/{rev}/{path}{anchor}"
defaultAnchor = "#L{line}"
defaultHealthCheckURI = "/healthz"
defaultResultLimit = 5000
)

type UrlPattern struct {
Expand Down Expand Up @@ -63,6 +64,7 @@ type Config struct {
MaxConcurrentIndexers int `json:"max-concurrent-indexers"`
HealthCheckURI string `json:"health-check-uri"`
VCSConfigMessages map[string]*SecretMessage `json:"vcs-config"`
ResultLimit int `json:"result-limit"`
}

// SecretMessage is just like json.RawMessage but it will not
Expand Down Expand Up @@ -130,6 +132,10 @@ func initConfig(c *Config) error {
c.HealthCheckURI = defaultHealthCheckURI
}

if c.ResultLimit == 0 {
c.ResultLimit = defaultResultLimit
}

return mergeVCSConfigs(c)
}

Expand Down
Loading

0 comments on commit 81a61c9

Please sign in to comment.