|
5 | 5 | [cmr.common-app.api.routes :as common-routes]
|
6 | 6 | [cmr.common-app.services.search :as search]
|
7 | 7 | [cmr.common.cache :as cache]
|
| 8 | + [cmr.common.config :refer [defconfig]] |
8 | 9 | [cmr.common.log :refer (debug info warn error)]
|
9 | 10 | [cmr.common.mime-types :as mt]
|
10 | 11 | [cmr.common.services.errors :as svc-errors]
|
|
47 | 48 | results (query-svc/find-concepts-by-json-query ctx concept-type params json-query)]
|
48 | 49 | (core-api/search-response ctx results)))
|
49 | 50 |
|
| 51 | +(defconfig block-queries |
| 52 | + "Indicates whether we are going to block a specific excessive query." |
| 53 | + {:type Boolean |
| 54 | + :default true}) |
| 55 | + |
| 56 | +(defn- block-excessive-queries |
| 57 | + "Temporary solution to prevent a specific query from overloading the CMR search resources." |
| 58 | + [ctx concept-type result-format params] |
| 59 | + (when (and (block-queries) |
| 60 | + (= concept-type :granule) |
| 61 | + (= :json result-format) |
| 62 | + (= "MCD43A4" (:short_name params)) |
| 63 | + (contains? params "")) |
| 64 | + (warn (format "Blocking %s query from client %s in format %s with params %s." |
| 65 | + (name concept-type) |
| 66 | + (:client-id ctx) |
| 67 | + (rfh/printable-result-format result-format) |
| 68 | + (pr-str params))) |
| 69 | + (svc-errors/throw-service-error |
| 70 | + :too-many-requests |
| 71 | + "Excessive query rate. Please contact [email protected]."))) |
| 72 | + |
50 | 73 | (defn- find-concepts-by-parameters
|
51 | 74 | "Invokes query service to parse the parameters query, find results, and
|
52 | 75 | return the response"
|
|
59 | 82 | ctx (assoc ctx :query-string body :scroll-id scroll-id)
|
60 | 83 | params (core-api/process-params concept-type params path-w-extension headers mt/xml)
|
61 | 84 | result-format (:result-format params)
|
| 85 | + _ (block-excessive-queries ctx concept-type result-format params) |
62 | 86 | _ (info (format "Searching for %ss from client %s in format %s with params %s."
|
63 | 87 | (name concept-type) (:client-id ctx)
|
64 | 88 | (rfh/printable-result-format result-format) (pr-str params)))
|
65 | 89 | search-params (if cached-search-params
|
66 | 90 | cached-search-params
|
67 | 91 | (lp/process-legacy-psa params))
|
68 | 92 | results (query-svc/find-concepts-by-parameters ctx concept-type search-params)]
|
69 |
| - (if (:scroll-id results) |
| 93 | + (if (:scroll-id results) |
70 | 94 | (core-api/search-response ctx results search-params)
|
71 | 95 | (core-api/search-response ctx results))))
|
72 | 96 |
|
|
0 commit comments