Skip to content

Commit c8c10be

Browse files
committed
Don't share SimpleDateFormat objects
They aren't thread-safe!
1 parent e81b71e commit c8c10be

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/clojars/web/common.clj

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -337,22 +337,26 @@
337337
(defn group-link [groupname]
338338
(link-to (str "/groups/" groupname) groupname))
339339

340+
;; Note: this uses a function to generate the formatter instead of using a
341+
;; static formatter object because SimpleDateFormat objects can't be shared
342+
;; across threads!
340343
(defn- format-date*
341-
[^SimpleDateFormat formatter v]
344+
[make-formatter v]
342345
(when v
343-
(.format formatter v)))
346+
(let [^SimpleDateFormat formatter (make-formatter)]
347+
(.format formatter v))))
344348

345349
(def format-date
346-
(partial format-date* (SimpleDateFormat. "yyyy-MM-dd")))
350+
(partial format-date* #(SimpleDateFormat. "yyyy-MM-dd")))
347351

348352
(def simple-date
349-
(partial format-date* (SimpleDateFormat. "MMM d, yyyy")))
353+
(partial format-date* #(SimpleDateFormat. "MMM d, yyyy")))
350354

351355
(def format-timestamp
352-
(partial format-date* (SimpleDateFormat. "MMM d, yyyy HH:mm:ss Z")))
356+
(partial format-date* #(SimpleDateFormat. "MMM d, yyyy HH:mm:ss Z")))
353357

354358
(def format-date-with-time
355-
(partial format-date* (SimpleDateFormat. "yyyy-MM-dd HH:mm")))
359+
(partial format-date* #(SimpleDateFormat. "yyyy-MM-dd HH:mm")))
356360

357361
(defn page-nav [current-page total-pages & {:keys [base-path] :or {base-path "/projects?page="}}]
358362
(let [previous-text (raw "← Previous")

0 commit comments

Comments
 (0)