Skip to content

Commit 7902110

Browse files
committed
[Fix #226] workaround for search.maven.org requests throttling
1 parent b7f1528 commit 7902110

File tree

2 files changed

+49
-51
lines changed

2 files changed

+49
-51
lines changed

src/refactor_nrepl/artifacts.clj

Lines changed: 42 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@
2525
:proxy-port (some->> ["https.proxyPort" "http.proxyPort"]
2626
(some #(System/getProperty %)) Integer/parseInt)}))
2727

28-
(defn get-artifacts-from-clojars!
28+
(defn- stale-cache?
29+
[]
30+
(or (empty? @artifacts)
31+
(if-let [last-modified (some-> artifacts meta :last-modified .getTime)]
32+
(neg? (- millis-per-day (- (.getTime (java.util.Date.)) last-modified)))
33+
true)))
34+
35+
(defn get-clojars-artifacts!
2936
"Returns a vector of [[some/lib \"0.1\"]...]."
3037
[]
3138
(try
@@ -38,17 +45,6 @@
3845
;; In the event clojars is down just return an empty vector. See #136.
3946
[])))
4047

41-
(defn add-artifacts-from-clojars! []
42-
(->> (get-artifacts-from-clojars!)
43-
(map #(swap! artifacts update-in [(str (first %))] conj (second %)))
44-
dorun))
45-
46-
(defn- stale-cache? []
47-
(or (empty? @artifacts)
48-
(if-let [last-modified (some-> artifacts meta :last-modified .getTime)]
49-
(neg? (- millis-per-day (- (.getTime (java.util.Date.)) last-modified)))
50-
true)))
51-
5248
(defn- get-mvn-artifacts!
5349
"All the artifacts under org.clojure in mvn central"
5450
[group-id]
@@ -59,58 +55,57 @@
5955
search-result (json/parse-string body true)]
6056
(map :a (-> search-result :response :docs))))
6157

62-
(defn- get-versions!
63-
"Gets all the versions from an artifact belonging to the org.clojure."
64-
[group-id artifact]
65-
(let [search-prefix "http://search.maven.org/solrsearch/select?q=g:%22"
58+
(defn- get-mvn-versions!
59+
"Fetches all the versions of particular artifact from maven repository."
60+
[for-artifact]
61+
(let [[group-id artifact] (str/split for-artifact #"/")
62+
search-prefix "http://search.maven.org/solrsearch/select?q=g:%22"
6663
{:keys [_ _ body _]} @(http/get (str search-prefix
6764
group-id
6865
"%22+AND+a:%22"
6966
artifact
70-
"%22&core=gav&rows=200&wt=json")
67+
"%22&core=gav&rows=100&wt=json")
7168
(assoc (get-proxy-opts) :as :text))]
7269
(->> (json/parse-string body true)
7370
:response
7471
:docs
75-
(map :v)
76-
doall)))
77-
78-
(defn- collate-artifact-and-versions [group-id artifact]
79-
(->> artifact
80-
(get-versions! group-id)
81-
(vector (str group-id "/" artifact))))
82-
83-
(defn- add-artifact [[artifact versions]]
84-
(swap! artifacts update-in [artifact] (constantly versions)))
72+
(map :v))))
8573

86-
(defn- add-artifacts [group-id artifacts]
87-
(->> artifacts
88-
(partition-all 2)
89-
(map #(future (->> %
90-
(map (partial collate-artifact-and-versions group-id))
91-
(map add-artifact)
92-
dorun)))))
93-
94-
(defn- get-artifacts-from-mvn-central! []
74+
(defn- get-artifacts-from-mvn-central!
75+
[]
9576
(let [group-ids #{"com.cognitect" "org.clojure"}]
96-
(mapcat (fn [group-id] (add-artifacts group-id (get-mvn-artifacts! group-id)))
77+
(mapcat (fn [group-id]
78+
(->> (get-mvn-artifacts! group-id)
79+
(map #(vector (str group-id "/" %) nil))))
9780
group-ids)))
9881

99-
(defn- update-artifact-cache! []
100-
(let [mvn-central-futures (get-artifacts-from-mvn-central!)
101-
clojars-future (future (add-artifacts-from-clojars!))]
102-
(dorun (map deref mvn-central-futures))
103-
@clojars-future)
104-
(alter-meta! artifacts update-in [:last-modified]
105-
(constantly (java.util.Date.))))
82+
(defn- get-artifacts-from-clojars!
83+
[]
84+
(reduce #(update %1 (str (first %2)) conj (second %2))
85+
(sorted-map)
86+
(get-clojars-artifacts!)))
87+
88+
(defn- update-artifact-cache!
89+
[]
90+
(let [clojars-artifacts (future (get-artifacts-from-clojars!))
91+
maven-artifacts (future (get-artifacts-from-mvn-central!))]
92+
(reset! artifacts (into @clojars-artifacts @maven-artifacts))
93+
(alter-meta! artifacts update-in [:last-modified] (constantly (java.util.Date.)))))
10694

107-
(defn artifact-list [{:keys [force]}]
95+
(defn artifact-list
96+
[{:keys [force]}]
10897
(when (or (= force "true") (stale-cache?))
10998
(update-artifact-cache!))
11099
(->> @artifacts keys list*))
111100

112-
(defn artifact-versions [{:keys [artifact]}]
113-
(->> artifact (get @artifacts) distinct versions/version-sort reverse list*))
101+
(defn artifact-versions
102+
[{:keys [artifact]}]
103+
(->> (or (get @artifacts artifact)
104+
(get-mvn-versions! artifact))
105+
distinct
106+
versions/version-sort
107+
reverse
108+
list*))
114109

115110
(defn- make-resolve-missing-aware-of-new-deps
116111
"Once the deps are available on cp we still have to load them and

test/refactor_nrepl/artifacts_test.clj

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,22 @@
2020
(deftest creates-a-map-of-artifacts
2121
(reset! artifacts/artifacts {})
2222
(with-redefs
23-
[artifacts/get-artifacts-from-clojars! (constantly clojars-artifacts)
23+
[artifacts/get-clojars-artifacts! (constantly clojars-artifacts)
2424
artifacts/get-mvn-artifacts! (constantly clojure-artifacts)
25-
artifacts/get-versions! (constantly clojure-versions)]
25+
artifacts/get-mvn-versions! (constantly clojure-versions)]
2626

2727
(is (#'artifacts/stale-cache?))
2828

2929
(#'artifacts/update-artifact-cache!)
3030

3131
(is (not (#'artifacts/stale-cache?)))
3232

33-
(testing "Contains clojure with correct versions"
33+
(testing "Contains no maven-based dependency versions fetched upfront"
3434
(is (contains? @artifacts/artifacts "org.clojure/clojure"))
35-
(is (= (count (@artifacts/artifacts "org.clojure/clojure"))
35+
(is (= 0 (count (@artifacts/artifacts "org.clojure/clojure")))))
36+
37+
(testing "Fetches versions of maven dependency when requested"
38+
(is (= (count (artifacts/artifact-versions {:artifact "org.clojure/clojure"}))
3639
(count clojure-versions))))
3740

3841
(testing "Contains artifacts from clojars"

0 commit comments

Comments
 (0)