{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
{-# OPTIONS_GHC -fno-warn-unused-matches #-}

-- Derived from AWS service descriptions, licensed under Apache 2.0.

-- |
-- Module      : Amazonka.AppMesh.Types.HttpRetryPolicy
-- Copyright   : (c) 2013-2021 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay <brendan.g.hay+amazonka@gmail.com>
-- Stability   : auto-generated
-- Portability : non-portable (GHC extensions)
module Amazonka.AppMesh.Types.HttpRetryPolicy where

import Amazonka.AppMesh.Types.Duration
import Amazonka.AppMesh.Types.TcpRetryPolicyEvent
import qualified Amazonka.Core as Core
import qualified Amazonka.Lens as Lens
import qualified Amazonka.Prelude as Prelude

-- | An object that represents a retry policy. Specify at least one value for
-- at least one of the types of @RetryEvents@, a value for @maxRetries@,
-- and a value for @perRetryTimeout@. Both @server-error@ and
-- @gateway-error@ under @httpRetryEvents@ include the Envoy @reset@
-- policy. For more information on the @reset@ policy, see the
-- <https://www.envoyproxy.io/docs/envoy/latest/configuration/http/http_filters/router_filter#x-envoy-retry-on Envoy documentation>.
--
-- /See:/ 'newHttpRetryPolicy' smart constructor.
data HttpRetryPolicy = HttpRetryPolicy'
  { -- | Specify at least one of the following values.
    --
    -- -   __server-error__ – HTTP status codes 500, 501, 502, 503, 504, 505,
    --     506, 507, 508, 510, and 511
    --
    -- -   __gateway-error__ – HTTP status codes 502, 503, and 504
    --
    -- -   __client-error__ – HTTP status code 409
    --
    -- -   __stream-error__ – Retry on refused stream
    HttpRetryPolicy -> Maybe (NonEmpty Text)
httpRetryEvents :: Prelude.Maybe (Prelude.NonEmpty Prelude.Text),
    -- | Specify a valid value. The event occurs before any processing of a
    -- request has started and is encountered when the upstream is temporarily
    -- or permanently unavailable.
    HttpRetryPolicy -> Maybe (NonEmpty TcpRetryPolicyEvent)
tcpRetryEvents :: Prelude.Maybe (Prelude.NonEmpty TcpRetryPolicyEvent),
    -- | The maximum number of retry attempts.
    HttpRetryPolicy -> Natural
maxRetries :: Prelude.Natural,
    -- | The timeout for each retry attempt.
    HttpRetryPolicy -> Duration
perRetryTimeout :: Duration
  }
  deriving (HttpRetryPolicy -> HttpRetryPolicy -> Bool
(HttpRetryPolicy -> HttpRetryPolicy -> Bool)
-> (HttpRetryPolicy -> HttpRetryPolicy -> Bool)
-> Eq HttpRetryPolicy
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: HttpRetryPolicy -> HttpRetryPolicy -> Bool
$c/= :: HttpRetryPolicy -> HttpRetryPolicy -> Bool
== :: HttpRetryPolicy -> HttpRetryPolicy -> Bool
$c== :: HttpRetryPolicy -> HttpRetryPolicy -> Bool
Prelude.Eq, ReadPrec [HttpRetryPolicy]
ReadPrec HttpRetryPolicy
Int -> ReadS HttpRetryPolicy
ReadS [HttpRetryPolicy]
(Int -> ReadS HttpRetryPolicy)
-> ReadS [HttpRetryPolicy]
-> ReadPrec HttpRetryPolicy
-> ReadPrec [HttpRetryPolicy]
-> Read HttpRetryPolicy
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [HttpRetryPolicy]
$creadListPrec :: ReadPrec [HttpRetryPolicy]
readPrec :: ReadPrec HttpRetryPolicy
$creadPrec :: ReadPrec HttpRetryPolicy
readList :: ReadS [HttpRetryPolicy]
$creadList :: ReadS [HttpRetryPolicy]
readsPrec :: Int -> ReadS HttpRetryPolicy
$creadsPrec :: Int -> ReadS HttpRetryPolicy
Prelude.Read, Int -> HttpRetryPolicy -> ShowS
[HttpRetryPolicy] -> ShowS
HttpRetryPolicy -> String
(Int -> HttpRetryPolicy -> ShowS)
-> (HttpRetryPolicy -> String)
-> ([HttpRetryPolicy] -> ShowS)
-> Show HttpRetryPolicy
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [HttpRetryPolicy] -> ShowS
$cshowList :: [HttpRetryPolicy] -> ShowS
show :: HttpRetryPolicy -> String
$cshow :: HttpRetryPolicy -> String
showsPrec :: Int -> HttpRetryPolicy -> ShowS
$cshowsPrec :: Int -> HttpRetryPolicy -> ShowS
Prelude.Show, (forall x. HttpRetryPolicy -> Rep HttpRetryPolicy x)
-> (forall x. Rep HttpRetryPolicy x -> HttpRetryPolicy)
-> Generic HttpRetryPolicy
forall x. Rep HttpRetryPolicy x -> HttpRetryPolicy
forall x. HttpRetryPolicy -> Rep HttpRetryPolicy x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep HttpRetryPolicy x -> HttpRetryPolicy
$cfrom :: forall x. HttpRetryPolicy -> Rep HttpRetryPolicy x
Prelude.Generic)

-- |
-- Create a value of 'HttpRetryPolicy' with all optional fields omitted.
--
-- Use <https://hackage.haskell.org/package/generic-lens generic-lens> or <https://hackage.haskell.org/package/optics optics> to modify other optional fields.
--
-- The following record fields are available, with the corresponding lenses provided
-- for backwards compatibility:
--
-- 'httpRetryEvents', 'httpRetryPolicy_httpRetryEvents' - Specify at least one of the following values.
--
-- -   __server-error__ – HTTP status codes 500, 501, 502, 503, 504, 505,
--     506, 507, 508, 510, and 511
--
-- -   __gateway-error__ – HTTP status codes 502, 503, and 504
--
-- -   __client-error__ – HTTP status code 409
--
-- -   __stream-error__ – Retry on refused stream
--
-- 'tcpRetryEvents', 'httpRetryPolicy_tcpRetryEvents' - Specify a valid value. The event occurs before any processing of a
-- request has started and is encountered when the upstream is temporarily
-- or permanently unavailable.
--
-- 'maxRetries', 'httpRetryPolicy_maxRetries' - The maximum number of retry attempts.
--
-- 'perRetryTimeout', 'httpRetryPolicy_perRetryTimeout' - The timeout for each retry attempt.
newHttpRetryPolicy ::
  -- | 'maxRetries'
  Prelude.Natural ->
  -- | 'perRetryTimeout'
  Duration ->
  HttpRetryPolicy
newHttpRetryPolicy :: Natural -> Duration -> HttpRetryPolicy
newHttpRetryPolicy Natural
pMaxRetries_ Duration
pPerRetryTimeout_ =
  HttpRetryPolicy' :: Maybe (NonEmpty Text)
-> Maybe (NonEmpty TcpRetryPolicyEvent)
-> Natural
-> Duration
-> HttpRetryPolicy
HttpRetryPolicy'
    { $sel:httpRetryEvents:HttpRetryPolicy' :: Maybe (NonEmpty Text)
httpRetryEvents = Maybe (NonEmpty Text)
forall a. Maybe a
Prelude.Nothing,
      $sel:tcpRetryEvents:HttpRetryPolicy' :: Maybe (NonEmpty TcpRetryPolicyEvent)
tcpRetryEvents = Maybe (NonEmpty TcpRetryPolicyEvent)
forall a. Maybe a
Prelude.Nothing,
      $sel:maxRetries:HttpRetryPolicy' :: Natural
maxRetries = Natural
pMaxRetries_,
      $sel:perRetryTimeout:HttpRetryPolicy' :: Duration
perRetryTimeout = Duration
pPerRetryTimeout_
    }

-- | Specify at least one of the following values.
--
-- -   __server-error__ – HTTP status codes 500, 501, 502, 503, 504, 505,
--     506, 507, 508, 510, and 511
--
-- -   __gateway-error__ – HTTP status codes 502, 503, and 504
--
-- -   __client-error__ – HTTP status code 409
--
-- -   __stream-error__ – Retry on refused stream
httpRetryPolicy_httpRetryEvents :: Lens.Lens' HttpRetryPolicy (Prelude.Maybe (Prelude.NonEmpty Prelude.Text))
httpRetryPolicy_httpRetryEvents :: (Maybe (NonEmpty Text) -> f (Maybe (NonEmpty Text)))
-> HttpRetryPolicy -> f HttpRetryPolicy
httpRetryPolicy_httpRetryEvents = (HttpRetryPolicy -> Maybe (NonEmpty Text))
-> (HttpRetryPolicy -> Maybe (NonEmpty Text) -> HttpRetryPolicy)
-> Lens
     HttpRetryPolicy
     HttpRetryPolicy
     (Maybe (NonEmpty Text))
     (Maybe (NonEmpty Text))
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\HttpRetryPolicy' {Maybe (NonEmpty Text)
httpRetryEvents :: Maybe (NonEmpty Text)
$sel:httpRetryEvents:HttpRetryPolicy' :: HttpRetryPolicy -> Maybe (NonEmpty Text)
httpRetryEvents} -> Maybe (NonEmpty Text)
httpRetryEvents) (\s :: HttpRetryPolicy
s@HttpRetryPolicy' {} Maybe (NonEmpty Text)
a -> HttpRetryPolicy
s {$sel:httpRetryEvents:HttpRetryPolicy' :: Maybe (NonEmpty Text)
httpRetryEvents = Maybe (NonEmpty Text)
a} :: HttpRetryPolicy) ((Maybe (NonEmpty Text) -> f (Maybe (NonEmpty Text)))
 -> HttpRetryPolicy -> f HttpRetryPolicy)
