statsig-io / dotnet-sdk

Statsig SDK for .NET applications on both client and server sides
ISC License
2 stars 6 forks source link

Cancellation token support for async methods #18

Open alex-bortnik opened 2 years ago

alex-bortnik commented 2 years ago

Hi,

we would like to add cancellation tokens support for async methods. I prepared PR (see patch in attachment). It also includes update from obsolete WebRequest to HttpClient

(please rename from txt -> patch)

cancellation_token_support_for_async_methods.txt .

jkw-statsig commented 2 years ago

Hi @alex-bortnik - we just closed an issue yesterday about this, and we provided an option for users to configure timeouts on these requests, which sounds like would be helpful to you. Can you take a look at https://github.com/statsig-io/dotnet-sdk/issues/12 and see if that works first?

I see that you've added cancellation tokens to functions like CheckGate - these functions are async but should actually return instantaneously because it just performs a local evaluation, so I'm not sure if cancellation token is really needed here.

alex-bortnik commented 2 years ago

Hi @jkw-statsig - yes, I saw that, my PR is based on #12 implementation. This PR allows to provide either timeout or cancellation token.

Override with optional cancellation token is added only to the public methods which are calling RequestDispatcher.Fetch (where actual http request is being sent).

Implementation of RequestDispatcher.Fetch is switched from WebRequest to HttpClient which has better async support. Also WebRequest is already declared obsolete: https://docs.microsoft.com/en-us/dotnet/core/compatibility/networking/6.0/webrequest-deprecated

alex-bortnik commented 2 years ago

Also, the common practice in .NET/C# is to pass cancellation token downstream from upper layer. It is not possible to get remaining timeout value from cancellation token downstream. Plus there could be other reasons for cancellation apart from timeout.

jkw-statsig commented 2 years ago

Got it, thanks for the context. We'll take a look into your patch

mitchell-arthur-wealth commented 1 year ago

@jkw-statsig any update on this?