Skip to content

Commit

Permalink
Update and add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
evenbrenden committed Jan 24, 2023
1 parent f31eda2 commit 255096d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
9 changes: 4 additions & 5 deletions example/Main.hs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
-- Copyright © FINN.no AS, Inc. All rights reserved.

-- Example application that uses unleash-client-haskell.
--
-- Spawns a state poller thread that updates the feature toggles, a metrics
-- sender thread, and an application that continuously reads a feature toggle.
-- The application will block until the first feature toggle set is received.
-- Example application that uses unleash-client-haskell. Spawns a state poller
-- thread that updates the feature toggles, a metrics sender thread, and an
-- application that continuously reads a feature toggle. The application will
-- block until the first feature toggle set is received.

module Main where

Expand Down
21 changes: 18 additions & 3 deletions src/Unleash/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import Servant.Client (BaseUrl, ClientEnv, ClientError, mkClientEnv)
import qualified Unleash
import Unleash.HttpClient (getAllClientFeatures, register, sendMetrics)

-- This is a smart constructor that initializes the mutable variables right
-- Smart constructor for Unleash client configuration
-- Initializes the mutable variables properly
makeConfig :: Text -> Text -> BaseUrl -> IO Config
makeConfig applicationName instanceId serverUrl = do
state <- newEmptyMVar
Expand All @@ -39,6 +40,8 @@ makeConfig applicationName instanceId serverUrl = do
httpClientEnvironment = clientEnv
}

-- Unleash client configuration
-- Use the smart constructor or make sure the mutable metrics variables are not empty
data Config = Config
{ applicationName :: Text,
instanceId :: Text,
Expand All @@ -50,6 +53,8 @@ data Config = Config
httpClientEnvironment :: ClientEnv
}

-- Registers client for the Unleash server
-- Call this on application startup before calling the state poller and metrics pusher functions
registerClient :: Config -> IO (Either ClientError ())
registerClient config = do
now <- getCurrentTime
Expand All @@ -63,6 +68,9 @@ registerClient config = do
}
void <$> register config.httpClientEnvironment Nothing registrationPayload

-- Fetches the most recent feature toggle set from the Unleash server
-- Meant to be run every statePollIntervalInSeconds
-- Non-blocking
pollState :: Config -> IO (Either ClientError ())
pollState config = do
eitherFeatures <- getAllClientFeatures config.httpClientEnvironment Nothing
Expand All @@ -73,6 +81,9 @@ pollState config = do
isUpdated <- tryPutMVar state value
unless isUpdated . void $ swapMVar state value

-- Pushes metrics to the Unleash server
-- Meant to be run every metricsPushIntervalInSeconds
-- Blocks if the mutable metrics variables are empty
pushMetrics :: Config -> IO (Either ClientError ())
pushMetrics config = do
now <- getCurrentTime
Expand All @@ -88,15 +99,19 @@ pushMetrics config = do
}
void <$> sendMetrics config.httpClientEnvironment Nothing metricsPayload

-- This function blocks until first feature toggle set is received
-- Checks if a feature is enabled or not
-- Blocks until first feature toggle set is received
-- Blocks if the mutable metrics variables are empty
isEnabled :: Config -> Text -> Unleash.Context -> IO Bool
isEnabled config feature context = do
state <- readMVar config.state
enabled <- Unleash.featureIsEnabled state feature context
modifyMVar_ config.metrics (\info -> pure $ (feature, enabled) : info)
pure enabled

-- This function returns false for all toggles until first toggle set is received
-- Checks if a feature is enabled or not
-- Returns false for all toggles until first toggle set is received
-- Blocks if the mutable metrics variables are empty
tryIsEnabled :: Config -> Text -> Unleash.Context -> IO Bool
tryIsEnabled config feature context = do
maybeState <- tryReadMVar config.state
Expand Down

0 comments on commit 255096d

Please sign in to comment.