rename sweeper thread properties to be a little clearer
i guess this is all subjective though ...
This commit is contained in:
parent
0855450a2b
commit
2724754021
|
@ -57,7 +57,7 @@
|
||||||
(read-timestamp [store key]
|
(read-timestamp [store key]
|
||||||
"Read a session from the store and return its timestamp. If no key exists, returns nil."))
|
"Read a session from the store and return its timestamp. If no key exists, returns nil."))
|
||||||
|
|
||||||
(defrecord MemoryAgingStore [session-map ttl refresh-on-write refresh-on-read req-count req-limit]
|
(defrecord MemoryAgingStore [session-map ttl refresh-on-write refresh-on-read op-counter op-threshold]
|
||||||
AgingStore
|
AgingStore
|
||||||
(read-timestamp [_ key]
|
(read-timestamp [_ key]
|
||||||
(get-in @session-map [key :timestamp]))
|
(get-in @session-map [key :timestamp]))
|
||||||
|
@ -73,8 +73,8 @@
|
||||||
|
|
||||||
(write-session [_ key data]
|
(write-session [_ key data]
|
||||||
(let [key (or key (unique-id))]
|
(let [key (or key (unique-id))]
|
||||||
(swap! req-count inc) ; Increase the request count
|
(swap! op-counter inc)
|
||||||
(if refresh-on-write ; Write key and and update timestamp.
|
(if refresh-on-write
|
||||||
(swap! session-map assoc key (new-entry data))
|
(swap! session-map assoc key (new-entry data))
|
||||||
(swap! session-map write-entry key data))
|
(swap! session-map write-entry key data))
|
||||||
key))
|
key))
|
||||||
|
@ -85,12 +85,12 @@
|
||||||
|
|
||||||
(defn sweeper-thread
|
(defn sweeper-thread
|
||||||
"Sweeper thread that watches the session and cleans it."
|
"Sweeper thread that watches the session and cleans it."
|
||||||
[{:keys [ttl req-count req-limit session-map]} sweep-delay]
|
[{:keys [ttl op-counter op-threshold session-map]} sweep-interval]
|
||||||
(loop []
|
(loop []
|
||||||
(when (>= @req-count req-limit)
|
(when (>= @op-counter op-threshold)
|
||||||
(swap! session-map sweep-session ttl)
|
(swap! session-map sweep-session ttl)
|
||||||
(reset! req-count 0))
|
(reset! op-counter 0))
|
||||||
(Thread/sleep sweep-delay) ;; sleep for 30s
|
(Thread/sleep sweep-interval)
|
||||||
(recur)))
|
(recur)))
|
||||||
|
|
||||||
(defn in-thread
|
(defn in-thread
|
||||||
|
@ -101,15 +101,19 @@
|
||||||
(defn aging-memory-store
|
(defn aging-memory-store
|
||||||
"Creates an in-memory session storage engine where entries expire after the given ttl"
|
"Creates an in-memory session storage engine where entries expire after the given ttl"
|
||||||
[ttl & [opts]]
|
[ttl & [opts]]
|
||||||
(let [{:keys [session-atom refresh-on-write refresh-on-read sweep-every sweep-delay]
|
(let [{:keys [session-atom refresh-on-write refresh-on-read sweep-threshold sweep-interval]
|
||||||
:or {session-atom (atom {})
|
:or {session-atom (atom {})
|
||||||
refresh-on-write false
|
refresh-on-write false
|
||||||
refresh-on-read false
|
refresh-on-read false
|
||||||
sweep-every 200
|
sweep-threshold 200
|
||||||
sweep-delay 30000}} opts
|
sweep-interval 30}} opts
|
||||||
ttl (* 1000 ttl) ; internally, we want ttl in milliseconds for convenience...
|
; internally, we want time values as milliseconds. externally, it is more convenient to have them specified
|
||||||
counter-atom (atom 0)
|
; as seconds because, really, for sessions, no one is really going to want to specify sub-second values for
|
||||||
store (MemoryAgingStore. session-atom ttl refresh-on-write refresh-on-read counter-atom sweep-every)]
|
; any of these times! (no, you don't really need a sweeper thread running multiple times per second ...)
|
||||||
(in-thread #(sweeper-thread store sweep-delay))
|
sweep-interval (* 1000 sweep-interval)
|
||||||
|
ttl (* 1000 ttl)
|
||||||
|
op-counter (atom 0)
|
||||||
|
store (MemoryAgingStore. session-atom ttl refresh-on-write refresh-on-read op-counter sweep-threshold)]
|
||||||
|
(in-thread #(sweeper-thread store sweep-interval))
|
||||||
store))
|
store))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue