G'day,

Ring ships with some development middleware that reloads the supplied
namespaces every request. This is fantastic for quick iterative
development, particularly because the JVM takes so long to start.
However as soon as you have more than a few namespaces every request
becomes rather slow - even if there are no source changes the
middleware reloads all supplied namespaces. I have tweaked the ring
reload middleware to only reload the namespaces if at least one file
below the supplied directory has been modified:


(defonce src-mtime (atom 0))

(defn read-src-mtime [dir]
  (reduce
   (fn [a b]
     (+ a (.lastModified b))) 0 (file-seq dir)))

;; Copied and modified from ring.middleware.reload
(defn wrap-reload
  "Wrap an app such that before a request is passed to the app, each
  namespace identified by syms in reloadables is reloaded if any file
  has changed below the identified source directory. Currently this
  requires that the namespaces in question are being (re)loaded from
  un-jarred source files, as apposed to source files in jars or
  compiled classes."
  [app dir reloadables]
  (fn [req]
    (let [new-mtime (read-src-mtime dir)]
      (if (> new-mtime @src-mtime)
        (do
          (swap! src-mtime (constantly new-mtime))
          (doseq [ns-sym reloadables]
            (require ns-sym :reload)))))
    (app req)))

There are two ways in which I can immediately see that this could be improved:

1. Don't pass in src directory as well as namespaces. If the user just
specifies the src directory then we can infer all of the namespaces?
2. Don't reload all namespaces when a file is changed - only the
affected namespaces. I think this can become tricky.

Anyway the current implementation works well for me and I thought it
might benefit others. Any feedback welcome.

Can I provide a patch or pull request for ring?


Cheers,
David

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to