csharp-sdk icon indicating copy to clipboard operation
csharp-sdk copied to clipboard

SDK should provide async methods for async operations

Open martincostello opened this issue 6 years ago • 5 comments

As noted in #51, the SDK does not follow accepted best practices in its use of async/await.

Specifically, the use of Task.Run() to make async calls "fire-and-forget" here:

https://github.com/optimizely/csharp-sdk/blob/17a0271a1f46a9639fbf6275da58891db13f8b9c/OptimizelySDK/Event/Dispatcher/HttpClientEventDispatcher45.cs#L78

At the expense of "returning to the caller immediately", this spawns new tasks to perform the HTTP calls.

A better approach would be to add asynchronous methods to the API, say by introducing a new async version of IOptimizely that the Optimizely class could also implement for integrators to opt into using. This would allow integrators to decide whether to make async tasks fire-and-forget or not, as well as allow CancellationTokens to be flowed to the underlying HttpClient calls where appropriate.

From looking at the code, the calls this flowing of async would apply to are those below:

https://github.com/optimizely/csharp-sdk/blob/17a0271a1f46a9639fbf6275da58891db13f8b9c/OptimizelySDK/Optimizely.cs#L200

https://github.com/optimizely/csharp-sdk/blob/17a0271a1f46a9639fbf6275da58891db13f8b9c/OptimizelySDK/Optimizely.cs#L263

https://github.com/optimizely/csharp-sdk/blob/17a0271a1f46a9639fbf6275da58891db13f8b9c/OptimizelySDK/Optimizely.cs#L376

https://github.com/optimizely/csharp-sdk/blob/17a0271a1f46a9639fbf6275da58891db13f8b9c/OptimizelySDK/Optimizely.cs#L564

Such changes would allow the SDK to be "async all the way down".

martincostello avatar Mar 14 '19 18:03 martincostello

We'll investigate this suggestion and get back to you!

alexsaves avatar Mar 14 '19 19:03 alexsaves

@alexsaves Any update on this? This continues to be a problem for anyone trying to use the Optimizely sdk from an AWS lambda (for example)? I wrote a blog post about the issues I experienced here and I know a new client of mine is looking at adopting both AWS Lambda and Optimizely using C#.

TimButterfield avatar Jul 23 '20 15:07 TimButterfield

Hi All,

I'm working through our unresolved C# Issues here and wondering if this is still something pertinent?

Thanks for any updates you all have.

mikechu-optimizely avatar Nov 17 '22 20:11 mikechu-optimizely

Yes it is.

martincostello avatar Nov 17 '22 20:11 martincostello

Thanks @martincostello.

I've added a spike (FSSDK-8702 for ref) to our pipeline. Sorry that this got dropped so long ago.

My initial thought is that this might take some careful planning and be incorporated after our effort to modernize the SDK early next year.

mikechu-optimizely avatar Nov 17 '22 21:11 mikechu-optimizely

FSSDK-8702 is still in our active backlog. We have a large feature release in the works, then we can work to modernize the SDK. Thanks for the patience.

mikechu-optimizely avatar Jan 13 '23 19:01 mikechu-optimizely

We still have the Jira item in our backlog, but it's included in our Q3 Maintenance epic. Thanks for hanging in there.

I am 🤔 hard about the right approach. I'd love to modernize the entire stack, but @martincostello 's route of adding *Async() methods may be the faster solution and provide a stepping stone to the later framework upgrade.

mikechu-optimizely avatar Jun 27 '23 12:06 mikechu-optimizely

We have planned for QY4 to modernize this SDK.

We want to republish to support multi-version .NET Core and Framework. At the same time we're looking to convert to Task based async/await throughout the stack.

We've moved this Jira item to that epic to ensure we accomplish this goal.

mikechu-optimizely avatar Jul 20 '23 17:07 mikechu-optimizely