-> ((Maybe (NonEmpty Text) -> f (Maybe (NonEmpty Text)))
    -> Maybe (NonEmpty Text) -> f (Maybe (NonEmpty Text)))
-> (Maybe (NonEmpty Text) -> f (Maybe (NonEmpty Text)))
-> HttpRetryPolicy
-> f HttpRetryPolicy
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. AnIso
  (NonEmpty Text) (NonEmpty Text) (NonEmpty Text) (NonEmpty Text)
-> Iso
     (Maybe (NonEmpty Text))
     (Maybe (NonEmpty Text))
     (Maybe (NonEmpty Text))
     (Maybe (NonEmpty Text))
forall (f :: * -> *) (g :: * -> *) s t a b.
(Functor f, Functor g) =>
AnIso s t a b -> Iso (f s) (g t) (f a) (g b)
Lens.mapping AnIso
  (NonEmpty Text) (NonEmpty Text) (NonEmpty Text) (NonEmpty Text)
forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

-- | Specify a valid value. The event occurs before any processing of a
-- request has started and is encountered when the upstream is temporarily
-- or permanently unavailable.
httpRetryPolicy_tcpRetryEvents :: Lens.Lens' HttpRetryPolicy (Prelude.Maybe (Prelude.NonEmpty TcpRetryPolicyEvent))
httpRetryPolicy_tcpRetryEvents :: (Maybe (NonEmpty TcpRetryPolicyEvent)
 -> f (Maybe (NonEmpty TcpRetryPolicyEvent)))
