Skip to content

Switched to AsyncKeyedLock #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

MarkCiliaVincenti
Copy link

This implementation will give you lower memory allocations.

@artmasa
Copy link
Contributor

artmasa commented May 13, 2024

Interesting. Let me take a look at this! Thanks!

@@ -20,7 +21,8 @@ public static IServiceCollection AddFunctionsAuthorizationCore(this IServiceColl

return services
.AddSingleton<IFunctionsAuthorizationResultHandler, FunctionsAuthorizationResultHandler>()
.AddSingleton(typeof(IFunctionsAuthorizationFilterCache<>), typeof(FunctionsAuthorizationFilterCache<>));
.AddSingleton(typeof(IFunctionsAuthorizationFilterCache<>), typeof(FunctionsAuthorizationFilterCache<>))
.AddSingleton(new AsyncKeyedLocker<string>(o => { o.PoolSize = 20; o.PoolInitialFill = 1; }));
Copy link
Contributor

@artmasa artmasa May 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to named lockers? If someone using this package also references your package for their own purposes and add the locker to the services, it would be replacing this configuration.
KeyedMonitor is internal and there won't be clashes.
Can you look into instrumenting the locker where you have a single constructor and initializing it using the options pattern.

public class AsyncKeyedLocker<TKey>
{
  // This should be the only public constructor 
  public AsyncKeyedLocker(AsyncKeyedLockOptions options)
  {
      // init code
  }

  ...
}

and intrument it like

// this would require a factory type to get the named singleton
services.AddKeyedLocker<TKey>("<locker-name>", options => { ... });

// or

services.AddKeyedLocker<TKey, TService>(options => { ... });

the second one allows for automatic injection when used as constructor parameters for TService and services provides a unique instance for this service, so it becomes a singleton for the service instead of a singleton for the application.

The way it is right now does not ensure that external code will not change the service as this package requires it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I understand you correctly. You're creating an instance of AsyncKeyedLocker and someone in a different assembly might also do the same, but every instance of AsyncKeyedLocker will have its own dictionary.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imagine we were just talking about a normal Dictionary here which you'd inject as a singleton. Unless you intentionally expose it no one will be able to modify your dictionary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Internally within this package we call AddSingleton(new AsyncKeyedLocker<string>(options => ...)). This gets invoked on AddFunctionsAuthentication(... )

The problem is when a consumer of this package has

services
   .AddFunctionsAuthentication(); //Internally this adds the singleton descriptor for the locker

// a few lines below
services
   .AddSingleton<AsyncKeyedLocker<string>>(); // This one just replaced the one configured for functions authentication

It does not ensure the locker behavior for what this package needs will be kept intact if a user uses DarkLoop package and your package for any other purpose.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it shouldn't. It should be easy to test. I mean technically you could wrap the locker in an internal KeyedLock class but that's not going to change anything except for slightly reducing performance.

Copy link
Author

@MarkCiliaVincenti MarkCiliaVincenti May 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As in the second AddSingleton will create a new instance not reuse an instance created by a different library. You can debug AsyncKeyedLocker so with that code you showed me you can see that it should go into the AsyncKeyedLock constructor twice, once for your library and once for wherever the library is used. That is you don't even need to set up locks or anything in order to test this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's how ServiceCollection works. The last registered descriptor for a specific Type will be the one resolving that type. It does not depend on your code. This is why I'm asking if your component can add the functionality to ensure that.
At the moment, KeyedMonitor ensures the desired behavior for the component.
Hopefully you are able to add this functionality at some point to onboard this change.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create a wrapper class for it if you're worried? What you're asking for is extremely unoptimal and you can already do it yourself by creating a Dictionary<string, AsyncKeyedLocker> but I'd advise to avoid that. Sorry for my tone, very very long day at work.

@MarkCiliaVincenti
Copy link
Author

Shall we revisit this @artmasa ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants