Skip to content

Commit

Permalink
chore: ConfigureAwait(false) for all await calls (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-statsig authored May 1, 2024
1 parent 9384bc3 commit 6986607
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 66 deletions.
4 changes: 2 additions & 2 deletions StatsigRedis/RedisDataStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ public bool SupportsPollingUpdates(string key)

public async Task<string?> 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
Expand Down
10 changes: 5 additions & 5 deletions dotnet-statsig/src/Statsig/Client/ClientDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public async Task Initialize(StatsigUser? user)
["statsigMetadata"] = GetStatsigMetadata(),
},
timeoutInMs: _options.ClientRequestTimeoutMs
);
).ConfigureAwait(false);
if (response == null)
{
return;
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -257,7 +257,7 @@ async ValueTask IAsyncDisposable.DisposeAsync()
throw new ObjectDisposedException("ClientDriver");
}

await _eventLogger.FlushEvents();
await _eventLogger.FlushEvents().ConfigureAwait(false);
_disposed = true;
}
#endif
Expand Down
6 changes: 3 additions & 3 deletions dotnet-statsig/src/Statsig/Client/StatsigClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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()
Expand Down
9 changes: 5 additions & 4 deletions dotnet-statsig/src/Statsig/Lib/ErrorBoundary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ public async Task Swallow(string tag, Func<Task> task)
{
await Capture(tag, async () =>
{
await task();
await task().ConfigureAwait(false);
return true;
}, () => false);
}, () => false)
.ConfigureAwait(false);
}

public void Swallow(string tag, Action task)
Expand All @@ -44,7 +45,7 @@ public async Task<T> Capture<T>(string tag, Func<Task<T>> task, Func<T> recover)
{
try
{
var result = await task();
var result = await task().ConfigureAwait(false);
return result;
}
catch (Exception ex)
Expand Down Expand Up @@ -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
{
Expand Down
8 changes: 4 additions & 4 deletions dotnet-statsig/src/Statsig/Network/EventLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ 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,
// so check once more to make sure that enough time has elapsed since the last
// event flush, and if so, then trigger a flush.
if (_lastFlushTime.AddSeconds(maxThresholdSecs) <= DateTime.UtcNow)
{
await FlushEvents();
await FlushEvents().ConfigureAwait(false);
}
}
catch (TaskCanceledException)
Expand All @@ -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()
Expand Down Expand Up @@ -168,7 +168,7 @@ await Task.WhenAll(
_backgroundPeriodicFlushTask,
FlushEvents(),
Task.WhenAll(_tasks.Keys)
);
).ConfigureAwait(false);
}

private bool ShouldAddEventAfterDeduping(EventLog entry)
Expand Down
18 changes: 9 additions & 9 deletions dotnet-statsig/src/Statsig/Network/RequestDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IReadOnlyDictionary<string, JToken>>(result ?? "");
}

Expand Down Expand Up @@ -100,28 +100,28 @@ 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);
}

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
{
Expand All @@ -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
{
Expand All @@ -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);
}
}

Expand All @@ -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);
}
}
}
4 changes: 2 additions & 2 deletions dotnet-statsig/src/Statsig/Server/Evaluation/Evaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ internal Evaluator(StatsigOptions options, RequestDispatcher dispatcher, string

internal async Task<InitializeResult> 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)
Expand Down
42 changes: 21 additions & 21 deletions dotnet-statsig/src/Statsig/Server/Evaluation/SpecStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,20 @@ internal async Task<InitializeResult> 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);
Expand All @@ -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
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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;
Expand All @@ -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);

Expand Down Expand Up @@ -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<string, JToken> idListMap)
Expand All @@ -317,7 +317,7 @@ private async Task SyncIDLists(IReadOnlyDictionary<string, JToken> idListMap)
// Ignore malformed lists
}
}
await Task.WhenAll(tasks);
await Task.WhenAll(tasks).ConfigureAwait(false);

var deletedLists = new List<string>();
foreach (var listName in _idLists.Keys)
Expand All @@ -342,13 +342,13 @@ private async Task SyncIDLists()
var response = await _requestDispatcher.Fetch("get_id_lists", new Dictionary<string, object>
{
["statsigMetadata"] = SDKDetails.GetServerSDKDetails().StatsigMetadata
});
}).ConfigureAwait(false);
if (response == null || response.Count == 0)
{
return;
}

await SyncIDLists(response);
await SyncIDLists(response).ConfigureAwait(false);
}

private async Task<InitializeResult> SyncValuesFromNetwork()
Expand All @@ -360,7 +360,7 @@ private async Task<InitializeResult> SyncValuesFromNetwork()
["sinceTime"] = LastSyncTime,
["statsigMetadata"] = SDKDetails.GetServerSDKDetails().StatsigMetadata
}
);
).ConfigureAwait(false);

var hasUpdates = ParseResponse(response);
if (hasUpdates)
Expand All @@ -369,7 +369,7 @@ private async Task<InitializeResult> SyncValuesFromNetwork()
}
if (hasUpdates && _dataStore != null && response != null)
{
await _dataStore.Set(DataStoreKey.Rulesets, response);
await _dataStore.Set(DataStoreKey.Rulesets, response).ConfigureAwait(false);
}
return status;
}
Expand All @@ -381,7 +381,7 @@ private async Task<bool> SyncValuesFromDataStore()
return false;
}

var response = await _dataStore.Get(DataStoreKey.Rulesets);
var response = await _dataStore.Get(DataStoreKey.Rulesets).ConfigureAwait(false);
return ParseResponse(response);
}

Expand Down
Loading

0 comments on commit 6986607

Please sign in to comment.