-> HttpRetryPolicy -> f HttpRetryPolicy
httpRetryPolicy_tcpRetryEvents = (HttpRetryPolicy -> Maybe (NonEmpty TcpRetryPolicyEvent))
-> (HttpRetryPolicy
    -> Maybe (NonEmpty TcpRetryPolicyEvent) -> HttpRetryPolicy)
-> Lens
     HttpRetryPolicy
     HttpRetryPolicy
     (Maybe (NonEmpty TcpRetryPolicyEvent))
     (Maybe (NonEmpty TcpRetryPolicyEvent))
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\HttpRetryPolicy' {Maybe (NonEmpty TcpRetryPolicyEvent)
tcpRetryEvents :: Maybe (NonEmpty TcpRetryPolicyEvent)
$sel:tcpRetryEvents:HttpRetryPolicy' :: HttpRetryPolicy -> Maybe (NonEmpty TcpRetryPolicyEvent)
tcpRetryEvents} -> Maybe (NonEmpty TcpRetryPolicyEvent)
tcpRetryEvents) (\s :: HttpRetryPolicy
s@HttpRetryPolicy' {} Maybe (NonEmpty TcpRetryPolicyEvent)
a -> HttpRetryPolicy
s {$sel:tcpRetryEvents:HttpRetryPolicy' :: Maybe (NonEmpty TcpRetryPolicyEvent)
tcpRetryEvents = Maybe (NonEmpty TcpRetryPolicyEvent)
a} :: HttpRetryPolicy) ((Maybe (NonEmpty TcpRetryPolicyEvent)
  -> f (Maybe (NonEmpty TcpRetryPolicyEvent)))
 -> HttpRetryPolicy -> f HttpRetryPolicy)
