Exemplo n.º 1
0
        public static void AddTokenRepositoryV2(this IServiceCollection services, string server, int maxRateLimit, TimeSpan restDuration, TimeSpan watchDuration, int maxForDuration = int.MinValue, bool blocking = false)
        {
            var apiLimitsConfig = new ApiLimitsConfig();

            apiLimitsConfig.Setup(server, maxRateLimit, restDuration, watchDuration, maxForDuration, blocking);
            var sp         = services.BuildServiceProvider();
            var logFactory = sp.GetRequiredService <ILoggerFactory>();

            if (maxForDuration > 0 && maxRateLimit > 0) //injects only 2 ITokenRepository
            {
                services.AddSingleton <IGrantToken>(sevs =>
                {
                    return(new MultiTokenApiGateway(apiLimitsConfig, logFactory));
                });
            }
            else if (maxForDuration > 0 || maxRateLimit > 0) //injects only 1 ITokenRepository
            {
                services.AddSingleton <IGrantToken>(sevs =>
                {
                    return(new SingleTokenApiGateway(apiLimitsConfig, logFactory));
                });
            }
            else
            {
                throw new InvalidOperationException("ITokenRepository cannot be determined, make sure configuration is valid to inject at least 1 ITokenRepository.");
            }

            ServiceActivator.Configure(sp);//Only for using Fody Attributes
        }
Exemplo n.º 2
0
 public MultiTokenApiGateway(ApiLimitsConfig apiLimitsConfig, ILoggerFactory loggerFactory)
 {
     _apiLimitsConfig = apiLimitsConfig;
     _iLimitRate      = apiLimitsConfig.IsBlocking
 ? new BlockingTokenRepo(apiLimitsConfig, loggerFactory.CreateLogger <BlockingTokenRepo>()) as ITokenRepository
 : new NonBlockingTokenRepo(apiLimitsConfig, loggerFactory.CreateLogger <NonBlockingTokenRepo>()) as ITokenRepository;
     _iLimitWindow = new FixWindowTokenRepo(apiLimitsConfig, loggerFactory.CreateLogger <FixWindowTokenRepo>()) as ITokenRepository;
 }
Exemplo n.º 3
0
 public SingleTokenApiGateway(ApiLimitsConfig apiLimitsConfig, ILoggerFactory loggerFactory)
 {
     _apiLimitsConfig = apiLimitsConfig;
     if (apiLimitsConfig.TotalLimit > 0)
     {
         _tokenRepo = new FixWindowTokenRepo(apiLimitsConfig, loggerFactory.CreateLogger <FixWindowTokenRepo>());
     }
     else
     {
         _tokenRepo = apiLimitsConfig.IsBlocking
   ? new BlockingTokenRepo(apiLimitsConfig, loggerFactory.CreateLogger <BlockingTokenRepo>()) as ITokenRepository
   : new NonBlockingTokenRepo(apiLimitsConfig, loggerFactory.CreateLogger <NonBlockingTokenRepo>()) as ITokenRepository;
     }
 }
Exemplo n.º 4
0
        public static void AddTokenRepository(this IServiceCollection services, string server, int maxRateLimit, TimeSpan restDuration, TimeSpan watchDuration, int maxForDuration = int.MinValue, bool blocking = false)
        {
            //An entity should not be a service that is injected.. it should simply be instantiated and used....
            //services.AddSingleton<IConfigureApiLimits>((services) =>
            //{
            //  var retVal = new ApiLimitsConfig() as IConfigureApiLimits;
            //  retVal.Setup(server, maxRateLimit, restDuration, watchDuration, maxForDuration, blocking);
            //  return retVal;
            //});
            var apiLimitsConfig = new ApiLimitsConfig();

            apiLimitsConfig.Setup(server, maxRateLimit, restDuration, watchDuration, maxForDuration, blocking);

            if (maxRateLimit > 0)
            {
                services.AddSingleton <ITokenRepository>((services) =>
                {
                    var logFactory = services.GetRequiredService <ILoggerFactory>();
                    var retVal     = blocking
              ? new BlockingTokenRepo(apiLimitsConfig, logFactory.CreateLogger <BlockingTokenRepo>()) as ITokenRepository
              : new NonBlockingTokenRepo(apiLimitsConfig, logFactory.CreateLogger <NonBlockingTokenRepo>()) as ITokenRepository;
                    return(retVal);
                });
            }
            if (maxForDuration > 0)
            {
                services.AddSingleton <ITokenRepository>((services) =>
                {
                    var logFactory = services.GetRequiredService <ILoggerFactory>();
                    var retVal     = new FixWindowTokenRepo(apiLimitsConfig, logFactory.CreateLogger <FixWindowTokenRepo>()) as ITokenRepository;
                    return(retVal);
                });
            }
            if (maxForDuration > 0 && maxRateLimit > 0) //injects only 2 ITokenRepository
            {
                services.AddTransient <IGrantToken, MultiTokenApiGateway>();
            }
            else if (maxForDuration > 0 || maxRateLimit > 0) //injects only 1 ITokenRepository
            {
                services.AddTransient <IGrantToken, SingleTokenApiGateway>();
            }
            else
            {
                throw new InvalidOperationException("ITokenRepository cannot be determined, make sure configuration is valid to inject at least 1 ITokenRepository.");
            }
        }