-
Notifications
You must be signed in to change notification settings - Fork 18
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
matthewbauer
wants to merge
4
commits into
memrange:master
Choose a base branch
from
MercuryTechnologies:is-connection-open
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -7,6 +7,7 @@ | |||||||||||||
-- Portability: portable | ||||||||||||||
-- | ||||||||||||||
-- Send push notifications using Apple's HTTP2 APN API | ||||||||||||||
{-# LANGUAGE CPP #-} | ||||||||||||||
{-# LANGUAGE DeriveGeneric #-} | ||||||||||||||
{-# LANGUAGE DeriveAnyClass #-} | ||||||||||||||
{-# LANGUAGE OverloadedStrings #-} | ||||||||||||||
|
@@ -39,6 +40,8 @@ module Network.PushNotify.APN | |||||||||||||
, clearSound | ||||||||||||||
, addSupplementalField | ||||||||||||||
, closeSession | ||||||||||||||
, isConnectionOpen | ||||||||||||||
, isSessionOpen | ||||||||||||||
, isOpen | ||||||||||||||
, ApnSession | ||||||||||||||
, JsonAps | ||||||||||||||
|
@@ -138,6 +141,8 @@ data ApnException = ApnExceptionHTTP ErrorCodeId | |||||||||||||
| ApnExceptionJSON String | ||||||||||||||
| ApnExceptionMissingHeader HTTP2.HeaderName | ||||||||||||||
| ApnExceptionUnexpectedResponse | ||||||||||||||
| ApnExceptionConnectionClosed | ||||||||||||||
| ApnExceptionSessionClosed | ||||||||||||||
deriving (Show, Typeable) | ||||||||||||||
|
||||||||||||||
instance Exception ApnException | ||||||||||||||
|
@@ -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 | ||||||||||||||
|
@@ -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 | ||||||||||||||
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 | ||||||||||||||
|
@@ -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 | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PR can have permissive bounds on
Suggested change
|
||||||||||||||
#else | ||||||||||||||
response <- waitStream stream isfc pph | ||||||||||||||
#endif | ||||||||||||||
let (errOrHeaders, frameResponses, _) = response | ||||||||||||||
case errOrHeaders of | ||||||||||||||
Left err -> throwIO (ApnExceptionHTTP $ toErrorCodeId err) | ||||||||||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 theensureSessionOpen
so thatensureOpen
can be deleted in the next major version.