From 1c5acde7ad08b8bc8c4cae68e2e2c955aaab65b0 Mon Sep 17 00:00:00 2001 From: Daniel <95646168+daniel-statsig@users.noreply.github.com> Date: Tue, 23 Apr 2024 18:08:11 -0700 Subject: [PATCH] chore: ConfigureAwait(false) for all await calls --- StatsigRedis/RedisDataStore.cs | 4 +- .../src/Statsig/Client/ClientDriver.cs | 10 ++--- .../src/Statsig/Client/StatsigClient.cs | 6 +-- .../src/Statsig/Lib/ErrorBoundary.cs | 9 ++-- .../src/Statsig/Network/EventLogger.cs | 8 ++-- .../src/Statsig/Network/RequestDispatcher.cs | 18 ++++---- .../Statsig/Server/Evaluation/Evaluator.cs | 4 +- .../Statsig/Server/Evaluation/SpecStore.cs | 42 +++++++++---------- .../src/Statsig/Server/ServerDriver.cs | 12 +++--- .../src/Statsig/Server/StatsigServer.cs | 20 ++++----- 10 files changed, 67 insertions(+), 66 deletions(-) diff --git a/StatsigRedis/RedisDataStore.cs b/StatsigRedis/RedisDataStore.cs index f11a6fa..7fcc568 100644 --- a/StatsigRedis/RedisDataStore.cs +++ b/StatsigRedis/RedisDataStore.cs @@ -40,13 +40,13 @@ public bool SupportsPollingUpdates(string key) public async Task Get(string key) { - var result = await _database.StringGetAsync(key); + var result = await _database.StringGetAsync(key).ConfigureAwait(false); return result.IsNull ? null : result.ToString(); } public async Task Set(string key, string value) { - await _database.StringSetAsync(key, value); + await _database.StringSetAsync(key, value).ConfigureAwait(false); } // noop diff --git a/dotnet-statsig/src/Statsig/Client/ClientDriver.cs b/dotnet-statsig/src/Statsig/Client/ClientDriver.cs index f278334..3b7a89a 100644 --- a/dotnet-statsig/src/Statsig/Client/ClientDriver.cs +++ b/dotnet-statsig/src/Statsig/Client/ClientDriver.cs @@ -96,7 +96,7 @@ public async Task Initialize(StatsigUser? user) ["statsigMetadata"] = GetStatsigMetadata(), }, timeoutInMs: _options.ClientRequestTimeoutMs - ); + ).ConfigureAwait(false); if (response == null) { return; @@ -107,9 +107,9 @@ public async Task Initialize(StatsigUser? user) public async Task Shutdown() { - await _eventLogger.Shutdown(); + await _eventLogger.Shutdown().ConfigureAwait(false); #if SUPPORTS_ASYNC_DISPOSAL - await ((IAsyncDisposable)this).DisposeAsync(); + await ((IAsyncDisposable)this).DisposeAsync().ConfigureAwait(false); #else ((IDisposable)this).Dispose(); #endif @@ -204,7 +204,7 @@ public Layer GetLayer(string layerName) public async Task UpdateUser(StatsigUser newUser) { _statsigMetadata = null; - await Initialize(newUser); + await Initialize(newUser).ConfigureAwait(false); } public void LogEvent( @@ -257,7 +257,7 @@ async ValueTask IAsyncDisposable.DisposeAsync() throw new ObjectDisposedException("ClientDriver"); } - await _eventLogger.FlushEvents(); + await _eventLogger.FlushEvents().ConfigureAwait(false); _disposed = true; } #endif diff --git a/dotnet-statsig/src/Statsig/Client/StatsigClient.cs b/dotnet-statsig/src/Statsig/Client/StatsigClient.cs index 7cfb74c..7445287 100644 --- a/dotnet-statsig/src/Statsig/Client/StatsigClient.cs +++ b/dotnet-statsig/src/Statsig/Client/StatsigClient.cs @@ -16,13 +16,13 @@ public static async Task Initialize(string clientKey, StatsigUser? user = null, } _singleDriver = new ClientDriver(clientKey, options); - await _singleDriver.Initialize(user); + await _singleDriver.Initialize(user).ConfigureAwait(false); } public static async Task Shutdown() { EnsureInitialized(); - await _singleDriver!.Shutdown(); + await _singleDriver!.Shutdown().ConfigureAwait(false); _singleDriver = null; } @@ -84,7 +84,7 @@ public static async Task UpdateUser(StatsigUser user) throw new InvalidOperationException("user cannot be null."); } EnsureInitialized(); - await _singleDriver!.UpdateUser(user); + await _singleDriver!.UpdateUser(user).ConfigureAwait(false); } static void EnsureInitialized() diff --git a/dotnet-statsig/src/Statsig/Lib/ErrorBoundary.cs b/dotnet-statsig/src/Statsig/Lib/ErrorBoundary.cs index bdd352b..d62dab9 100644 --- a/dotnet-statsig/src/Statsig/Lib/ErrorBoundary.cs +++ b/dotnet-statsig/src/Statsig/Lib/ErrorBoundary.cs @@ -26,9 +26,10 @@ public async Task Swallow(string tag, Func task) { await Capture(tag, async () => { - await task(); + await task().ConfigureAwait(false); return true; - }, () => false); + }, () => false) + .ConfigureAwait(false); } public void Swallow(string tag, Action task) @@ -44,7 +45,7 @@ public async Task Capture(string tag, Func> task, Func recover) { try { - var result = await task(); + var result = await task().ConfigureAwait(false); return result; } catch (Exception ex) @@ -123,7 +124,7 @@ public async void LogException(string tag, Exception ex) request.Headers.Add(kv.Key, kv.Value); } - await client.SendAsync(request); + await client.SendAsync(request).ConfigureAwait(false); } catch { diff --git a/dotnet-statsig/src/Statsig/Network/EventLogger.cs b/dotnet-statsig/src/Statsig/Network/EventLogger.cs index 2b5684c..a93e3a3 100644 --- a/dotnet-statsig/src/Statsig/Network/EventLogger.cs +++ b/dotnet-statsig/src/Statsig/Network/EventLogger.cs @@ -106,7 +106,7 @@ private async Task BackgroundPeriodicFlushTask(int maxThresholdSecs, Cancellatio var timeUntilNextFlush = _lastFlushTime.AddSeconds(maxThresholdSecs) - DateTime.UtcNow; if (timeUntilNextFlush > TimeSpan.Zero) { - await Task.Delay(timeUntilNextFlush, cancellationToken); + await Task.Delay(timeUntilNextFlush, cancellationToken).ConfigureAwait(false); } // While waiting, a flush may have been triggered because the queue filled up, @@ -114,7 +114,7 @@ private async Task BackgroundPeriodicFlushTask(int maxThresholdSecs, Cancellatio // event flush, and if so, then trigger a flush. if (_lastFlushTime.AddSeconds(maxThresholdSecs) <= DateTime.UtcNow) { - await FlushEvents(); + await FlushEvents().ConfigureAwait(false); } } catch (TaskCanceledException) @@ -129,7 +129,7 @@ private async Task BackgroundPeriodicFlushTask(int maxThresholdSecs, Cancellatio } // Do one final flush before exiting - await FlushEvents(); + await FlushEvents().ConfigureAwait(false); } internal async Task FlushEvents() @@ -168,7 +168,7 @@ await Task.WhenAll( _backgroundPeriodicFlushTask, FlushEvents(), Task.WhenAll(_tasks.Keys) - ); + ).ConfigureAwait(false); } private bool ShouldAddEventAfterDeduping(EventLog entry) diff --git a/dotnet-statsig/src/Statsig/Network/RequestDispatcher.cs b/dotnet-statsig/src/Statsig/Network/RequestDispatcher.cs index 4ef2d9d..f78111c 100644 --- a/dotnet-statsig/src/Statsig/Network/RequestDispatcher.cs +++ b/dotnet-statsig/src/Statsig/Network/RequestDispatcher.cs @@ -53,7 +53,7 @@ string sessionID int backoff = 1, int timeoutInMs = 0) { - var (result, status) = await FetchAsString(endpoint, body, retries, backoff, timeoutInMs); + var (result, status) = await FetchAsString(endpoint, body, retries, backoff, timeoutInMs).ConfigureAwait(false); return JsonConvert.DeserializeObject>(result ?? ""); } @@ -100,7 +100,7 @@ string sessionID client.Timeout = TimeSpan.FromMilliseconds(timeoutInMs); } - var response = await client.SendAsync(request); + var response = await client.SendAsync(request).ConfigureAwait(false); if (response == null) { return (null, InitializeResult.Success); @@ -108,20 +108,20 @@ string sessionID if ((int)response.StatusCode >= 200 && (int)response.StatusCode < 300) { - var result = await response.Content.ReadAsStringAsync(); + var result = await response.Content.ReadAsStringAsync().ConfigureAwait(false); return (result, InitializeResult.Success); } if (retries > 0 && RetryCodes.Contains((int)response.StatusCode)) { - return await Retry(endpoint, body, retries, backoff); + return await Retry(endpoint, body, retries, backoff).ConfigureAwait(false); } } catch (TaskCanceledException) { if (retries > 0) { - return await Retry(endpoint, body, retries, backoff); + return await Retry(endpoint, body, retries, backoff).ConfigureAwait(false); } else { @@ -133,7 +133,7 @@ string sessionID { if (retries > 0) { - return await Retry(endpoint, body, retries, backoff); + return await Retry(endpoint, body, retries, backoff).ConfigureAwait(false); } else { @@ -145,7 +145,7 @@ string sessionID { if (retries > 0) { - return await Retry(endpoint, body, retries, backoff); + return await Retry(endpoint, body, retries, backoff).ConfigureAwait(false); } } @@ -158,8 +158,8 @@ string sessionID int retries = 0, int backoff = 1) { - await Task.Delay(backoff * 1000); - return await FetchAsString(endpoint, body, retries - 1, backoff * BackoffMultiplier); + await Task.Delay(backoff * 1000).ConfigureAwait(false); + return await FetchAsString(endpoint, body, retries - 1, backoff * BackoffMultiplier).ConfigureAwait(false); } } } \ No newline at end of file diff --git a/dotnet-statsig/src/Statsig/Server/Evaluation/Evaluator.cs b/dotnet-statsig/src/Statsig/Server/Evaluation/Evaluator.cs index 1ebee0a..e01d35c 100644 --- a/dotnet-statsig/src/Statsig/Server/Evaluation/Evaluator.cs +++ b/dotnet-statsig/src/Statsig/Server/Evaluation/Evaluator.cs @@ -39,12 +39,12 @@ internal Evaluator(StatsigOptions options, RequestDispatcher dispatcher, string internal async Task Initialize() { - return await _store.Initialize(); + return await _store.Initialize().ConfigureAwait(false); } internal async Task Shutdown() { - await _store.Shutdown(); + await _store.Shutdown().ConfigureAwait(false); } internal void OverrideGate(string gateName, bool value, string? userID) diff --git a/dotnet-statsig/src/Statsig/Server/Evaluation/SpecStore.cs b/dotnet-statsig/src/Statsig/Server/Evaluation/SpecStore.cs index 1b390c8..b89fe64 100644 --- a/dotnet-statsig/src/Statsig/Server/Evaluation/SpecStore.cs +++ b/dotnet-statsig/src/Statsig/Server/Evaluation/SpecStore.cs @@ -71,20 +71,20 @@ internal async Task Initialize() { if (_dataStore != null) { - await _dataStore.Init(); + await _dataStore.Init().ConfigureAwait(false); } var status = InitializeResult.Success; - var bootedFromDataStore = await SyncValuesFromDataStore(); + var bootedFromDataStore = await SyncValuesFromDataStore().ConfigureAwait(false); if (!bootedFromDataStore) { - status = await SyncValuesFromNetwork(); + status = await SyncValuesFromNetwork().ConfigureAwait(false); } else { EvalReason = EvaluationReason.DataAdapter; } - await SyncIDLists(); + await SyncIDLists().ConfigureAwait(false); // Start background tasks to periodically refresh the store _syncValuesTask = BackgroundPeriodicSyncValuesTask(_cts.Token); @@ -97,7 +97,7 @@ internal async Task Shutdown() { if (_dataStore != null) { - await _dataStore.Shutdown(); + await _dataStore.Shutdown().ConfigureAwait(false); } // Signal that the periodic task should exit, and then wait for them to finish @@ -107,11 +107,11 @@ internal async Task Shutdown() } if (_syncIDListsTask != null) { - await _syncIDListsTask; + await _syncIDListsTask.ConfigureAwait(false); } if (_syncValuesTask != null) { - await _syncValuesTask; + await _syncValuesTask.ConfigureAwait(false); } } @@ -143,8 +143,8 @@ private async Task BackgroundPeriodicSyncIDListsTask(CancellationToken cancellat { try { - await Delay.Wait(delayInterval, cancellationToken); - await SyncIDLists(); + await Delay.Wait(delayInterval, cancellationToken).ConfigureAwait(false); + await SyncIDLists().ConfigureAwait(false); } catch (TaskCanceledException) { @@ -165,18 +165,18 @@ private async Task BackgroundPeriodicSyncValuesTask(CancellationToken cancellati { try { - await Delay.Wait(delayInterval, cancellationToken); + await Delay.Wait(delayInterval, cancellationToken).ConfigureAwait(false); if (_dataStore != null && _dataStore.SupportsPollingUpdates(DataStoreKey.Rulesets)) { - var didSync = await SyncValuesFromDataStore(); + var didSync = await SyncValuesFromDataStore().ConfigureAwait(false); if (didSync) { continue; } } - await SyncValuesFromNetwork(); + await SyncValuesFromNetwork().ConfigureAwait(false); } catch (TaskCanceledException) { @@ -198,7 +198,7 @@ private async Task DownloadIDList(IDList list) using (var request = new HttpRequestMessage(HttpMethod.Get, list.URL)) { request.Headers.Add("Range", string.Format("bytes={0}-", list.Size)); - var response = await client.SendAsync(request); + var response = await client.SendAsync(request).ConfigureAwait(false); if (response == null) { return; @@ -208,7 +208,7 @@ private async Task DownloadIDList(IDList list) { using (var memoryStream = new MemoryStream()) { - await response.Content.CopyToAsync(memoryStream); + await response.Content.CopyToAsync(memoryStream).ConfigureAwait(false); var contentLength = memoryStream.Length; memoryStream.Seek(0, SeekOrigin.Begin); @@ -292,7 +292,7 @@ private async Task AddServerIDList(string listName, IDList serverList) return; } - await DownloadIDList(localList); + await DownloadIDList(localList).ConfigureAwait(false); } private async Task SyncIDLists(IReadOnlyDictionary idListMap) @@ -317,7 +317,7 @@ private async Task SyncIDLists(IReadOnlyDictionary idListMap) // Ignore malformed lists } } - await Task.WhenAll(tasks); + await Task.WhenAll(tasks).ConfigureAwait(false); var deletedLists = new List(); foreach (var listName in _idLists.Keys) @@ -342,13 +342,13 @@ private async Task SyncIDLists() var response = await _requestDispatcher.Fetch("get_id_lists", new Dictionary { ["statsigMetadata"] = SDKDetails.GetServerSDKDetails().StatsigMetadata - }); + }).ConfigureAwait(false); if (response == null || response.Count == 0) { return; } - await SyncIDLists(response); + await SyncIDLists(response).ConfigureAwait(false); } private async Task SyncValuesFromNetwork() @@ -360,7 +360,7 @@ private async Task SyncValuesFromNetwork() ["sinceTime"] = LastSyncTime, ["statsigMetadata"] = SDKDetails.GetServerSDKDetails().StatsigMetadata } - ); + ).ConfigureAwait(false); var hasUpdates = ParseResponse(response); if (hasUpdates) @@ -369,7 +369,7 @@ private async Task SyncValuesFromNetwork() } if (hasUpdates && _dataStore != null && response != null) { - await _dataStore.Set(DataStoreKey.Rulesets, response); + await _dataStore.Set(DataStoreKey.Rulesets, response).ConfigureAwait(false); } return status; } @@ -381,7 +381,7 @@ private async Task SyncValuesFromDataStore() return false; } - var response = await _dataStore.Get(DataStoreKey.Rulesets); + var response = await _dataStore.Get(DataStoreKey.Rulesets).ConfigureAwait(false); return ParseResponse(response); } diff --git a/dotnet-statsig/src/Statsig/Server/ServerDriver.cs b/dotnet-statsig/src/Statsig/Server/ServerDriver.cs index d2c3ad0..446f7b6 100644 --- a/dotnet-statsig/src/Statsig/Server/ServerDriver.cs +++ b/dotnet-statsig/src/Statsig/Server/ServerDriver.cs @@ -59,10 +59,10 @@ public async Task Initialize() { return await _errorBoundary.Capture("Initialize", async () => { - var result = await evaluator.Initialize(); + var result = await evaluator.Initialize().ConfigureAwait(false); _initialized = true; return result; - }, () => { return InitializeResult.Failure; }); + }, () => { return InitializeResult.Failure; }).ConfigureAwait(false); } public async Task Shutdown() @@ -71,14 +71,14 @@ await _errorBoundary.Swallow("Shutdown", async () => { await Task.WhenAll( evaluator.Shutdown(), - _eventLogger.Shutdown()); + _eventLogger.Shutdown()).ConfigureAwait(false); #if SUPPORTS_ASYNC_DISPOSAL - await ((IAsyncDisposable)this).DisposeAsync(); + await ((IAsyncDisposable)this).DisposeAsync().ConfigureAwait(false); #else ((IDisposable)this).Dispose(); #endif - }); + }).ConfigureAwait(false); } #region Local Overrides @@ -370,7 +370,7 @@ async ValueTask IAsyncDisposable.DisposeAsync() return; } - await _eventLogger.FlushEvents(); + await _eventLogger.FlushEvents().ConfigureAwait(false); _disposed = true; } #endif diff --git a/dotnet-statsig/src/Statsig/Server/StatsigServer.cs b/dotnet-statsig/src/Statsig/Server/StatsigServer.cs index cbcc6ea..479ae92 100644 --- a/dotnet-statsig/src/Statsig/Server/StatsigServer.cs +++ b/dotnet-statsig/src/Statsig/Server/StatsigServer.cs @@ -39,12 +39,12 @@ public static async Task Initialize(string serverSecret, Stats } _singleDriver = new ServerDriver(serverSecret, options); - return await _singleDriver.Initialize(); + return await _singleDriver.Initialize().ConfigureAwait(false); } public static async Task Shutdown() { - await EnforceInitialized().Shutdown(); + await EnforceInitialized().Shutdown().ConfigureAwait(false); _singleDriver = null; } @@ -210,20 +210,20 @@ public static List GetExperimentList() [Obsolete("Please use CheckGateSync instead. " + ServerDriver.AsyncFuncDeprecationLink)] public static async Task CheckGate(StatsigUser user, string gateName) { - return await EnforceInitialized().CheckGate(user, gateName); + return await EnforceInitialized().CheckGate(user, gateName).ConfigureAwait(false); } [Obsolete( "Please use CheckGateWithExposureLoggingDisabledSync instead. " + ServerDriver.AsyncFuncDeprecationLink)] public static async Task CheckGateWithExposureLoggingDisabled(StatsigUser user, string gateName) { - return await EnforceInitialized().CheckGateWithExposureLoggingDisabled(user, gateName); + return await EnforceInitialized().CheckGateWithExposureLoggingDisabled(user, gateName).ConfigureAwait(false); } [Obsolete("Please use GetConfigSync instead. " + ServerDriver.AsyncFuncDeprecationLink)] public static async Task GetConfig(StatsigUser user, string configName) { - return await EnforceInitialized().GetConfig(user, configName); + return await EnforceInitialized().GetConfig(user, configName).ConfigureAwait(false); } [Obsolete( @@ -233,13 +233,13 @@ public static async Task GetConfigWithExposureLoggingDisabled( string configName ) { - return await EnforceInitialized().GetConfigWithExposureLoggingDisabled(user, configName); + return await EnforceInitialized().GetConfigWithExposureLoggingDisabled(user, configName).ConfigureAwait(false); } [Obsolete("Please use GetExperimentSync instead. " + ServerDriver.AsyncFuncDeprecationLink)] public static async Task GetExperiment(StatsigUser user, string experimentName) { - return await EnforceInitialized().GetExperiment(user, experimentName); + return await EnforceInitialized().GetExperiment(user, experimentName).ConfigureAwait(false); } [Obsolete("Please use GetExperimentWithExposureLoggingDisabledSync instead. " + @@ -249,20 +249,20 @@ public static async Task GetExperimentWithExposureLoggingDisabled string experimentName ) { - return await EnforceInitialized().GetExperimentWithExposureLoggingDisabled(user, experimentName); + return await EnforceInitialized().GetExperimentWithExposureLoggingDisabled(user, experimentName).ConfigureAwait(false); } [Obsolete("Please use GetLayerSync instead. " + ServerDriver.AsyncFuncDeprecationLink)] public static async Task GetLayer(StatsigUser user, string layerName) { - return await EnforceInitialized().GetLayer(user, layerName); + return await EnforceInitialized().GetLayer(user, layerName).ConfigureAwait(false); } [Obsolete( "Please use GetLayerWithExposureLoggingDisabledSync instead. " + ServerDriver.AsyncFuncDeprecationLink)] public static async Task GetLayerWithExposureLoggingDisabled(StatsigUser user, string layerName) { - return await EnforceInitialized().GetLayerWithExposureLoggingDisabled(user, layerName); + return await EnforceInitialized().GetLayerWithExposureLoggingDisabled(user, layerName).ConfigureAwait(false); } #endregion