{-# 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-2023 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay
-- 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.Core.Lens.Internal as Lens
import qualified Amazonka.Data as Data
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
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]
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
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. 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'
    { $sel:httpRetryEvents:HttpRetryPolicy' :: Maybe (NonEmpty Text)
httpRetryEvents = forall a. Maybe a
Prelude.Nothing,
      $sel:tcpRetryEvents:HttpRetryPolicy' :: Maybe (NonEmpty TcpRetryPolicyEvent)
tcpRetryEvents = 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 :: Lens' HttpRetryPolicy (Maybe (NonEmpty Text))
httpRetryPolicy_httpRetryEvents = 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) forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. 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 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 :: Lens' HttpRetryPolicy (Maybe (NonEmpty TcpRetryPolicyEvent))
httpRetryPolicy_tcpRetryEvents = 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) forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. 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 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 :: Lens' HttpRetryPolicy Natural
httpRetryPolicy_maxRetries = 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 :: Lens' HttpRetryPolicy Duration
httpRetryPolicy_perRetryTimeout = 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 Data.FromJSON HttpRetryPolicy where
  parseJSON :: Value -> Parser HttpRetryPolicy
parseJSON =
    forall a. String -> (Object -> Parser a) -> Value -> Parser a
Data.withObject
      String
"HttpRetryPolicy"
      ( \Object
x ->
          Maybe (NonEmpty Text)
-> Maybe (NonEmpty TcpRetryPolicyEvent)
-> Natural
-> Duration
-> HttpRetryPolicy
HttpRetryPolicy'
            forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
Data..:? Key
"httpRetryEvents")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
Data..:? Key
"tcpRetryEvents")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser a
Data..: Key
"maxRetries")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser a
Data..: Key
"perRetryTimeout")
      )

instance Prelude.Hashable HttpRetryPolicy where
  hashWithSalt :: Int -> HttpRetryPolicy -> Int
hashWithSalt Int
_salt 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)
..} =
    Int
_salt
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe (NonEmpty Text)
httpRetryEvents
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe (NonEmpty TcpRetryPolicyEvent)
tcpRetryEvents
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Natural
maxRetries
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Duration
perRetryTimeout

instance Prelude.NFData HttpRetryPolicy where
  rnf :: HttpRetryPolicy -> ()
rnf 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)
..} =
    forall a. NFData a => a -> ()
Prelude.rnf Maybe (NonEmpty Text)
httpRetryEvents
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe (NonEmpty TcpRetryPolicyEvent)
tcpRetryEvents
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Natural
maxRetries
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Duration
perRetryTimeout

instance Data.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
Data.object
      ( forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Key
"httpRetryEvents" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=)
              forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe (NonEmpty Text)
httpRetryEvents,
            (Key
"tcpRetryEvents" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=)
              forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe (NonEmpty TcpRetryPolicyEvent)
tcpRetryEvents,
            forall a. a -> Maybe a
Prelude.Just (Key
"maxRetries" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..= Natural
maxRetries),
            forall a. a -> Maybe a
Prelude.Just
              (Key
"perRetryTimeout" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..= Duration
perRetryTimeout)
          ]
      )