From 9d3867b11a86c87422a17cf8249fdb0770d0746f Mon Sep 17 00:00:00 2001 From: Iustin Pop <iustin@google.com> Date: Fri, 14 Dec 2012 01:31:53 +0100 Subject: [PATCH] Add constants for two values which we re-evaluate 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 | 7 +++++-- htools/Ganeti/Confd/Utils.hs | 6 +++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/htools/Ganeti/Confd/Server.hs b/htools/Ganeti/Confd/Server.hs index 53891c34a..b4b13854f 100644 --- a/htools/Ganeti/Confd/Server.hs +++ b/htools/Ganeti/Confd/Server.hs @@ -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 } diff --git a/htools/Ganeti/Confd/Utils.hs b/htools/Ganeti/Confd/Utils.hs index 667055f7e..71f4ef006 100644 --- a/htools/Ganeti/Confd/Utils.hs +++ b/htools/Ganeti/Confd/Utils.hs @@ -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) -- GitLab