grpc-go源码阅读(1) grpc server的启动

正常启动一个grpcServer demo如下:

func main() {
    // 监听端口
    lis, err := net.Listen("tcp", port)
    if err != nil {
        log.Fatalf("failed to listen: %v", err)
    }
    s := grpc.NewServer()
    pb.RegisterGreeterServer(s, &server{})
    // Register reflection service on gRPC server.
    reflection.Register(s)
    if err := s.Serve(lis); err != nil {
        log.Fatalf("failed to serve: %v", err)
    }
}

1.grpc.NewServer

// NewServer creates a gRPC server which has no service registered and has not
// started to accept requests yet.
func NewServer(opt ...ServerOption) *Server {
    var opts options
    opts.maxMsgSize = defaultMaxMsgSize // 默认4MB
    for _, o := range opt {
        o(&opts)
    }
    if opts.codec == nil {
        // Set the default codec.
        opts.codec = protoCodec{}
    }
    s := &Server{
        lis:   make(map[net.Listener]bool),
        opts:  opts,
        conns: make(map[io.Closer]bool),
        m:     make(map[string]*service),
    }
    s.cv = sync.NewCond(&s.mu) //  cond实例,可以唤醒等待mu锁的goroutine
    s.ctx, s.cancel = context.WithCancel(context.Background())
    if EnableTracing {   //  controls whether to trace RPCs using the golang.org/x/net/trace package
        _, file, line, _ := runtime.Caller(1)   //  runtime库的Caller函数,可以返回运行时正在执行的文件名和行号
        s.events = trace.NewEventLog("grpc.Server", fmt.Sprintf("%s:%d", file, line))
    }
    return s
}

  • ServerOption参数(Server的配置选项),启动时可不传,结构如下:
type options struct {
    creds                credentials.TransportCredentials  // cred证书
    codec                Codec       // Codec defines the interface gRPC uses to encode and decode messages,默认为protobuf
    cp                   Compressor  // Compressor defines the interface gRPC uses to compress a message.
    dc                   Decompressor  //Decompressor defines the interface gRPC uses to decompress a message.

    maxMsgSize           int  // the max message size in bytes the server can receive,If this is not set, gRPC uses the default 4MB.(默认4MB)
    unaryInt             UnaryServerInterceptor  //provides a hook to intercept the execution of a unary RPC on the server(拦截器)
    streamInt            StreamServerInterceptor  //provides a hook to intercept the execution of a streaming RPC on the server(拦截器)
    inTapHandle          tap.ServerInHandle  //sets the tap handle for all the server transport to be created
    statsHandler         stats.Handler
    maxConcurrentStreams uint32 //  一个连接中最大并发Stream数
    useHandlerImpl       bool   // use http.Handler-based server 
}
注释来源:https://godoc.org/google.golang.org/grpc#UnaryInterceptor

credentials.TransportCredentials 是grpc提供认证的证书

RPC 默认提供了两种认证方式:

基于SSL/TLS认证方式

  • 远程调用认证方式

  • 两种方式可以混合使用

具体详情参考:https://segmentfault.com/a/1190000007933303

通过grpc.Creds(creds)设置该参数

options.useHandlerImpl 是控制处理RawConn的主要判别flag

g-.png

UnaryServerInterceptor/StreamServerInterceptor

详情介绍:https://github.com/grpc-ecosystem/go-grpc-middleware / https://www.colabug.com/2496579.html

2.RegisterService

// RegisterService register a service and its implementation to the gRPC
// server. Called from the IDL generated code. This must be called before
// invoking Serve.
func (s *Server) RegisterService(sd *ServiceDesc, ss interface{}) {
    ht := reflect.TypeOf(sd.HandlerType).Elem()    // 获取sd.HandlerType类型
    st := reflect.TypeOf(ss)                                      // 获取ss类型
    if !st.Implements(ht) {  //ss接口必须继承sd的处理类型接口
        grpclog.Fatalf("grpc: Server.RegisterService found the handler of type %v that does not satisfy %v", st, ht)
    }
    s.register(sd, ss)
}

func (s *Server) register(sd *ServiceDesc, ss interface{}) {
    s.mu.Lock()
    defer s.mu.Unlock()
    s.printf("RegisterService(%q)", sd.ServiceName)  // proto的service struct名称
    if _, ok := s.m[sd.ServiceName]; ok {  // serivce已注册
        grpclog.Fatalf("grpc: Server.RegisterService found duplicate service registration for %q", sd.ServiceName)
    }
    srv := &service{  // 构造service类,包括该service的information及method
        server: ss,
        md:     make(map[string]*MethodDesc),
        sd:     make(map[string]*StreamDesc),
        mdata:  sd.Metadata,
    }
    for i := range sd.Methods {  // 注册pb中service方法对应的handle
        d := &sd.Methods[i]
        srv.md[d.MethodName] = d
    }
    for i := range sd.Streams {  // 注册stream对应的handle
        d := &sd.Streams[i]
        srv.sd[d.StreamName] = d
    }
    s.m[sd.ServiceName] = srv  // 将service加入grpc server
}

