Exemplo n.º 1
0
        public static IRegistrar <IRequestProcessorMiddleware> AddFilter(this IRegistrar <IRequestProcessorMiddleware> builder, Func <Request, bool> predicate)
        {
            if (predicate == null)
            {
                throw new ArgumentNullException(nameof(predicate));
            }

            return(builder.Add(ctx => new FilteringMiddleware(ctx.GetLogger(), predicate)));
        }
Exemplo n.º 2
0
 private static void Transports(IRegistrar <ITransport> services)
 {
     services
     .Add(new DemoTransport(new[] { new CompositeCommand() }))
     //throw
     .Add(new DemoTransport(new[]
     {
         new ThrowCommand(), new ThrowCommand {
             Type = nameof(TimeoutException)
         }
     }))
     // delays
     .Add(new DemoTransport(new[]
     {
         new DelayCommand(TimeSpan.FromSeconds(2)), new DelayCommand(TimeSpan.FromMinutes(2)),
         new DelayCommand(TimeSpan.FromSeconds(20))
         {
             WatchDogTimeout = TimeSpan.FromSeconds(30)
         }
     }))
     // combined
     .Add(new DemoTransport(new[]
     {
         new CompositeCommand {
             Delay = new DelayCommand(TimeSpan.FromSeconds(2))
         },
         new CompositeCommand {
             Delay = new DelayCommand(TimeSpan.FromMinutes(2))
         },
         new CompositeCommand
         {
             Delay = new DelayCommand(TimeSpan.FromSeconds(2)), Throw = new ThrowCommand()
         },
         new CompositeCommand
         {
             Delay = new DelayCommand(TimeSpan.FromMinutes(2)), Throw = new ThrowCommand()
         }
     }))
     // scheduled
     .Add(new ScheduledTransport(new InMemoryScheduledJobStore(new[]
     {
         ScheduledJobDefinition.Create("* * * * *", new HelloCommand {
             Message = "Hi (every min)"
         }),
         ScheduledJobDefinition.Create("*/2 * * * *", new ProduceInProcess()),
         new ScheduledJobDefinition
         {
             Cron        = "*/5 * * * *",
             Name        = typeof(HelloCommand).FullName,
             Content     = "{\"Message\":\"How are you (every 5 min)\"}",
             ContentType = "json"
         }
     })))
     // in process
     .Add(InProcess.Instance);
 }
Exemplo n.º 3
0
        /// <summary>
        /// Registers watch-dog middleware using <paramref name="builder"/>.
        /// </summary>
        /// <param name="builder">The builder to use for registration.</param>
        /// <param name="options">The watch-dog configuration.</param>
        /// <returns><paramref name="builder"/></returns>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="builder"/> or <paramref name="options"/> is null.
        /// </exception>
        public static IRegistrar <IRequestProcessorMiddleware> AddWatchDog(this IRegistrar <IRequestProcessorMiddleware> builder, Action <WatchDogOptions> configureOptions)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            var options = OptionsBuilder.ConfigureAndValidate(configureOptions);

            return(builder.Add(ctx => new WatchDogMiddleware(options, ctx.GetLogger())));
        }
        public static IRegistrar <IRequestProcessorMiddleware> AddRetry(this IRegistrar <IRequestProcessorMiddleware> builder, IRetryPolicy retryPolicy)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }
            if (retryPolicy == null)
            {
                throw new ArgumentNullException(nameof(retryPolicy));
            }

            return(builder.Add(ctx => new RetryingMiddleware(retryPolicy, ctx.GetLogger())));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Registers middleware.
        /// </summary>
        /// <param name="builder">The builder to use for middleware registration.</param>
        /// <param name="creator">The function that creates <see cref="IRequestProcessorMiddleware"/>.</param>
        /// <returns></returns>
        public static IRegistrar <IRequestProcessorMiddleware> Add(this IRegistrar <IRequestProcessorMiddleware> builder,
                                                                   Func <IServiceProvider, Func <Context, RequestProcessorCallback, Task> > creator)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }
            if (creator == null)
            {
                throw new ArgumentNullException(nameof(creator));
            }

            return(builder.Add(provider => new AdHocRequestProcessorMiddleware(creator(provider))));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Registers middleware.
        /// </summary>
        /// <param name="builder">The builder to use for middleware registration.</param>
        /// <param name="middleware">The <see cref="IRequestProcessor"/> middleware.</param>
        /// <returns></returns>
        public static IRegistrar <IRequestProcessorMiddleware> Add(this IRegistrar <IRequestProcessorMiddleware> builder,
                                                                   Func <Context, RequestProcessorCallback, Task> middleware)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }
            if (middleware == null)
            {
                throw new ArgumentNullException(nameof(middleware));
            }

            return(builder.Add(new AdHocRequestProcessorMiddleware(middleware)));
        }
        /// <summary>
        /// Register Debounce middleware with the specified <paramref name="lastRunStore"/>.
        /// </summary>
        /// <param name="builder">The builder to use for registration.</param>
        /// <param name="lastRunStore">The last run store to use.</param>
        /// <returns><paramref name="builder"/></returns>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="builder"/> or <paramref name="lastRunStore"/> is null.
        /// </exception>
        public static IRegistrar <IRequestProcessorMiddleware> AddDebounce(this IRegistrar <IRequestProcessorMiddleware> builder,
                                                                           ILastRunStore lastRunStore)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }
            if (lastRunStore == null)
            {
                throw new ArgumentNullException(nameof(lastRunStore));
            }

            return(builder.Add(ctx => new DebounceMiddleware(lastRunStore, ctx.GetLogger())));
        }
