Skip to content

Commit d052b57

Browse files
committed
[retry] use Ref for tracking failures
1 parent 5f53e6b commit d052b57

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

bower.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"purescript-integers": "^0.2.0",
3131
"purescript-math": "^0.2.0",
3232
"purescript-nullable": "^0.2.0",
33+
"purescript-refs": "^0.2.0",
3334
"purescript-unsafe-coerce": "^0.1.0"
3435
},
3536
"devDependencies": {

docs/Network.HTTP.Affjax.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ A sensible default for retries: no timeout, maximum delay of 30s, initial delay
171171
#### `retry`
172172

173173
``` purescript
174-
retry :: forall e a b. (Requestable a) => RetryPolicy -> (AffjaxRequest a -> Affjax (avar :: AVAR | e) b) -> AffjaxRequest a -> Affjax (avar :: AVAR | e) b
174+
retry :: forall e a b. (Requestable a) => RetryPolicy -> (AffjaxRequest a -> Affjax (avar :: AVAR, ref :: REF | e) b) -> AffjaxRequest a -> Affjax (avar :: AVAR, ref :: REF | e) b
175175
```
176176

177177
Retry a request using a `RetryPolicy`. After the timeout, the last received response is returned; if it was not possible to communicate with the server due to an error, then this is bubbled up.

src/Network/HTTP/Affjax.purs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ import Control.Monad.Aff (Aff(), makeAff, makeAff', Canceler(..), attempt, later
2323
import Control.Monad.Aff.Par (Par(..), runPar)
2424
import Control.Monad.Aff.AVar (AVAR(), makeVar, takeVar, putVar)
2525
import Control.Monad.Eff (Eff())
26+
import Control.Monad.Eff.Class (liftEff)
2627
import Control.Monad.Eff.Exception (Error(), error)
28+
import Control.Monad.Eff.Ref (REF(), newRef, readRef, writeRef)
2729
import Control.Monad.Error.Class (throwError)
2830
import Data.Either (Either(..), either)
2931
import Data.Foreign (Foreign(..), F(), parseJSON, readString)
@@ -149,11 +151,11 @@ defaultRetryPolicy =
149151
type RetryState e a = Either (Either e a) a
150152

151153
-- | Retry a request using a `RetryPolicy`. After the timeout, the last received response is returned; if it was not possible to communicate with the server due to an error, then this is bubbled up.
152-
retry :: forall e a b. (Requestable a) => RetryPolicy -> (AffjaxRequest a -> Affjax (avar :: AVAR | e) b) -> (AffjaxRequest a -> Affjax (avar :: AVAR | e) b)
154+
retry :: forall e a b. (Requestable a) => RetryPolicy -> (AffjaxRequest a -> Affjax (avar :: AVAR, ref :: REF | e) b) -> (AffjaxRequest a -> Affjax (avar :: AVAR, ref :: REF | e) b)
153155
retry policy run req = do
154-
-- failureVar is either an exception or a failed request
155-
failureVar <- makeVar
156-
let loop = go failureVar
156+
-- failureRef is either an exception or a failed request
157+
failureRef <- liftEff $ newRef Nothing
158+
let loop = go failureRef
157159
case policy.timeout of
158160
Nothing -> loop 1
159161
Just timeout -> do
@@ -165,7 +167,11 @@ retry policy run req = do
165167
loopHandle `cancel` error "Cancel"
166168
result <- takeVar respVar
167169
case result of
168-
Nothing -> takeVar failureVar >>= either throwError pure
170+
Nothing -> do
171+
failure <- liftEff $ readRef failureRef
172+
case failure of
173+
Nothing -> throwError $ error "Timeout"
174+
Just failure -> either throwError pure failure
169175
Just resp -> pure resp
170176
where
171177
retryState :: Either _ _ -> RetryState _ _
@@ -179,12 +185,12 @@ retry policy run req = do
179185
else
180186
Right resp
181187

182-
go failureVar n = do
188+
go failureRef n = do
183189
result <- retryState <$> attempt (run req)
184190
case result of
185191
Left err -> do
186-
putVar failureVar err
187-
later' (policy.delayCurve n) $ go failureVar (n + 1)
192+
liftEff $ writeRef failureRef $ Just err
193+
later' (policy.delayCurve n) $ go failureRef (n + 1)
188194
Right resp -> pure resp
189195

190196
-- | Run a request directly without using `Aff`.

0 commit comments

Comments
 (0)