3
3
from functools import partial
4
4
from itertools import cycle
5
5
from typing import Optional
6
+ from websockets .exceptions import ConnectionClosed
6
7
7
8
from async_substrate_interface .async_substrate import AsyncSubstrateInterface
8
9
from async_substrate_interface .errors import MaxRetriesExceeded
9
10
from async_substrate_interface .sync_substrate import SubstrateInterface
10
11
12
+ logger = logging .getLogger ("async_substrate_interface" )
13
+
11
14
12
15
RETRY_METHODS = [
13
16
"_get_block_handler" ,
@@ -106,65 +109,64 @@ def __init__(
106
109
max_retries = max_retries ,
107
110
)
108
111
initialized = True
112
+ logger .info (f"Connected to { chain_url } " )
109
113
break
110
114
except ConnectionError :
111
- logging .warning (f"Unable to connect to { chain_url } " )
115
+ logger .warning (f"Unable to connect to { chain_url } " )
112
116
if not initialized :
113
117
raise ConnectionError (
114
118
f"Unable to connect at any chains specified: { [url ] + fallback_chains } "
115
119
)
120
+ retry_methods = ["connect" ] + RETRY_METHODS
116
121
self ._original_methods = {
117
- method : getattr (self , method ) for method in RETRY_METHODS
122
+ method : getattr (self , method ) for method in retry_methods
118
123
}
119
- for method in RETRY_METHODS :
124
+ for method in retry_methods :
120
125
setattr (self , method , partial (self ._retry , method ))
121
126
122
127
def _retry (self , method , * args , ** kwargs ):
123
128
try :
124
129
method_ = self ._original_methods [method ]
125
130
return method_ (* args , ** kwargs )
126
- except (MaxRetriesExceeded , ConnectionError , ConnectionRefusedError ) as e :
131
+ except (MaxRetriesExceeded , ConnectionError , EOFError , ConnectionClosed ) as e :
127
132
try :
128
133
self ._reinstantiate_substrate (e )
129
- method_ = getattr ( self , method )
130
- return self . _retry ( method_ (* args , ** kwargs ) )
134
+ method_ = self . _original_methods [ method ]
135
+ return method_ (* args , ** kwargs )
131
136
except StopIteration :
132
- logging .error (
137
+ logger .error (
133
138
f"Max retries exceeded with { self .url } . No more fallback chains."
134
139
)
135
140
raise MaxRetriesExceeded
136
141
137
142
def _retry_property (self , property_ ):
138
143
try :
139
144
return getattr (self , property_ )
140
- except (MaxRetriesExceeded , ConnectionError , ConnectionRefusedError ) as e :
145
+ except (MaxRetriesExceeded , ConnectionError , EOFError , ConnectionClosed ) as e :
141
146
try :
142
147
self ._reinstantiate_substrate (e )
143
148
return self ._retry_property (property_ )
144
149
except StopIteration :
145
- logging .error (
150
+ logger .error (
146
151
f"Max retries exceeded with { self .url } . No more fallback chains."
147
152
)
148
153
raise MaxRetriesExceeded
149
154
150
155
def _reinstantiate_substrate (self , e : Optional [Exception ] = None ) -> None :
151
156
next_network = next (self .fallback_chains )
157
+ self .ws .close ()
152
158
if e .__class__ == MaxRetriesExceeded :
153
- logging .error (
159
+ logger .error (
154
160
f"Max retries exceeded with { self .url } . Retrying with { next_network } ."
155
161
)
156
162
else :
157
- print (f"Connection error. Trying again with { next_network } " )
158
- super ().__init__ (
159
- url = next_network ,
160
- ss58_format = self .ss58_format ,
161
- type_registry = self .type_registry ,
162
- use_remote_preset = self .use_remote_preset ,
163
- chain_name = self .chain_name ,
164
- _mock = self ._mock ,
165
- retry_timeout = self .retry_timeout ,
166
- max_retries = self .max_retries ,
167
- )
163
+ logger .error (f"Connection error. Trying again with { next_network } " )
164
+ self .url = next_network
165
+ self .chain_endpoint = next_network
166
+ self .initialized = False
167
+ self .ws = self .connect (init = True )
168
+ if not self ._mock :
169
+ self .initialize ()
168
170
169
171
170
172
class RetryAsyncSubstrate (AsyncSubstrateInterface ):
@@ -213,7 +215,7 @@ def __init__(
213
215
def _reinstantiate_substrate (self , e : Optional [Exception ] = None ) -> None :
214
216
next_network = next (self .fallback_chains )
215
217
if e .__class__ == MaxRetriesExceeded :
216
- logging .error (
218
+ logger .error (
217
219
f"Max retries exceeded with { self .url } . Retrying with { next_network } ."
218
220
)
219
221
else :
@@ -228,12 +230,22 @@ def _reinstantiate_substrate(self, e: Optional[Exception] = None) -> None:
228
230
retry_timeout = self .retry_timeout ,
229
231
max_retries = self .max_retries ,
230
232
)
233
+ self ._original_methods = {
234
+ method : getattr (self , method ) for method in RETRY_METHODS
235
+ }
236
+ for method in RETRY_METHODS :
237
+ setattr (self , method , partial (self ._retry , method ))
231
238
232
239
async def _retry (self , method , * args , ** kwargs ):
233
240
try :
234
241
method_ = self ._original_methods [method ]
235
242
return await method_ (* args , ** kwargs )
236
- except (MaxRetriesExceeded , ConnectionError , ConnectionRefusedError ) as e :
243
+ except (
244
+ MaxRetriesExceeded ,
245
+ ConnectionError ,
246
+ ConnectionRefusedError ,
247
+ EOFError ,
248
+ ) as e :
237
249
try :
238
250
self ._reinstantiate_substrate (e )
239
251
await self .initialize ()
@@ -243,7 +255,7 @@ async def _retry(self, method, *args, **kwargs):
243
255
else :
244
256
return method_ (* args , ** kwargs )
245
257
except StopIteration :
246
- logging .error (
258
+ logger .error (
247
259
f"Max retries exceeded with { self .url } . No more fallback chains."
248
260
)
249
261
raise MaxRetriesExceeded
@@ -256,7 +268,7 @@ async def _retry_property(self, property_):
256
268
self ._reinstantiate_substrate (e )
257
269
return await self ._retry_property (property_ )
258
270
except StopIteration :
259
- logging .error (
271
+ logger .error (
260
272
f"Max retries exceeded with { self .url } . No more fallback chains."
261
273
)
262
274
raise MaxRetriesExceeded
0 commit comments