Add constants for two values which we re-evaluate
authorIustin Pop <iustin@google.com>
Fri, 14 Dec 2012 00:31:53 +0000 (01:31 +0100)
committerIustin Pop <iustin@google.com>
Fri, 14 Dec 2012 14:55:24 +0000 (15:55 +0100)
Two constants which we use as Integer are defined as Int in
Constants.hs (coming from constants.py), so we do the conversion every
time we use it. Let's move them to top-level names, so that the
conversion is only done once.

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Guido Trotter <ultrotter@google.com>

htools/Ganeti/Confd/Server.hs
htools/Ganeti/Confd/Utils.hs

index 53891c3..b4b1385 100644 (file)
@@ -100,6 +100,10 @@ configReloadTimeout = C.confdConfigReloadTimeout * 1000000
 configReloadRatelimit :: Int
 configReloadRatelimit = C.confdConfigReloadRatelimit * 1000000
 
+-- | Ratelimit timeout in seconds, as an 'Integer'.
+reloadRatelimitSec :: Integer
+reloadRatelimitSec = fromIntegral C.confdConfigReloadRatelimit
+
 -- | Initial poll round.
 initialPoll :: ReloadModel
 initialPoll = ReloadPoll 0
@@ -448,8 +452,7 @@ onInotify inotify path cref mstate _ =
          ctime <- getCurrentTime
          (newfstat, _) <- safeUpdateConfig path (reloadFStat state) cref
          let state' = state { reloadFStat = newfstat, reloadTime = ctime }
-         if abs (reloadTime state - ctime) <
-            fromIntegral C.confdConfigReloadRatelimit
+         if abs (reloadTime state - ctime) < reloadRatelimitSec
            then do
              mode <- moveToPolling "too many reloads" inotify path cref mstate
              return state' { reloadModel = mode }
index 667055f..71f4ef0 100644 (file)
@@ -44,6 +44,10 @@ import qualified Ganeti.Path as Path
 import Ganeti.JSON
 import Ganeti.Utils
 
+-- | Type-adjusted max clock skew constant.
+maxClockSkew :: Integer
+maxClockSkew = fromIntegral C.confdMaxClockSkew
+
 -- | Returns the HMAC key.
 getClusterHmac :: IO HashKey
 getClusterHmac = Path.confdHmacKey >>= fmap B.unpack . B.readFile
@@ -64,7 +68,7 @@ parseMessage :: HashKey -> String -> Integer
 parseMessage hmac msg curtime = do
   (salt, origmsg, request) <- parseRequest hmac msg
   ts <- tryRead "Parsing timestamp" salt::Result Integer
-  if abs (ts - curtime) > fromIntegral C.confdMaxClockSkew
+  if abs (ts - curtime) > maxClockSkew
     then fail "Too old/too new timestamp or clock skew"
     else return (origmsg, request)