on master help sign in
Put an upper bound on the total number of sessions
commit5592064c83756c91d23451870a30e19db3dc377a authorHarendra Kumar <harendra.kumar@gmail.com> dateFri 7 Feb 2020, 15:18 (7 years ago) parentf1d30a1
1 file changed+12−6
src/Streamly/Internal/Prelude.hs+12−6
src/Streamly/Internal/Prelude.hs+12−6
@@ -4009,6 +4009,8 @@ data SessionState t m k a b = SessionState
40094009-- is started with a tick duration specified by @tick@. This timer is used to
40104010-- detect session timeouts in the absence of new events.
40114011--
4012+-- After @maxSessions@ limit is reached we start ejecting old sessions.
4013+--
40124014-- /Internal/
40134015--
40144016
@@ -4019,10 +4021,12 @@ classifySessionsBy
40194021 => Double -- ^ timer tick in seconds
40204022 -> Double -- ^ session timeout in seconds
40214023 -> Bool -- ^ reset the timeout when an event is received
4024+ -> Int -- ^ maximum number of sessions to cache
40224025 -> Fold m a (Either b b) -- ^ Fold to be applied to session events
40234026 -> t m (k, a, AbsTime) -- ^ session key, data, timestamp
40244027 -> t m (k, b) -- ^ session key, fold result
4025−classifySessionsBy tick timeout reset (Fold step initial extract) str =
4028+classifySessionsBy tick timeout reset maxSessions
4029+ (Fold step initial extract) str =
40264030 concatMap (\session -> sessionOutputStream session)
40274031 $ scanlM' sstep szero stream
40284032
@@ -4113,10 +4117,11 @@ classifySessionsBy tick timeout reset (Fold step initial extract) str =
41134117 -- Got a timer tick event
41144118 -- XXX can we yield the entries without accumulating them?
41154119 sstep (session@SessionState{..}) Nothing = do
4116− (hp', mp', out, count) <- go sessionTimerHeap sessionKeyValueMap K.nil 0
4120+ (hp', mp', out, count) <-
4121+ go sessionTimerHeap sessionKeyValueMap K.nil sessionCount
41174122 return $ session
41184123 { sessionCurTime = curTime
4119− , sessionCount = sessionCount - count
4124+ , sessionCount = count
41204125 , sessionTimerHeap = hp'
41214126 , sessionKeyValueMap = mp'
41224127 , sessionOutputStream = out
@@ -4125,11 +4130,12 @@ classifySessionsBy tick timeout reset (Fold step initial extract) str =
41254130 where
41264131
41274132 curTime = addToAbsTime sessionCurTime tickMs
4133+ expired etime cnt = curTime >= etime || cnt > maxSessions
41284134 go hp mp out cnt = do
41294135 let hres = H.uncons hp
41304136 case hres of
41314137 Just (Entry expiry key, hp') -> do
4132− if curTime >= expiry
4138+ if expired expiry
41334139 then do
41344140 let (r, mp') = Map.updateLookupWithKey
41354141 (\_ _ -> Nothing) key mp
@@ -4137,14 +4143,14 @@ classifySessionsBy tick timeout reset (Fold step initial extract) str =
41374143 Nothing -> go hp' mp' out cnt
41384144 Just (Tuple' latestTS acc) -> do
41394145 let expiry' = addToAbsTime latestTS timeoutMs
4140− if curTime >= expiry' || not reset
4146+ if expired expiry' || not reset
41414147 then do
41424148 sess <- extract acc
41434149 let val = case sess of
41444150 Left x -> x
41454151 Right x -> x
41464152 go hp' mp' ((key, val) `K.cons` out)
4147− (cnt + 1)
4153+ (cnt - 1)
41484154 else
41494155 -- reset the session timeout
41504156 -- XXX purge anyway if the session size