Skip to content

Commit

Permalink
Bug fix - middleware was being invoked twice.
Browse files Browse the repository at this point in the history
The client function is called in the let bindings. Successive
invocations of the client function were resulting in two HTTP requests.
  • Loading branch information
carld committed Apr 20, 2017
1 parent c11f48d commit 947bfd9
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions examples/caching_middleware.clj
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@
; do not invoke further middleware
(do
(println "CACHE MISS")
(let [resp (update (client req) :body slurp-bytes)]
(if (http/success? resp)
(do
(reset! http-cache (cache/miss @http-cache cache-key resp)) ; update cache value
(client req resp nil)) ; invoke next middleware
(do
(client req resp nil)))))))))
(let [resp (update (client req) :body slurp-bytes)] ; middleware chain invoked
(when (http/success? resp)
(reset! http-cache (cache/miss @http-cache cache-key resp)) ; update cache value
resp)))))))

(defn wrap-caching-middleware
[client]
Expand All @@ -45,7 +42,7 @@
;; :debug-body true
;; :throw-entire-message? true
})))
(select-keys ,,, [:status :reason-phrase])))
(select-keys ,,, [:status :reason-phrase :headers])))

;; Try this out:
;;
Expand Down

0 comments on commit 947bfd9

Please sign in to comment.