Skip to content

Commit

Permalink
结果扫描与完整搜索不共用锁
Browse files Browse the repository at this point in the history
  • Loading branch information
xljiulang committed Jun 16, 2021
1 parent 0be0610 commit a9025be
Show file tree
Hide file tree
Showing 15 changed files with 270 additions and 158 deletions.
1 change: 1 addition & 0 deletions FastGithub.Core/FastGithub.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Nullable>enable</Nullable>
<RootNamespace>FastGithub</RootNamespace>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
using System;
using System.Threading.Tasks;

namespace FastGithub.Scanner
namespace FastGithub
{
/// <summary>
/// 定义中间件的接口
/// </summary>
interface IGithubScanMiddleware
/// </summary>
/// <typeparam name="TContext"></typeparam>
public interface IMiddleware<TContext>
{
/// <summary>
/// 执行中间件
/// </summary>
/// <param name="context">上下文</param>
/// <param name="next">下一个中间件</param>
/// <returns></returns>
Task InvokeAsync(GithubContext context, Func<Task> next);
Task InvokeAsync(TContext context, Func<Task> next);
}
}
54 changes: 54 additions & 0 deletions FastGithub.Core/IPipelineBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Threading.Tasks;

namespace FastGithub
{
/// <summary>
/// 定义中间件管道创建者的接口
/// </summary>
/// <typeparam name="TContext">中间件上下文</typeparam>
public interface IPipelineBuilder<TContext>
{
/// <summary>
/// 获取服务提供者
/// </summary>
IServiceProvider AppServices { get; }

/// <summary>
/// 使用中间件
/// </summary>
/// <typeparam name="TContext"></typeparam>
/// <typeparam name="TMiddleware"></typeparam>
/// <param name="builder"></param>
/// <returns></returns>
IPipelineBuilder<TContext> Use<TMiddleware>() where TMiddleware : class, IMiddleware<TContext>;

/// <summary>
/// 使用中间件
/// </summary>
/// <typeparam name="TContext"></typeparam>
/// <param name="builder"></param>
/// <param name="middleware"></param>
/// <returns></returns>
IPipelineBuilder<TContext> Use(Func<TContext, Func<Task>, Task> middleware);

/// <summary>
/// 使用中间件
/// </summary>
/// <param name="middleware">中间件</param>
/// <returns></returns>
IPipelineBuilder<TContext> Use(Func<InvokeDelegate<TContext>, InvokeDelegate<TContext>> middleware);

/// <summary>
/// 创建所有中间件执行处理者
/// </summary>
/// <returns></returns>
InvokeDelegate<TContext> Build();

/// <summary>
/// 使用默认配制创建新的PipelineBuilder
/// </summary>
/// <returns></returns>
IPipelineBuilder<TContext> New();
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System.Threading.Tasks;

namespace FastGithub.Scanner
namespace FastGithub
{
/// <summary>
/// 表示所有中间件执行委托
/// </summary>
/// </summary>
/// <typeparam name="TContext">中间件上下文类型</typeparam>
/// <param name="context">中间件上下文</param>
/// <returns></returns>
delegate Task GithubScanDelegate(GithubContext context);
public delegate Task InvokeDelegate<TContext>(TContext context);
}
103 changes: 103 additions & 0 deletions FastGithub.Core/PipelineBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace FastGithub
{
/// <summary>
/// 表示中间件创建者
/// </summary>
public class PipelineBuilder<TContext> : IPipelineBuilder<TContext>
{
private readonly InvokeDelegate<TContext> completedHandler;
private readonly List<Func<InvokeDelegate<TContext>, InvokeDelegate<TContext>>> middlewares = new List<Func<InvokeDelegate<TContext>, InvokeDelegate<TContext>>>();

/// <summary>
/// 获取服务提供者
/// </summary>
public IServiceProvider AppServices { get; }

/// <summary>
/// 中间件创建者
/// </summary>
/// <param name="appServices"></param>
public PipelineBuilder(IServiceProvider appServices)
: this(appServices, context => Task.CompletedTask)
{
}

/// <summary>
/// 中间件创建者
/// </summary>
/// <param name="appServices"></param>
/// <param name="completedHandler">完成执行内容处理者</param>
public PipelineBuilder(IServiceProvider appServices, InvokeDelegate<TContext> completedHandler)
{
this.AppServices = appServices;
this.completedHandler = completedHandler;
}


/// <summary>
/// 使用中间件
/// </summary>
/// <typeparam name="TContext"></typeparam>
/// <typeparam name="TMiddleware"></typeparam>
/// <param name="builder"></param>
/// <returns></returns>
public IPipelineBuilder<TContext> Use<TMiddleware>() where TMiddleware : class, IMiddleware<TContext>
{
var middleware = this.AppServices.GetRequiredService<TMiddleware>();
return this.Use(middleware.InvokeAsync);
}

/// <summary>
/// 使用中间件
/// </summary>
/// <typeparam name="TContext"></typeparam>
/// <param name="builder"></param>
/// <param name="middleware"></param>
/// <returns></returns>
public IPipelineBuilder<TContext> Use(Func<TContext, Func<Task>, Task> middleware)
{
return this.Use(next => context => middleware(context, () => next(context)));
}

/// <summary>
/// 使用中间件
/// </summary>
/// <param name="middleware"></param>
/// <returns></returns>
public IPipelineBuilder<TContext> Use(Func<InvokeDelegate<TContext>, InvokeDelegate<TContext>> middleware)
{
this.middlewares.Add(middleware);
return this;
}


/// <summary>
/// 创建所有中间件执行处理者
/// </summary>
/// <returns></returns>
public InvokeDelegate<TContext> Build()
{
var handler = this.completedHandler;
for (var i = this.middlewares.Count - 1; i >= 0; i--)
{
handler = this.middlewares[i](handler);
}
return handler;
}


/// <summary>
/// 使用默认配制创建新的PipelineBuilder
/// </summary>
/// <returns></returns>
public IPipelineBuilder<TContext> New()
{
return new PipelineBuilder<TContext>(this.AppServices, this.completedHandler);
}
}
}
71 changes: 71 additions & 0 deletions FastGithub.Core/PipelineBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;

namespace FastGithub
{
/// <summary>
/// 中间件创建者扩展
/// </summary>
public static class PipelineBuilderExtensions
{
/// <summary>
/// 中断执行中间件
/// </summary>
/// <typeparam name="TContext"></typeparam>
/// <param name="builder"></param>
/// <param name="handler">处理者</param>
/// <returns></returns>
public static IPipelineBuilder<TContext> Run<TContext>(this IPipelineBuilder<TContext> builder, InvokeDelegate<TContext> handler)
{
return builder.Use(_ => handler);
}

/// <summary>
/// 条件中间件
/// </summary>
/// <typeparam name="TContext"></typeparam>
/// <param name="builder"></param>
/// <param name="predicate"></param>
/// <param name="handler"></param>
/// <returns></returns>
public static IPipelineBuilder<TContext> When<TContext>(this IPipelineBuilder<TContext> builder, Func<TContext, bool> predicate, InvokeDelegate<TContext> handler)
{
return builder.Use(next => async context =>
{
if (predicate.Invoke(context) == true)
{
await handler.Invoke(context);
}
else
{
await next(context);
}
});
}


/// <summary>
/// 条件中间件
/// </summary>
/// <typeparam name="TContext"></typeparam>
/// <param name="builder"></param>
/// <param name="predicate"></param>
/// <param name="configureAction"></param>
/// <returns></returns>
public static IPipelineBuilder<TContext> When<TContext>(this IPipelineBuilder<TContext> builder, Func<TContext, bool> predicate, Action<IPipelineBuilder<TContext>> configureAction)
{
return builder.Use(next => async context =>
{
if (predicate.Invoke(context) == true)
{
var branchBuilder = builder.New();
configureAction(branchBuilder);
await branchBuilder.Build().Invoke(context);
}
else
{
await next(context);
}
});
}
}
}
66 changes: 0 additions & 66 deletions FastGithub.Scanner/GithubScanBuilder.cs

This file was deleted.

36 changes: 0 additions & 36 deletions FastGithub.Scanner/GithubScanBuilderExtensions.cs

This file was deleted.

Loading

0 comments on commit a9025be

Please sign in to comment.