Skip to content

Commit

Permalink
implement graceful shutdown for the client
Browse files Browse the repository at this point in the history
  • Loading branch information
domenkozar committed Jan 26, 2024
1 parent d2d4431 commit 3d98ada
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

High-level interface for websockets.

## Features
## Features

- Splits up receiving and sending into separate threads
- Wires up Ping/Pong for Client and Server
- 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
- Retries connection indefinitely for the client automatically using [Stamina](https://github.com/cachix/stamina.hs)
- TODO: graceful shutdown
- Handles graceful shutdown for the client

## Example

Expand Down
16 changes: 13 additions & 3 deletions src/Network/WebSockets/Typed/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Network.WebSockets.Typed.Client
)
where

import Control.Exception (finally)
import Control.Monad (when)
import Data.ByteString (ByteString)
import Data.ByteString.Char8 (unpack)
Expand All @@ -23,7 +24,8 @@ data Options = Options
{ headers :: WS.Headers,
messageLimit :: Int,
staminaSettings :: Stamina.RetrySettings,
staminaRetry :: Stamina.RetryStatus -> IO ()
staminaRetry :: Stamina.RetryStatus -> IO (),
onShutdown :: WS.Connection -> IO ()
}

defaultOptions :: Options
Expand All @@ -36,7 +38,11 @@ defaultOptions =
{ Stamina.maxTime = Nothing,
Stamina.maxAttempts = Nothing
},
staminaRetry = const $ return ()
staminaRetry = const $ return (),
onShutdown = \connection ->
-- TODO: drain the message queues
-- TODO: if there was an exception related to socket, we need to handle it?
WS.sendClose connection ("Bye!" :: ByteString)
}

run :: (Session.Codec send, Session.Codec receive) => ByteString -> Options -> Session.Session IO send receive () -> (receive -> Session.Session IO send receive ()) -> IO ()
Expand All @@ -55,4 +61,8 @@ run uriBS options app receiveApp = 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)
PingPong.withPingPong
WS.defaultPingPongOptions
connection
$ const
$ Session.run (messageLimit options) connection app receiveApp `finally` onShutdown options connection

0 comments on commit 3d98ada

Please sign in to comment.