 ; perfomance log
 (defonce log-msgs (atom []))
 (defn add-log [msg]
-  (let [msg (clojure.string/join " " [(Thread/currentThread) (System/currentTimeMillis) msg "\r\n"])]
-  (swap! log-msgs conj msg)))
+  (if (< (count @log-msgs) 4096)
+    (do
+      (let [msg (clojure.string/join " " [(Thread/currentThread) (System/currentTimeMillis) msg "\r\n"])]
+      (swap! log-msgs conj msg)))))
 (defn dump []
   (do
     (with-open [wrtr (writer "perfomance.txt" :append true)]

  (when (:data packet)
    (let [author (@clients channel) text (strip-html-tags (:data packet))]
      (do
        (dosync (ref-set all-msgs (conj @all-msgs {:id (next-id) :time (now) :msg text :author author})))
        (send! channel (json-str {:status "ok"}))
        (clear-old-msgs)))))
; fetch version 1
(defmethod process-packet ["fetch" "1"] [packet channel]
  (let [last-id (@clients-last-id channel) cur-id (+ @max-id 1)]

    (reset! log-msgs [])))
; messages
(defonce all-msgs (ref [{:id (next-id) :time (now) :msg "Добро пожаловать в чат!" :author "system"}]))
(defn clear-old-msgs []
  (dosync
    (let [total (count @all-msgs)]
      (if (> total 4096)
        (ref-set all-msgs (vec (drop (- total 4096) (sort-by :id < @all-msgs))))
        (ref-set all-msgs @all-msgs)))))
(defn add-msg-to-storage [msg]
  (dosync
    (let [all-msgs* (conj @all-msgs msg)] (ref-set all-msgs all-msgs*))
    (clear-old-msgs)))
(defn min-msg-id []
  (apply min (map (fn [msg] (:id msg)) @all-msgs)))

(def not-nil? (complement nil?))
; multi method, dispatch by command and version
(defn dispatcher [packet channel] [(:command packet) (:version packet)])
