-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from mlabs-haskell/euonymos/jitter-backoff
Support for Backoff
- Loading branch information
Showing
7 changed files
with
147 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.cabal | ||
dist-newstyle | ||
dist | ||
dist-* | ||
|
This file contains 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 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 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 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 |
---|---|---|
@@ -1,19 +1,39 @@ | ||
{-# LANGUAGE LambdaCase #-} | ||
|
||
module Maestro.Client.V1.Core | ||
( apiV1Client | ||
, module Maestro.Client.V1.Core.Pagination | ||
) where | ||
|
||
import Control.Exception (throwIO) | ||
import Control.Retry (retrying, limitRetriesByCumulativeDelay, exponentialBackoff) | ||
import Maestro.API.V1 | ||
import Maestro.Client.Env | ||
import Maestro.Client.Error (fromServantClientError) | ||
import Maestro.Client.Error (fromServantClientError, MaestroError (..)) | ||
import Maestro.Client.V1.Core.Pagination | ||
import Servant.API.Generic (fromServant) | ||
import Servant.Client | ||
import Servant.Client.Generic | ||
import Control.Monad ((>=>)) | ||
|
||
|
||
apiV1ClientAuth :: MaestroEnv 'V1 -> MaestroApiV1Auth (AsClientT IO) | ||
apiV1ClientAuth MaestroEnv{..} = genericClientHoist $ \x -> runClientM x maeClientEnv >>= either (throwIO . fromServantClientError) pure | ||
apiV1ClientAuth MaestroEnv{..} = | ||
genericClientHoist $ | ||
do | ||
let handler = case (_maeBaseDelay , _maeMaxDelay) of | ||
(Just bDelay, Just mDelay) -> | ||
\x -> | ||
retrying | ||
(limitRetriesByCumulativeDelay mDelay$ exponentialBackoff bDelay) | ||
(\_retryStatus -> \case | ||
Right _ -> pure False | ||
Left clientErr -> case fromServantClientError clientErr of | ||
MaestroUsageLimitReached -> pure True | ||
_ -> pure False | ||
) | ||
(\_ -> runClientM x _maeClientEnv) | ||
_ -> \x -> runClientM x _maeClientEnv | ||
handler >=> either (throwIO . fromServantClientError) pure | ||
|
||
apiV1Client :: MaestroEnv 'V1 -> MaestroApiV1 (AsClientT IO) | ||
apiV1Client mEnv@MaestroEnv {..} = fromServant $ apiV1 (apiV1ClientAuth mEnv) maeToken | ||
apiV1Client mEnv@MaestroEnv {..} = fromServant $ apiV1 (apiV1ClientAuth mEnv) _maeToken |
This file contains 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
{-# OPTIONS_GHC -F -pgmF tasty-discover #-} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
module Maestro.Test.Backoff where | ||
|
||
import Test.Hspec | ||
|
||
import Control.Concurrent (MVar, ThreadId, newMVar, takeMVar, putMVar, newEmptyMVar, forkFinally) | ||
import Control.Exception (try) | ||
import Control.Monad (void) | ||
import Data.Text (pack) | ||
import Maestro.Client.V1 | ||
import Maestro.Types.V1 | ||
import System.Environment (getEnv) | ||
|
||
|
||
spec_backoff :: Spec | ||
spec_backoff = do | ||
it "errors without backoff" $ do | ||
shouldThrow (doConcCall Nothing) anyErrorCall | ||
|
||
it "works with default backoff settings" $ do | ||
doConcCall defaultBackoff | ||
|
||
type Ret = Either MaestroError [UtxoWithSlot] | ||
|
||
doConcCall :: Maybe (Int, Int) -> IO () | ||
doConcCall backoffSettings = do | ||
maestroKey <- pack <$> getEnv "MAESTRO_API_KEY" | ||
env <- mkMaestroEnv @'V1 maestroKey Preprod backoffSettings | ||
children <- newMVar [] | ||
void $ mapM (forkChild children) $ replicate 30 $ task env | ||
waitForChildren children | ||
where | ||
task :: MaestroEnv 'V1 -> IO Ret | ||
task env = | ||
try | ||
$ allPages | ||
$ flip | ||
( | ||
utxosAtMultiAddresses env | ||
(Just True) | ||
(Just False) | ||
) ["addr_test1vqj247zdmh7n9g46ukk59k2yxeslevzhah0uj3t0t450x3ggycpxj"] | ||
|
||
forkChild :: MVar [MVar Ret] -> IO (Ret) -> IO ThreadId | ||
forkChild children action = do | ||
mvar :: MVar Ret <- newEmptyMVar | ||
childs <- takeMVar children | ||
putMVar children (mvar:childs) | ||
forkFinally action $ \ret -> case ret of | ||
Left _ -> putMVar mvar $ Left $ MaestroError "client finished abruptly" | ||
Right ret' -> putMVar mvar ret' | ||
|
||
waitForChildren :: MVar [MVar Ret] -> IO () | ||
waitForChildren children = do | ||
cs <- takeMVar children | ||
case cs of | ||
[] -> return () | ||
m:ms -> do | ||
putMVar children ms | ||
ret <- takeMVar m | ||
case ret of | ||
Left _ -> error "failed" | ||
_ -> waitForChildren children | ||
|
||
|