Skip to content

Commit

Permalink
use sensible defaults if given a bogus config
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellwrosen committed Sep 27, 2023
1 parent 986268f commit e9691c7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 23 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Unreleased

- `create` / `with` will no longer throw an exception if given an invalid config; rather, the config's invalid values
will be replaced with sensible defaults
- Remove `atomic-primops` dependency

## [0.4.0.1] - 2022-11-05
Expand Down
30 changes: 9 additions & 21 deletions src/TimerWheel.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,12 @@ module TimerWheel
)
where

import Control.Exception (throwIO)
import Control.Monad (when)
import Data.Bool (bool)
import Data.Fixed (E6, Fixed)
import Data.Function (fix)
import Data.IORef (newIORef, readIORef, writeIORef)
import GHC.Exception (errorCallException)
import qualified Ki
import TimerWheel.Internal.Config (Config)
import qualified TimerWheel.Internal.Config as Config
import TimerWheel.Internal.Config (Config (..))
import TimerWheel.Internal.Counter (Counter, incrCounter, newCounter)
import TimerWheel.Internal.Micros (Micros (Micros))
import qualified TimerWheel.Internal.Micros as Micros
Expand Down Expand Up @@ -93,14 +89,12 @@ data TimerWheel = TimerWheel
}

-- | Create a timer wheel in a scope.
--
-- /Throws./
--
-- * Calls 'error' if the config is invalid
create :: Ki.Scope -> Config -> IO TimerWheel
create scope config = do
validateConfig config
wheel <- Wheel.create (Config.spokes config) (Micros.fromFixed (Config.resolution config))
create scope Config {spokes, resolution} = do
wheel <-
Wheel.create
(if spokes <= 0 then 1024 else spokes)
(Micros.fromFixed (if resolution <= 0 then 1 else resolution))
counter <- newCounter
Ki.fork_ scope (Wheel.reap wheel)
pure TimerWheel {counter, wheel}
Expand All @@ -109,7 +103,6 @@ create scope config = do
--
-- /Throws./
--
-- * Calls 'error' if the config is invalid
-- * Throws the exception the given action throws, if any
-- * Throws the exception the timer wheel thread throws, if any
with :: Config -> (TimerWheel -> IO a) -> IO a
Expand All @@ -118,11 +111,6 @@ with config action =
wheel <- create scope config
action wheel

validateConfig :: Config -> IO ()
validateConfig config =
when (Config.spokes config <= 0 || Config.resolution config <= 0) do
throwIO (errorCallException ("timer-wheel: invalid config: " ++ show config))

-- | @register wheel delay action@ registers an action __@action@__ in timer wheel __@wheel@__ to fire after __@delay@__
-- seconds.
--
Expand Down Expand Up @@ -214,10 +202,10 @@ reregister wheel delay =
where
reso :: Micros
reso =
resolution wheel
wheelResolution wheel

resolution :: TimerWheel -> Micros
resolution =
wheelResolution :: TimerWheel -> Micros
wheelResolution =
Wheel.resolution . wheel

-- Repeat an IO action until it returns 'True'.
Expand Down
4 changes: 2 additions & 2 deletions src/TimerWheel/Internal/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import GHC.Generics (Generic)

-- | Timer wheel config.
--
-- * @spokes@ must be ∈ @[1, maxBound]@
-- * @resolution@ must ∈ @(0, ∞]@
-- * @spokes@ must be ∈ @[1, maxBound]@, and is set to @1024@ if invalid.
-- * @resolution@ must be ∈ @(0, ∞]@, and is set to @1@ if invalid.
data Config = Config
{ -- | Spoke count
spokes :: {-# UNPACK #-} !Int,
Expand Down

0 comments on commit e9691c7

Please sign in to comment.