Skip to content

Commit

Permalink
Add a hook when retrying
Browse files Browse the repository at this point in the history
  • Loading branch information
domenkozar committed Jan 8, 2024
1 parent 6c1a19e commit 4f495aa
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Network/WebSockets/Simple/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ module Network.WebSockets.Simple.Client
)
where

import Control.Monad (when)
import Data.ByteString (ByteString)
import Data.ByteString.Char8 (unpack)
import Data.Maybe (isJust)
import Network.WebSockets qualified as WS
import Network.WebSockets.Connection.PingPong qualified as PingPong
import Network.WebSockets.Simple.Session qualified as Session
Expand All @@ -20,21 +22,25 @@ import Wuss qualified
data Options = Options
{ headers :: WS.Headers,
messageLimit :: Int,
staminaSettings :: Stamina.RetrySettings
staminaSettings :: Stamina.RetrySettings,
staminaRetry :: Stamina.RetryStatus -> IO ()
}

defaultOptions :: Options
defaultOptions =
Options
{ headers = [],
messageLimit = 10000,
staminaSettings = Stamina.defaults
staminaSettings = Stamina.defaults,
staminaRetry = const $ return ()
}

run :: (Session.Codec send, Session.Codec receive) => ByteString -> Options -> Session.Session IO send receive () -> (receive -> Session.Session IO send receive ()) -> IO ()
run uriBS options app receiveApp = do
(isSecure, host, port, path) <- Utils.parseURI uriBS
Stamina.retry (staminaSettings options) $ \retryStatus ->
Stamina.retry (staminaSettings options) $ \retryStatus -> do
when (isJust $ Stamina.lastException retryStatus) $
staminaRetry options retryStatus
if isSecure
then Wuss.runSecureClientWith (unpack host) (fromIntegral port) (unpack path) connectionOptions (headers options) (go retryStatus)
else WS.runClientWith (unpack host) (fromIntegral port) (unpack path) connectionOptions (headers options) (go retryStatus)
Expand Down

0 comments on commit 4f495aa

Please sign in to comment.