3.Serve(lis net.Listener)

net.Error接口
//net.Error 
type Error interface {
    error
    Timeout() bool   // Is the error a timeout?
    Temporary() bool // Is the error temporary?
}
// Serve accepts incoming connections on the listener lis, creating a new
// ServerTransport and service goroutine for each. The service goroutines
// read gRPC requests and then call the registered handlers to reply to them.
// Serve returns when lis.Accept fails with fatal errors.  lis will be closed when
// this method returns.
// Serve always returns non-nil error.
func (s *Server) Serve(lis net.Listener) error {
    s.mu.Lock()
    s.printf("serving")
    if s.lis == nil {
        s.mu.Unlock()
        lis.Close()
        return ErrServerStopped
    }
    s.lis[lis] = true   // 设置grpcServer的lis为true
    s.mu.Unlock()
    defer func() {  // 退出函数时关闭lis,删除grpcServer的lis
        s.mu.Lock()
        if s.lis != nil && s.lis[lis] {
            lis.Close()
            delete(s.lis, lis)
        }
        s.mu.Unlock()
    }()

    var tempDelay time.Duration // how long to sleep on accept failure

    for {
        rawConn, err := lis.Accept()
        if err != nil {
            if ne, ok := err.(interface {
                Temporary() bool
            }); ok && ne.Temporary() {
                if tempDelay == 0 {
                    tempDelay = 5 * time.Millisecond   // 第一次接受失败retry,延迟5毫秒
                } else {
                    tempDelay *= 2                              // 之后每次retry递增1倍时间
                }
                if max := 1 * time.Second; tempDelay > max {  // 最大等待1秒
                    tempDelay = max
                }
                s.mu.Lock()
                s.printf("Accept error: %v; retrying in %v", err, tempDelay)
                s.mu.Unlock()
                select {  //  等待时间超时或context cancle时才继续往下
                case <-time.After(tempDelay):  
                case <-s.ctx.Done():
                }
                continue
            }
            s.mu.Lock()
            s.printf("done serving; Accept = %v", err)
            s.mu.Unlock()
            return err
        }
        tempDelay = 0
        // Start a new goroutine to deal with rawConn
        // so we don't stall this Accept loop goroutine.
        go s.handleRawConn(rawConn)  // 处理rawConn并不会导致grpc停止accept
    }
}

4.handleRawConn

// handleRawConn is run in its own goroutine and handles a just-accepted
// connection that has not had any I/O performed on it yet.
func (s *Server) handleRawConn(rawConn net.Conn) {
    conn, authInfo, err := s.useTransportAuthenticator(rawConn)
    if err != nil {
        s.mu.Lock()
        s.errorf("ServerHandshake(%q) failed: %v", rawConn.RemoteAddr(), err)
        s.mu.Unlock()
        grpclog.Printf("grpc: Server.Serve failed to complete security handshake from %q: %v", rawConn.RemoteAddr(), err)
        // If serverHandShake returns ErrConnDispatched, keep rawConn open.
        if err != credentials.ErrConnDispatched {  // 如果是认证error,保持rawConn open
            rawConn.Close()
        }
        return
    }

    s.mu.Lock()
    if s.conns == nil {
        s.mu.Unlock()
        conn.Close()
        return
    }
    s.mu.Unlock()

    if s.opts.useHandlerImpl {    // 选择不同包的的http2服务,默认调用serveUsingHandler
        s.serveUsingHandler(conn)
    } else {
        s.serveHTTP2Transport(conn, authInfo)
    }
}
// serveUsingHandler is called from handleRawConn when s is configured
// to handle requests via the http.Handler interface. It sets up a
// net/http.Server to handle the just-accepted conn. The http.Server
// is configured to route all incoming requests (all HTTP/2 streams)
// to ServeHTTP, which creates a new ServerTransport for each stream.
// serveUsingHandler blocks until conn closes.
//
// This codepath is only used when Server.TestingUseHandlerImpl has
// been configured. This lets the end2end tests exercise the ServeHTTP
// method as one of the environment types.
//
// conn is the *tls.Conn that's already been authenticated.
func (s *Server) serveUsingHandler(conn net.Conn) {
    if !s.addConn(conn) {  // conn注册到grpc server中
        conn.Close()
        return
    }
    defer s.removeConn(conn)
    h2s := &http2.Server{
        MaxConcurrentStreams: s.opts.maxConcurrentStreams,  //一个连接中最大并发Stream数
    }
    h2s.ServeConn(conn, &http2.ServeConnOpts{
        Handler: s,
    })
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,458评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,454评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,171评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,062评论 0 207
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,440评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,661评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,906评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,609评论 0 200
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,379评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,600评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,085评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,409评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,072评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,088评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,860评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,704评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,608评论 2 270

推荐阅读更多精彩内容