Skip to content

Check if connection is open in withConnection #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion push-notify-apn.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ library
, containers
, data-default
, http2
, http2-client
, http2-client >= 0.10.0.1
, lifted-base
, mtl
, random
Expand Down
37 changes: 31 additions & 6 deletions src/Network/PushNotify/APN.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
-- Portability: portable
--
-- Send push notifications using Apple's HTTP2 APN API
{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE OverloadedStrings #-}
Expand Down Expand Up @@ -39,6 +40,8 @@ module Network.PushNotify.APN
, clearSound
, addSupplementalField
, closeSession
, isConnectionOpen
, isSessionOpen
, isOpen
, ApnSession
, JsonAps
Expand Down Expand Up @@ -138,6 +141,8 @@ data ApnException = ApnExceptionHTTP ErrorCodeId
| ApnExceptionJSON String
| ApnExceptionMissingHeader HTTP2.HeaderName
| ApnExceptionUnexpectedResponse
| ApnExceptionConnectionClosed
| ApnExceptionSessionClosed
deriving (Show, Typeable)

instance Exception ApnException
Expand Down Expand Up @@ -433,17 +438,28 @@ closeSession s = do

-- | Check whether a session is open or has been closed
-- by a call to closeSession
isSessionOpen :: ApnSession -> IO Bool
isSessionOpen = readIORef . apnSessionOpen

-- | Check whether a session is open or has been closed
-- by a call to closeSession
{-# DEPRECATED isOpen "Use isSessionOpen instead." #-}
isOpen :: ApnSession -> IO Bool
isOpen = readIORef . apnSessionOpen
isOpen = isSessionOpen

-- | Check whether the connection is open or has been closed.
isConnectionOpen :: ApnConnection -> IO Bool
isConnectionOpen = readIORef . apnConnectionOpen

timeoutSeconds :: Int
timeoutSeconds = 300 * 1_000_000 -- 300 seconds to microseconds

withConnection :: ApnSession -> (ApnConnection -> ClientIO a) -> ClientIO a
withConnection s action = do
lift $ ensureOpen s
lift $ ensureSessionOpen s
ExceptT . try $
withResource (apnSessionPool s) $ \conn -> do
ensureConnectionOpen conn
mRes <- timeout timeoutSeconds (runClientIO (action conn))
case mRes of
Nothing -> do
Expand Down Expand Up @@ -604,10 +620,15 @@ sendSilentMessage s token mJwt = catchErrors $
sendApnRaw c token mJwt message
where message = "{\"aps\":{\"content-available\":1}}"

ensureOpen :: ApnSession -> IO ()
ensureOpen s = do
open <- isOpen s
Comment on lines -607 to -609

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For backwards compat, it's nicer if we leave the existing function as an alias. The types indicate what sort of thing it is. Then we're in minor bump land. A DEPRECATED pragma could also point users to the ensureSessionOpen so that ensureOpen can be deleted in the next major version.

unless open $ error "Session is closed"
ensureSessionOpen :: ApnSession -> IO ()
ensureSessionOpen s = do
open <- isSessionOpen s
unless open $ throwIO ApnExceptionSessionClosed

ensureConnectionOpen :: ApnConnection -> IO ()
ensureConnectionOpen c = do
open <- isConnectionOpen c
unless open $ throwIO ApnExceptionConnectionClosed

-- | Send a push notification message.
sendApnRaw
Expand Down Expand Up @@ -639,7 +660,11 @@ sendApnRaw connection deviceToken mJwtBearerToken message = bracket_
upload message (HTTP2.setEndHeader . HTTP2.setEndStream) client (_outgoingFlowControl client) stream osfc
let pph _hStreamId _hStream hHeaders _hIfc _hOfc =
lift $ print hHeaders
#if MIN_VERSION_http2_client(0, 10, 0)
response <- waitStream client stream isfc pph

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR can have permissive bounds on http2-client if this is guarded with CPP

Suggested change
response <- waitStream client stream isfc pph
#if MIN_VERSION_http2_client(?major, ?major, ?minor)
response <- waitStream client stream isfc pph
#else
response <- waitStream stream isfc pph
#endif

#else
response <- waitStream stream isfc pph
#endif
let (errOrHeaders, frameResponses, _) = response
case errOrHeaders of
Left err -> throwIO (ApnExceptionHTTP $ toErrorCodeId err)
Expand Down