Skip to content

Commit 4207138

Browse files
committed
TonOptions: ConcurrencyTimeout and TonClientSyncTimeout added
1 parent 216c93d commit 4207138

File tree

2 files changed

+56
-28
lines changed

2 files changed

+56
-28
lines changed

TonLibDotNet/TonClient.cs

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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
{

TonLibDotNet/TonOptions.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,29 @@ public class TonOptions
2121
public bool UseMainnet { get; set; } = true;
2222

2323
/// <summary>
24-
/// Max amount of time TonClient will wait for valid (synced!) response from tonlib.
24+
/// Max amount of time TonClient will wait for access to tonlib when used as singleton in multithreaded scenarios.
2525
/// </summary>
26+
/// <remarks>See also <see cref="TonClientTimeout"/> and <see cref="TonClientSyncTimeout"/>.</remarks>
27+
/// <seealso cref="TonClientTimeout"/>
28+
/// <seealso cref="TonClientSyncTimeout"/>
29+
public TimeSpan ConcurrencyTimeout { get; set; } = TimeSpan.FromSeconds(21); // A bit more than TonClientTimeout
30+
31+
/// <summary>
32+
/// Max amount of time TonClient will wait for valid (synced!) response from tonlib (except <see cref="Requests.Sync"/> request).
33+
/// </summary>
34+
/// <remarks>See also <see cref="TonClientSyncTimeout"/> and <see cref="SemaphoreTimeout"/>.</remarks>
35+
/// <seealso cref="TonClientSyncTimeout"/>
36+
/// <seealso cref="SemaphoreTimeout"/>
2637
public TimeSpan TonClientTimeout { get; set; } = TimeSpan.FromSeconds(20);
2738

39+
/// <summary>
40+
/// Max amount of time TonClient will wait for valid (synced!) response from tonlib for <see cref="Requests.Sync"/> request.
41+
/// </summary>
42+
/// <remarks>See also <see cref="TonClientTimeout"/> and <see cref="SemaphoreTimeout"/>.</remarks>
43+
/// <seealso cref="TonClientTimeout"/>
44+
/// <seealso cref="SemaphoreTimeout"/>
45+
public TimeSpan TonClientSyncTimeout { get; set; } = TimeSpan.FromSeconds(60);
46+
2847
/// <summary>
2948
/// TonLib timeout when making calls to tonlib/LiteServer.
3049
/// </summary>

0 commit comments

Comments
 (0)