Exemplo n.º 8
0
 private static void MetadataProviders(IRegistrar <IRequestMetadataProvider> services)
 {
     services
     .Add(new BuiltInRequestMetadataProvider(typeof(Program).Assembly))
     .Add(new RequestMetadataProvider
     {
         { "SomeRequest", new RequestMetadata {
               new ThrottleAttribute(2)
           } },
         new RequestMetadata {
             new WatchDogTimeoutAttribute(2, 0, 0)
         }
     });
 }
Exemplo n.º 9
0
        /// <summary>
        /// Registers throttling middleware using <paramref name="builder"/> with the specified <paramref name="options"/>.
        /// </summary>
        /// <param name="builder">The builder to use for registration.</param>
        /// <param name="options">The throttling configuration.</param>
        /// <returns><paramref name="builder"/></returns>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="builder"/> or <paramref name="options"/> is null.
        /// </exception>
        public static IRegistrar <IRequestProcessorMiddleware> AddThrottling(this IRegistrar <IRequestProcessorMiddleware> builder, Action <ThrottleOptions> configureOptions)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }
            if (configureOptions == null)
            {
                throw new ArgumentNullException(nameof(configureOptions));
            }

            var options = OptionsBuilder.ConfigureAndValidate(configureOptions);

            return(builder.Add(ctx => new ThrottleMiddleware(options)));
        }
Exemplo n.º 10
0
 private static void Steps(IRegistrar <IRequestProcessorMiddleware> services)
 {
     services
     .Add(builder => new DemoMiddleware(builder.GetLogger()))
     .Add(CreateMetricsMiddleware)
     .AddRetry(new RetryPolicy(new[] { typeof(TimeoutException) }))
     .AddThrottling(options =>
     {
         options.DefaultRunAtOnceInGroup = 2;
     })
     .AddWatchDog(options =>
     {
         options.DefaultTimeout = TimeSpan.FromSeconds(10);
     });
 }
Exemplo n.º 11
0
 public static IRegistrar <IRequestProcessorMiddleware> AddSimpleInjector(this IRegistrar <IRequestProcessorMiddleware> collection, Container container)
 {
     return(collection.Add(async(ctx, next) =>
     {
         if (IsServiceProviderAttached(ctx))
         {
             await next(ctx);
         }
         else
         {
             using (var scope = AsyncScopedLifestyle.BeginScope(container))
             {
                 AttachServiceProvider(ctx, new ScopeAdapter(scope));
                 await next(ctx);
             }
         }
     }));
 }
        public static IRegistrar <IRequestProcessorMiddleware> AddResolveFormatter(this IRegistrar <IRequestProcessorMiddleware> builder,
                                                                                   IEnumerable <IRequestContentFormatter> formatters, IRequestContentFormatter fallbackFormatter)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }
            if (formatters == null)
            {
                throw new ArgumentNullException(nameof(formatters));
            }
            if (fallbackFormatter == null)
            {
                throw new ArgumentNullException(nameof(fallbackFormatter));
            }

            return(builder.Add(ctx => new FormattingMiddleware(formatters, fallbackFormatter)));
        }