-> ((Maybe (NonEmpty TcpRetryPolicyEvent)
     -> f (Maybe (NonEmpty TcpRetryPolicyEvent)))
    -> Maybe (NonEmpty TcpRetryPolicyEvent)
    -> f (Maybe (NonEmpty TcpRetryPolicyEvent)))
-> (Maybe (NonEmpty TcpRetryPolicyEvent)
    -> f (Maybe (NonEmpty TcpRetryPolicyEvent)))
-> HttpRetryPolicy
-> f HttpRetryPolicy
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. AnIso
  (NonEmpty TcpRetryPolicyEvent)
  (NonEmpty TcpRetryPolicyEvent)
  (NonEmpty TcpRetryPolicyEvent)
  (NonEmpty TcpRetryPolicyEvent)
-> Iso
     (Maybe (NonEmpty TcpRetryPolicyEvent))
     (Maybe (NonEmpty TcpRetryPolicyEvent))
     (Maybe (NonEmpty TcpRetryPolicyEvent))
     (Maybe (NonEmpty TcpRetryPolicyEvent))
forall (f :: * -> *) (g :: * -> *) s t a b.
(Functor f, Functor g) =>
AnIso s t a b -> Iso (f s) (g t) (f a) (g b)
Lens.mapping AnIso
  (NonEmpty TcpRetryPolicyEvent)
  (NonEmpty TcpRetryPolicyEvent)
  (NonEmpty TcpRetryPolicyEvent)
  (NonEmpty TcpRetryPolicyEvent)
forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

-- | The maximum number of retry attempts.
httpRetryPolicy_maxRetries :: Lens.Lens' HttpRetryPolicy Prelude.Natural
httpRetryPolicy_maxRetries :: (Natural -> f Natural) -> HttpRetryPolicy -> f HttpRetryPolicy
httpRetryPolicy_maxRetries = (HttpRetryPolicy -> Natural)
-> (HttpRetryPolicy -> Natural -> HttpRetryPolicy)
-> Lens HttpRetryPolicy HttpRetryPolicy Natural Natural
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\HttpRetryPolicy' {Natural
maxRetries :: Natural
$sel:maxRetries:HttpRetryPolicy' :: HttpRetryPolicy -> Natural
maxRetries} -> Natural
maxRetries) (\s :: HttpRetryPolicy
s@HttpRetryPolicy' {} Natural
a -> HttpRetryPolicy
s {$sel:maxRetries:HttpRetryPolicy' :: Natural
maxRetries = Natural
a} :: HttpRetryPolicy)

-- | The timeout for each retry attempt.
httpRetryPolicy_perRetryTimeout :: Lens.Lens' HttpRetryPolicy Duration
httpRetryPolicy_perRetryTimeout :: (Duration -> f Duration) -> HttpRetryPolicy -> f HttpRetryPolicy
httpRetryPolicy_perRetryTimeout = (HttpRetryPolicy -> Duration)
-> (HttpRetryPolicy -> Duration -> HttpRetryPolicy)
-> Lens HttpRetryPolicy HttpRetryPolicy Duration Duration
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\HttpRetryPolicy' {Duration
perRetryTimeout :: Duration
$sel:perRetryTimeout:HttpRetryPolicy' :: HttpRetryPolicy -> Duration
perRetryTimeout} -> Duration
perRetryTimeout) (\s :: HttpRetryPolicy
s@HttpRetryPolicy' {} Duration
a -> HttpRetryPolicy
s {$sel:perRetryTimeout:HttpRetryPolicy' :: Duration
perRetryTimeout = Duration
a} :: HttpRetryPolicy)

