@@ -65,19 +65,21 @@ public static void RegisterAssembly(System.Reflection.Assembly assembly)
6565 {
6666 logger . LogDebug ( "Reinitializing..." ) ;
6767
68- if ( await syncRoot . WaitAsync ( tonOptions . TonClientTimeout ) )
68+ if ( ! await syncRoot . WaitAsync ( tonOptions . ConcurrencyTimeout ) )
6969 {
70- if ( client != null )
71- {
72- tonlib_client_json_destroy ( client . Value ) ;
73- client = null ;
74- }
75-
76- initialized = false ;
77- needReinit = false ;
70+ throw new TimeoutException ( "Failed while waiting for semaphore" ) ;
71+ }
7872
79- syncRoot . Release ( ) ;
73+ if ( client != null )
74+ {
75+ tonlib_client_json_destroy ( client . Value ) ;
76+ client = null ;
8077 }
78+
79+ initialized = false ;
80+ needReinit = false ;
81+
82+ syncRoot . Release ( ) ;
8183 }
8284
8385 if ( initialized )
@@ -112,15 +114,22 @@ public async Task<TResponse> Execute<TResponse>(RequestBase<TResponse> request)
112114 {
113115 if ( client == null )
114116 {
115- if ( await syncRoot . WaitAsync ( tonOptions . TonClientTimeout ) )
117+ if ( ! await syncRoot . WaitAsync ( tonOptions . ConcurrencyTimeout ) )
118+ {
119+ throw new TimeoutException ( "Failed while waiting for semaphore" ) ;
120+ }
121+
122+ try
116123 {
117124 if ( client == null )
118125 {
119126 tonlib_client_set_verbosity_level ( tonOptions . VerbosityLevel ) ;
120127 client = tonlib_client_json_create ( ) ;
121128 initialized = false ;
122129 }
123-
130+ }
131+ finally
132+ {
124133 syncRoot . Release ( ) ;
125134 }
126135 }
@@ -135,26 +144,26 @@ public async Task<TResponse> Execute<TResponse>(RequestBase<TResponse> request)
135144 throw new InvalidOperationException ( $ "Must call { nameof ( InitIfNeeded ) } () first") ;
136145 }
137146
138- if ( await syncRoot . WaitAsync ( tonOptions . TonClientTimeout ) )
147+ if ( ! await syncRoot . WaitAsync ( tonOptions . ConcurrencyTimeout ) )
139148 {
140- try
141- {
142- var res = await ExecuteInternalAsync ( request ) ;
149+ throw new TimeoutException ( "Failed while waiting for semaphore" ) ;
150+ }
143151
144- if ( request is Init )
145- {
146- initialized = true ;
147- }
152+ try
153+ {
154+ var res = await ExecuteInternalAsync ( request ) ;
148155
149- return res ;
150- }
151- finally
156+ if ( request is Init )
152157 {
153- syncRoot . Release ( ) ;
158+ initialized = true ;
154159 }
155- }
156160
157- throw new TimeoutException ( "Failed while waiting for semaphore" ) ;
161+ return res ;
162+ }
163+ finally
164+ {
165+ syncRoot . Release ( ) ;
166+ }
158167 }
159168
160169 public decimal ConvertFromNanoTon ( long nano )
@@ -235,7 +244,7 @@ protected async Task<TResponse> ExecuteInternalAsync<TResponse>(RequestBase<TRes
235244
236245 tonlib_client_json_send ( client . Value , reqText ) ;
237246
238- var endOfLoop = DateTimeOffset . UtcNow . Add ( tonOptions . TonClientTimeout ) ;
247+ var endOfLoop = DateTimeOffset . UtcNow . Add ( request is Sync ? tonOptions . TonClientSyncTimeout : tonOptions . TonClientTimeout ) ;
239248
240249 while ( true )
241250 {
0 commit comments