14
14
*/
15
15
16
16
using System ;
17
+ using System . Diagnostics ;
18
+ using System . Linq ;
17
19
using System . Threading ;
18
20
using System . Threading . Tasks ;
19
21
using MongoDB . Bson ;
20
22
using MongoDB . Driver . Core . Misc ;
23
+ using MongoDB . Driver . Core . Servers ;
21
24
using MongoDB . Driver . Core . WireProtocol ;
22
25
using MongoDB . Libmongocrypt ;
23
26
@@ -181,6 +184,7 @@ private void ProcessNeedMongoMarkingsState(CryptContext context, string database
181
184
catch ( TimeoutException ) when ( attempt == 1 )
182
185
{
183
186
_mongocryptdFactory . SpawnMongocryptdProcessIfRequired ( ) ;
187
+ WaitForMongocryptdReady ( ) ;
184
188
}
185
189
}
186
190
@@ -205,6 +209,7 @@ private async Task ProcessNeedMongoMarkingsStateAsync(CryptContext context, stri
205
209
catch ( TimeoutException ) when ( attempt == 1 )
206
210
{
207
211
_mongocryptdFactory . SpawnMongocryptdProcessIfRequired ( ) ;
212
+ await WaitForMongocryptdReadyAsync ( ) . ConfigureAwait ( false ) ;
208
213
}
209
214
}
210
215
@@ -223,5 +228,35 @@ private void RestoreDbNodeInResponse(BsonDocument request, BsonDocument response
223
228
}
224
229
}
225
230
}
231
+
232
+ private void WaitForMongocryptdReady ( )
233
+ {
234
+ var stopwatch = Stopwatch . StartNew ( ) ;
235
+ while ( stopwatch . Elapsed < TimeSpan . FromSeconds ( 5 ) )
236
+ {
237
+ var clusterDescription = _mongocryptdClient . Cluster ? . Description ;
238
+ var mongocryptdServer = clusterDescription ? . Servers ? . FirstOrDefault ( ) ;
239
+ if ( mongocryptdServer != null && mongocryptdServer . Type != ServerType . Unknown )
240
+ {
241
+ return ;
242
+ }
243
+ Thread . Sleep ( TimeSpan . FromMilliseconds ( 5 ) ) ;
244
+ }
245
+ }
246
+
247
+ private async Task WaitForMongocryptdReadyAsync ( )
248
+ {
249
+ var stopwatch = Stopwatch . StartNew ( ) ;
250
+ while ( stopwatch . Elapsed < TimeSpan . FromSeconds ( 5 ) )
251
+ {
252
+ var clusterDescription = _mongocryptdClient . Cluster ? . Description ;
253
+ var mongocryptdServer = clusterDescription ? . Servers ? . FirstOrDefault ( ) ;
254
+ if ( mongocryptdServer != null && mongocryptdServer . Type != ServerType . Unknown )
255
+ {
256
+ return ;
257
+ }
258
+ await Task . Delay ( TimeSpan . FromMilliseconds ( 5 ) ) . ConfigureAwait ( false ) ;
259
+ }
260
+ }
226
261
}
227
262
}
0 commit comments