@@ -36,19 +36,22 @@ Retries
3636Connection retries
3737^^^^^^^^^^^^^^^^^^
3838
39- With :code: `reconnecting=True `, gql will use the `backoff `_ module to repeatedly try to connect with
40- exponential backoff and jitter with a maximum delay of 60 seconds by default.
39+ With :code: `reconnecting=True `, gql will use the `tenacity `_ module to repeatedly
40+ try to connect with exponential backoff and jitter with a maximum delay of
41+ 60 seconds by default.
4142
4243You can change the default reconnecting profile by providing your own
43- backoff decorator to the :code: `retry_connect ` argument.
44+ retry decorator (from tenacity) to the :code: `retry_connect ` argument.
4445
4546.. code-block :: python
4647
48+ from tenacity import retry, retry_if_exception_type, wait_exponential
49+
4750 # Here wait maximum 5 minutes between connection retries
48- retry_connect = backoff.on_exception (
49- backoff.expo, # wait generator (here: exponential backoff )
50- Exception , # which exceptions should cause a retry (here: everything)
51- max_value = 300 , # max wait time in seconds
51+ retry_connect = retry (
52+ # which exceptions should cause a retry (here: everything )
53+ retry = retry_if_exception_type( Exception ),
54+ wait = wait_exponential( max = 300 ), # max wait time in seconds
5255 )
5356 session = await client.connect_async(
5457 reconnecting = True ,
@@ -66,32 +69,49 @@ There is no retry in case of a :code:`TransportQueryError` exception as it indic
6669the connection to the backend is working correctly.
6770
6871You can change the default execute retry profile by providing your own
69- backoff decorator to the :code: `retry_execute ` argument.
72+ retry decorator (from tenacity) to the :code: `retry_execute ` argument.
7073
7174.. code-block :: python
7275
76+ from tenacity import (
77+ retry,
78+ retry_if_exception_type,
79+ stop_after_attempt,
80+ wait_exponential,
81+ )
82+
7383 # Here Only 3 tries for execute calls
74- retry_execute = backoff.on_exception (
75- backoff.expo ,
76- Exception ,
77- max_tries = 3 ,
84+ retry_execute = retry (
85+ retry = retry_if_exception_type( Exception ) ,
86+ stop = stop_after_attempt( 3 ) ,
87+ wait = wait_exponential() ,
7888 )
7989 session = await client.connect_async(
8090 reconnecting = True ,
8191 retry_execute = retry_execute,
8292 )
8393
84- If you don't want any retry on the execute calls, you can disable the retries with :code: `retry_execute=False `
94+ If you don't want any retry on the execute calls, you can disable the retries
95+ with :code: `retry_execute=False `
8596
8697.. note ::
8798 If you want to retry even with :code: `TransportQueryError ` exceptions,
88- then you need to make your own backoff decorator on your own method:
99+ then you need to make your own retry decorator (from tenacity) on your own method:
89100
90101 .. code-block :: python
91102
92- @backoff.on_exception (backoff.expo,
93- Exception ,
94- max_tries = 3 )
103+ from tenacity import (
104+ retry,
105+ retry_if_exception_type,
106+ stop_after_attempt,
107+ wait_exponential,
108+ )
109+
110+ @retry (
111+ retry = retry_if_exception_type(Exception ),
112+ stop = stop_after_attempt(3 ),
113+ wait = wait_exponential(),
114+ )
95115 async def execute_with_retry (session , query ):
96116 return await session.execute(query)
97117
@@ -100,14 +120,25 @@ Subscription retries
100120
101121There is no :code: `retry_subscribe ` as it is not feasible with async generators.
102122If you want retries for your subscriptions, then you can do it yourself
103- with backoff decorators on your methods.
123+ with retry decorators (from tenacity) on your methods.
104124
105125.. code-block :: python
106126
107- @backoff.on_exception (backoff.expo,
108- Exception ,
109- max_tries = 3 ,
110- giveup = lambda e : isinstance (e, TransportQueryError))
127+ from tenacity import (
128+ retry,
129+ retry_if_exception_type,
130+ retry_unless_exception_type,
131+ stop_after_attempt,
132+ wait_exponential,
133+ )
134+ from gql.transport.exceptions import TransportQueryError
135+
136+ @retry (
137+ retry = retry_if_exception_type(Exception )
138+ & retry_unless_exception_type(TransportQueryError),
139+ stop = stop_after_attempt(3 ),
140+ wait = wait_exponential(),
141+ )
111142 async def execute_subscription1 (session ):
112143 async for result in session.subscribe(subscription1):
113144 print (result)
@@ -123,4 +154,4 @@ Console example
123154.. literalinclude :: ../code_examples/console_async.py
124155
125156.. _difficult to manage : https://github.com/graphql-python/gql/issues/179
126- .. _ backoff : https://github.com/litl/backoff
157+ .. _ tenacity : https://github.com/jd/tenacity
0 commit comments