azure-cosmos-dotnet-repository icon indicating copy to clipboard operation
azure-cosmos-dotnet-repository copied to clipboard

Add the ability to provide custom ConnectionPolicy

Open AnyaMostowyj opened this issue 2 years ago • 6 comments

As detailed in the Cosmos documentation, the native client exposes the ability to override the default ConnectionPolicy: https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.documents.client.connectionpolicy?view=azure-dotnet

Many of these settings are quite niche, but there are some standard features (like RequestTimeout and RetryOptions) that it would be useful to be able to override.

For context, my current scenario is a fire-and-forget call to Cosmos (as a non-critical dependency) where I would like to reduce the default request timeout from 10 seconds to 3, but I can also foresee a scenario where we may want to override the rate-limiting retries too.

Hopefully this is enough detail, but let me know if you need more info. Happy to submit a PR for the feature if you agree it's worth adding :)

AnyaMostowyj avatar Feb 02 '22 22:02 AnyaMostowyj

Hi @AnyaMostowyj - this sounds great!

We currently have the ICosmosClientOptionsProvider https://github.com/IEvangelist/azure-cosmos-dotnet-repository/blob/2432320ceb0a46227f6f91bed42e54556759bb63/src/Microsoft.Azure.CosmosRepository/Providers/DefaultCosmosClientOptionsProvider.cs#L16

This class provides the options for the CosmosClient that we create and then cache for the lifetime of the app. We usually specify these properties on the RepositoryOptions class.

https://github.com/IEvangelist/azure-cosmos-dotnet-repository/blob/2432320ceb0a46227f6f91bed42e54556759bb63/src/Microsoft.Azure.CosmosRepository/Options/RepositoryOptions.cs#L17

We could add the relevant properties for the CosmosClient on here and then use them in the ICosmosClientOptionsProvider.

I would be happy to review a PR for this :)

mumby0168 avatar Feb 02 '22 22:02 mumby0168

Good stuff, I'll get on the case!

AnyaMostowyj avatar Feb 03 '22 13:02 AnyaMostowyj

hi @AnyaMostowyj I have just noticed this might already be possible - check this out

https://github.com/IEvangelist/azure-cosmos-dotnet-repository/blob/2432320ceb0a46227f6f91bed42e54556759bb63/src/Microsoft.Azure.CosmosRepository/Extensions/ServiceCollectionExtensions.cs#L42

mumby0168 avatar Feb 05 '22 18:02 mumby0168

hi @AnyaMostowyj I have just noticed this might already be possible - check this out

https://github.com/IEvangelist/azure-cosmos-dotnet-repository/blob/2432320ceb0a46227f6f91bed42e54556759bb63/src/Microsoft.Azure.CosmosRepository/Extensions/ServiceCollectionExtensions.cs#L42

Yes, I agree with @mumby0168 - this is possible through that API.

IEvangelist avatar Feb 07 '22 15:02 IEvangelist

Apologies for the delay, @mumby0168 @IEvangelist , and great spot, this does indeed appear to allow me to pass a custom CosmosClientOptions object. It appears I would need to reference the type directly from the underlying Microsoft.Azure.Cosmos namespace though, instead of being nicely wrapped by a Microsoft.Azure.CosmosRepository type (like RepositoryOptions). Supplying these options also appears to mask the fact that they will override any options supplied using the RepositoryOptions type, or am I misunderstanding the implementation? This may be by design, but I'm not sure it's transparent to the consumer?

AnyaMostowyj avatar Feb 08 '22 15:02 AnyaMostowyj

Apologies for the delay, @mumby0168 @IEvangelist , and great spot, this does indeed appear to allow me to pass a custom CosmosClientOptions object. It appears I would need to reference the type directly from the underlying Microsoft.Azure.Cosmos namespace though, instead of being nicely wrapped by a Microsoft.Azure.CosmosRepository type (like RepositoryOptions). Supplying these options also appears to mask the fact that they will override any options supplied using the RepositoryOptions type, or am I misunderstanding the implementation? This may be by design, but I'm not sure it's transparent to the consumer?

I think in this case given we aren't trying to hide the library we are wrapping this might be okay as is. What do you think @IEvangelist ?

mumby0168 avatar Mar 05 '22 21:03 mumby0168

Can I take it? I will add properties from ConnectionPolicy to RepositoryOptions

idormenco avatar Oct 18 '22 08:10 idormenco

Apologies for the delay, @mumby0168 @IEvangelist , and great spot, this does indeed appear to allow me to pass a custom CosmosClientOptions object. It appears I would need to reference the type directly from the underlying Microsoft.Azure.Cosmos namespace though, instead of being nicely wrapped by a Microsoft.Azure.CosmosRepository type (like RepositoryOptions). Supplying these options also appears to mask the fact that they will override any options supplied using the RepositoryOptions type, or am I misunderstanding the implementation? This may be by design, but I'm not sure it's transparent to the consumer?

I think in this case given we aren't trying to hide the library we are wrapping this might be okay as is. What do you think @IEvangelist ?

I agree!

IEvangelist avatar Oct 18 '22 18:10 IEvangelist

Can I take it? I will add properties from ConnectionPolicy to RepositoryOptions

I'm ok with that, as long as @AnyaMostowyj and @mumby0168 are?

IEvangelist avatar Oct 18 '22 18:10 IEvangelist

Closing this one as it is resolved by the user, and the existing API functions.

IEvangelist avatar Jan 19 '23 01:01 IEvangelist