@@ -122,13 +122,27 @@ public async Task<TResult> ExecuteClientOperationAsync<TResult>(AsyncOperation<T
122122 {
123123 if ( retryOnFailure )
124124 {
125- client = await Reconnect ( client ) ;
126- try
125+ try {
126+ client = await Reconnect ( client ) ;
127+ return await operation ( client ) ;
128+ } catch ( TException retryEx )
127129 {
128- var resp = await operation ( client ) ;
129- return resp ;
130+ throw new TException ( errMsg , retryEx ) ;
130131 }
131- catch ( TException retryEx )
132+ }
133+ else
134+ {
135+ throw new TException ( errMsg , ex ) ;
136+ }
137+ }
138+ catch ( Exception ex )
139+ {
140+ if ( retryOnFailure )
141+ {
142+ try {
143+ client = await Reconnect ( client ) ;
144+ return await operation ( client ) ;
145+ } catch ( TException retryEx )
132146 {
133147 throw new TException ( errMsg , retryEx ) ;
134148 }
@@ -179,7 +193,17 @@ public async Task Open(CancellationToken cancellationToken = default)
179193 {
180194 for ( var index = 0 ; index < _poolSize ; index ++ )
181195 {
182- _clients . Add ( await CreateAndOpen ( _host , _port , _enableRpcCompression , _timeout , cancellationToken ) ) ;
196+ try
197+ {
198+ _clients . Add ( await CreateAndOpen ( _host , _port , _enableRpcCompression , _timeout , cancellationToken ) ) ;
199+ }
200+ catch ( Exception e )
201+ {
202+ if ( _debugMode )
203+ {
204+ _logger . LogWarning ( e , "Currently connecting to {0}:{1} failed" , _host , _port ) ;
205+ }
206+ }
183207 }
184208 }
185209 else
@@ -217,47 +241,63 @@ public async Task Open(CancellationToken cancellationToken = default)
217241
218242 if ( _clients . ClientQueue . Count != _poolSize )
219243 {
220- throw new TException ( string . Format ( "Error occurs when opening session pool. Client pool size is not equal to the expected size. Client pool size: {0}, expected size: {1}" , _clients . ClientQueue . Count , _poolSize ) , null ) ;
244+ throw new TException ( string . Format ( "Error occurs when opening session pool. Client pool size is not equal to the expected size. Client pool size: {0}, expected size: {1}, Please check the server status " , _clients . ClientQueue . Count , _poolSize ) , null ) ;
221245 }
222246 _isClose = false ;
223247 }
224248
225249
226250 public async Task < Client > Reconnect ( Client originalClient = null , CancellationToken cancellationToken = default )
227251 {
228- if ( _nodeUrls . Count == 0 )
229- {
230- await Open ( _enableRpcCompression ) ;
231- return _clients . Take ( ) ;
232- }
233-
234- originalClient . Transport . Close ( ) ;
252+ originalClient ? . Transport . Close ( ) ;
235253
236- int startIndex = _endPoints . FindIndex ( x => x . Ip == originalClient . EndPoint . Ip && x . Port == originalClient . EndPoint . Port ) ;
237- if ( startIndex == - 1 )
238- {
239- throw new ArgumentException ( $ "The original client is not in the list of endpoints. Original client: { originalClient . EndPoint . Ip } :{ originalClient . EndPoint . Port } ") ;
240- }
241-
242- for ( int attempt = 1 ; attempt <= RetryNum ; attempt ++ )
254+ if ( _nodeUrls . Count == 0 )
243255 {
244- for ( int i = 0 ; i < _endPoints . Count ; i ++ )
256+ for ( int attempt = 1 ; attempt <= RetryNum ; attempt ++ )
245257 {
246- int j = ( startIndex + i ) % _endPoints . Count ;
247258 try
248259 {
249- var client = await CreateAndOpen ( _endPoints [ j ] . Ip , _endPoints [ j ] . Port , _enableRpcCompression , _timeout , cancellationToken ) ;
260+ var client = await CreateAndOpen ( _host , _port , _enableRpcCompression , _timeout , cancellationToken ) ;
250261 return client ;
251262 }
252263 catch ( Exception e )
253264 {
254265 if ( _debugMode )
255266 {
256- _logger . LogWarning ( e , "Attempt connecting to {0}:{1} failed" , _endPoints [ j ] . Ip , _endPoints [ j ] . Port ) ;
267+ _logger . LogWarning ( e , "Attempt reconnecting to {0}:{1} failed" , _host , _port ) ;
268+ }
269+ }
270+ }
271+ }
272+ else
273+ {
274+ int startIndex = _endPoints . FindIndex ( x => x . Ip == originalClient . EndPoint . Ip && x . Port == originalClient . EndPoint . Port ) ;
275+ if ( startIndex == - 1 )
276+ {
277+ throw new ArgumentException ( $ "The original client is not in the list of endpoints. Original client: { originalClient . EndPoint . Ip } :{ originalClient . EndPoint . Port } ") ;
278+ }
279+
280+ for ( int attempt = 1 ; attempt <= RetryNum ; attempt ++ )
281+ {
282+ for ( int i = 0 ; i < _endPoints . Count ; i ++ )
283+ {
284+ int j = ( startIndex + i ) % _endPoints . Count ;
285+ try
286+ {
287+ var client = await CreateAndOpen ( _endPoints [ j ] . Ip , _endPoints [ j ] . Port , _enableRpcCompression , _timeout , cancellationToken ) ;
288+ return client ;
289+ }
290+ catch ( Exception e )
291+ {
292+ if ( _debugMode )
293+ {
294+ _logger . LogWarning ( e , "Attempt connecting to {0}:{1} failed" , _endPoints [ j ] . Ip , _endPoints [ j ] . Port ) ;
295+ }
257296 }
258297 }
259298 }
260299 }
300+
261301 throw new TException ( "Error occurs when reconnecting session pool. Could not connect to any server" , null ) ;
262302 }
263303
0 commit comments