instance Core.FromJSON HttpRetryPolicy where
  parseJSON :: Value -> Parser HttpRetryPolicy
parseJSON =
    String
-> (Object -> Parser HttpRetryPolicy)
-> Value
-> Parser HttpRetryPolicy
forall a. String -> (Object -> Parser a) -> Value -> Parser a
Core.withObject
      String
"HttpRetryPolicy"
      ( \Object
x ->
          Maybe (NonEmpty Text)
-> Maybe (NonEmpty TcpRetryPolicyEvent)
-> Natural
-> Duration
-> HttpRetryPolicy
HttpRetryPolicy'
            (Maybe (NonEmpty Text)
 -> Maybe (NonEmpty TcpRetryPolicyEvent)
 -> Natural
 -> Duration
 -> HttpRetryPolicy)
-> Parser (Maybe (NonEmpty Text))
-> Parser
     (Maybe (NonEmpty TcpRetryPolicyEvent)
      -> Natural -> Duration -> HttpRetryPolicy)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x Object -> Text -> Parser (Maybe (NonEmpty Text))
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
Core..:? Text
"httpRetryEvents")
            Parser
  (Maybe (NonEmpty TcpRetryPolicyEvent)
   -> Natural -> Duration -> HttpRetryPolicy)
-> Parser (Maybe (NonEmpty TcpRetryPolicyEvent))
-> Parser (Natural -> Duration -> HttpRetryPolicy)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x Object -> Text -> Parser (Maybe (NonEmpty TcpRetryPolicyEvent))
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
Core..:? Text
"tcpRetryEvents")
            Parser (Natural -> Duration -> HttpRetryPolicy)
-> Parser Natural -> Parser (Duration -> HttpRetryPolicy)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x Object -> Text -> Parser Natural
forall a. FromJSON a => Object -> Text -> Parser a
Core..: Text
"maxRetries")
            Parser (Duration -> HttpRetryPolicy)
-> Parser Duration -> Parser HttpRetryPolicy
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x Object -> Text -> Parser Duration
forall a. FromJSON a => Object -> Text -> Parser a
Core..: Text
"perRetryTimeout")
      )

instance Prelude.Hashable HttpRetryPolicy

instance Prelude.NFData HttpRetryPolicy

instance Core.ToJSON HttpRetryPolicy where
  toJSON :: HttpRetryPolicy -> Value
toJSON HttpRetryPolicy' {Natural
Maybe (NonEmpty Text)
Maybe (NonEmpty TcpRetryPolicyEvent)
Duration
perRetryTimeout :: Duration
maxRetries :: Natural
tcpRetryEvents :: Maybe (NonEmpty TcpRetryPolicyEvent)
httpRetryEvents :: Maybe (NonEmpty Text)
$sel:perRetryTimeout:HttpRetryPolicy' :: HttpRetryPolicy -> Duration
$sel:maxRetries:HttpRetryPolicy' :: HttpRetryPolicy -> Natural
$sel:tcpRetryEvents:HttpRetryPolicy' :: HttpRetryPolicy -> Maybe (NonEmpty TcpRetryPolicyEvent)
$sel:httpRetryEvents:HttpRetryPolicy' :: HttpRetryPolicy -> Maybe (NonEmpty Text)
..} =
    [Pair] -> Value
Core.object
      ( [Maybe Pair] -> [Pair]
forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Text
"httpRetryEvents" Text -> NonEmpty Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=)
              (NonEmpty Text -> Pair) -> Maybe (NonEmpty Text) -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe (NonEmpty Text)
httpRetryEvents,
            (Text
"tcpRetryEvents" Text -> NonEmpty TcpRetryPolicyEvent -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=)
              (NonEmpty TcpRetryPolicyEvent -> Pair)
-> Maybe (NonEmpty TcpRetryPolicyEvent) -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe (NonEmpty TcpRetryPolicyEvent)
tcpRetryEvents,
            Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just (Text
"maxRetries" Text -> Natural -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= Natural
maxRetries),
            Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just
              (Text
"perRetryTimeout" Text -> Duration -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= Duration
perRetryTimeout)
          ]
      )