源码解析Grpc拦截器(C#版本)

本文深入解析gRPC在C#中的拦截器实现,详细介绍了客户端和服务端拦截器的工作原理。讲解了Interceptor和CallInvoker抽象类,以及如何添加和使用拦截器。客户端拦截器通过拦截CallInvoker调用来执行,服务端拦截器则在ServerServiceDefinition中绑定并执行。文章还提供了源码分析,帮助读者更好地理解和应用gRPC拦截器。

前言

其实Grpc拦截器是我以前研究过,但是我看网上相关C#版本的源码解析相对少一点,所以笔者借这篇文章给大家分享下Grpc拦截器的实现,废话不多说,直接开讲(Grpc的源码看着很方便,包自动都能还原成功。.Net源码就硬生啃。。。弄了半天没还原成功😂)。ps:

•本篇文章主要是讲解源码,并不进行举例Demo,所以读者尽量先写一个小Demo,看看生成的代码,然后伴随着看文章。•如果没有用过Grpc的读者,可以先写个小Demo,可以看官网点击这里[1],主要是查看下通过Proto文件生成的代码的格式。•这篇文章讲解分别从客户端和服务端两部分讲解(实现有点不一样),篇幅原因只讲解一元调用的示例,其他形式的调用其实是类似的。

Client端

Interceptor和CallInvoker抽象类

public abstract class Interceptor
{
    //一元调用同步拦截器
    public virtual TResponse BlockingUnaryCall<TRequest, TResponse>(TRequest request, ClientInterceptorContext<TRequest, TResponse> context, BlockingUnaryCallContinuation<TRequest, TResponse> continuation)
        where TRequest : class
        where TResponse : class
    {
        return continuation(request, context);
    }
    //一元调用异步拦截器
    public virtual AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(TRequest request, ClientInterceptorContext<TRequest, TResponse> context, AsyncUnaryCallContinuation<TRequest, TResponse> continuation)
        where TRequest : class
        where TResponse : class
    {
        return continuation(request, context);
    }
}
public abstract class CallInvoker
{
    //一元调用同步拦截器
    public abstract TResponse BlockingUnaryCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string host, CallOptions options, TRequest request)
        where TRequest : class
        where TResponse : class;
    //一元调用异步拦截器
    public abstract AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string host, CallOptions options, TRequest request)
        where TRequest : class
        where TResponse : class;
}
 

首先我们要理解这两个抽象类分别是干什么的,上述代码讲解:

•Interceptor我们知道,在实现自定义的拦截器时,需要继承这个类,并对某些方法进行自定义的实现,而continuation就是调用下一个拦截器。•其实CallInvoker其实就是客户端构造的对象,主要用于调用远程服务,通过你自己实现的Demo可以看到,先创建Channel,然后通过Channe创建默认的CallInvoker,而在创建Client通过proto生成的文件里可以看到对应的重载构造函数。

添加拦截器

public static class CallInvokerExtensions
{
    //增加一个拦截器
    public static CallInvoker Intercept(this CallInvoker invoker, Interceptor interceptor)
    {
        return new InterceptingCallInvoker(invoker, interceptor);
    }
    //增加一组拦截器
    public static CallInvoker Intercept(this CallInvoker invoker, params Interceptor[] interceptors)
    {
        //检查是否为Null
        GrpcPreconditions.CheckNotNull(invoker, nameof(invoker));
        GrpcPreconditions.CheckNotNull(interceptors, nameof(interceptors));
        //反转集合,构造对象
        foreach (var interceptor in interceptors.Reverse())
        {
            invoker = Intercept(invoker, interceptor);
        }

        return invoker;
    }
    //篇幅原因,这种方式这里不进行讲解,大家可以自己翻
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值