Skip to content

Commit

Permalink
use stamina for retries
Browse files Browse the repository at this point in the history
  • Loading branch information
domenkozar committed Dec 30, 2023
1 parent 1ccc24c commit 6c1a19e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ High-level interface for websockets.
- Uses channels for sending/receiving messages
- URI based API supporting `ws://` and `wss://` (for the client)
- Simple interface between client and server send/receive types
- TODO: retrying client connection
- Retries connection for the client automatically using [Stamina](https://github.com/cachix/stamina.hs)
- TODO: graceful shutdown

## Example
Expand Down
22 changes: 12 additions & 10 deletions src/Network/WebSockets/Simple/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,35 @@ import Network.WebSockets qualified as WS
import Network.WebSockets.Connection.PingPong qualified as PingPong
import Network.WebSockets.Simple.Session qualified as Session
import Network.WebSockets.Simple.Utils qualified as Utils
import Stamina qualified
import Wuss qualified

data Options = Options
{ headers :: WS.Headers,
messageLimit :: Int
messageLimit :: Int,
staminaSettings :: Stamina.RetrySettings
}

defaultOptions :: Options
defaultOptions =
Options
{ headers = [],
messageLimit = 10000
messageLimit = 10000,
staminaSettings = Stamina.defaults
}

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
if isSecure
then Wuss.runSecureClientWith (unpack host) (fromIntegral port) (unpack path) connectionOptions (headers options) go
else WS.runClientWith (unpack host) (fromIntegral port) (unpack path) connectionOptions (headers options) go
Stamina.retry (staminaSettings 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)
where
connectionOptions :: WS.ConnectionOptions
connectionOptions = WS.defaultConnectionOptions

go :: WS.ClientApp ()
go connection = do
go :: Stamina.RetryStatus -> WS.ClientApp ()
go retryStatus connection = do
Stamina.resetInitial retryStatus
PingPong.withPingPong WS.defaultPingPongOptions connection (\conn -> Session.run (messageLimit options) conn app receiveApp)

-- TODO: shutdown :: IO ()
-- TODO: retries
1 change: 1 addition & 0 deletions stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ nix:
extra-deps:
- wuss-2.0.1.7
- websockets-0.13.0.0
- stamina-0.1.0.0
3 changes: 2 additions & 1 deletion websockets-simple.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ common common
wuss >= 2.0.1.7,
unliftio-core,
bytestring,
exceptions
exceptions,
stamina

default-extensions: OverloadedStrings

Expand Down

0 comments on commit 6c1a19e

Please sign in to comment.