1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741 |
- package grpc
- import (
- "context"
- "errors"
- "fmt"
- "io"
- "math"
- "net"
- "net/http"
- "reflect"
- "runtime"
- "strings"
- "sync"
- "sync/atomic"
- "time"
- "golang.org/x/net/trace"
- "google.golang.org/grpc/codes"
- "google.golang.org/grpc/credentials"
- "google.golang.org/grpc/encoding"
- "google.golang.org/grpc/encoding/proto"
- "google.golang.org/grpc/grpclog"
- "google.golang.org/grpc/internal/binarylog"
- "google.golang.org/grpc/internal/channelz"
- "google.golang.org/grpc/internal/grpcrand"
- "google.golang.org/grpc/internal/grpcsync"
- "google.golang.org/grpc/internal/transport"
- "google.golang.org/grpc/keepalive"
- "google.golang.org/grpc/metadata"
- "google.golang.org/grpc/peer"
- "google.golang.org/grpc/stats"
- "google.golang.org/grpc/status"
- "google.golang.org/grpc/tap"
- )
- const (
- defaultServerMaxReceiveMessageSize = 1024 * 1024 * 4
- defaultServerMaxSendMessageSize = math.MaxInt32
- )
- var statusOK = status.New(codes.OK, "")
- var logger = grpclog.Component("core")
- type methodHandler func(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor UnaryServerInterceptor) (interface{}, error)
- type MethodDesc struct {
- MethodName string
- Handler methodHandler
- }
- type ServiceDesc struct {
- ServiceName string
-
-
- HandlerType interface{}
- Methods []MethodDesc
- Streams []StreamDesc
- Metadata interface{}
- }
- type service struct {
- server interface{}
- md map[string]*MethodDesc
- sd map[string]*StreamDesc
- mdata interface{}
- }
- type serverWorkerData struct {
- st transport.ServerTransport
- wg *sync.WaitGroup
- stream *transport.Stream
- }
- type Server struct {
- opts serverOptions
- mu sync.Mutex
- lis map[net.Listener]bool
- conns map[transport.ServerTransport]bool
- serve bool
- drain bool
- cv *sync.Cond
- m map[string]*service
- events trace.EventLog
- quit *grpcsync.Event
- done *grpcsync.Event
- channelzRemoveOnce sync.Once
- serveWG sync.WaitGroup
- channelzID int64
- czData *channelzData
- serverWorkerChannels []chan *serverWorkerData
- }
- type serverOptions struct {
- creds credentials.TransportCredentials
- codec baseCodec
- cp Compressor
- dc Decompressor
- unaryInt UnaryServerInterceptor
- streamInt StreamServerInterceptor
- chainUnaryInts []UnaryServerInterceptor
- chainStreamInts []StreamServerInterceptor
- inTapHandle tap.ServerInHandle
- statsHandler stats.Handler
- maxConcurrentStreams uint32
- maxReceiveMessageSize int
- maxSendMessageSize int
- unknownStreamDesc *StreamDesc
- keepaliveParams keepalive.ServerParameters
- keepalivePolicy keepalive.EnforcementPolicy
- initialWindowSize int32
- initialConnWindowSize int32
- writeBufferSize int
- readBufferSize int
- connectionTimeout time.Duration
- maxHeaderListSize *uint32
- headerTableSize *uint32
- numServerWorkers uint32
- }
- var defaultServerOptions = serverOptions{
- maxReceiveMessageSize: defaultServerMaxReceiveMessageSize,
- maxSendMessageSize: defaultServerMaxSendMessageSize,
- connectionTimeout: 120 * time.Second,
- writeBufferSize: defaultWriteBufSize,
- readBufferSize: defaultReadBufSize,
- }
- type ServerOption interface {
- apply(*serverOptions)
- }
- type EmptyServerOption struct{}
- func (EmptyServerOption) apply(*serverOptions) {}
- type funcServerOption struct {
- f func(*serverOptions)
- }
- func (fdo *funcServerOption) apply(do *serverOptions) {
- fdo.f(do)
- }
- func newFuncServerOption(f func(*serverOptions)) *funcServerOption {
- return &funcServerOption{
- f: f,
- }
- }
- func WriteBufferSize(s int) ServerOption {
- return newFuncServerOption(func(o *serverOptions) {
- o.writeBufferSize = s
- })
- }
- func ReadBufferSize(s int) ServerOption {
- return newFuncServerOption(func(o *serverOptions) {
- o.readBufferSize = s
- })
- }
- func InitialWindowSize(s int32) ServerOption {
- return newFuncServerOption(func(o *serverOptions) {
- o.initialWindowSize = s
- })
- }
- func InitialConnWindowSize(s int32) ServerOption {
- return newFuncServerOption(func(o *serverOptions) {
- o.initialConnWindowSize = s
- })
- }
- func KeepaliveParams(kp keepalive.ServerParameters) ServerOption {
- if kp.Time > 0 && kp.Time < time.Second {
- logger.Warning("Adjusting keepalive ping interval to minimum period of 1s")
- kp.Time = time.Second
- }
- return newFuncServerOption(func(o *serverOptions) {
- o.keepaliveParams = kp
- })
- }
- func KeepaliveEnforcementPolicy(kep keepalive.EnforcementPolicy) ServerOption {
- return newFuncServerOption(func(o *serverOptions) {
- o.keepalivePolicy = kep
- })
- }
- func CustomCodec(codec Codec) ServerOption {
- return newFuncServerOption(func(o *serverOptions) {
- o.codec = codec
- })
- }
- func RPCCompressor(cp Compressor) ServerOption {
- return newFuncServerOption(func(o *serverOptions) {
- o.cp = cp
- })
- }
- func RPCDecompressor(dc Decompressor) ServerOption {
- return newFuncServerOption(func(o *serverOptions) {
- o.dc = dc
- })
- }
- func MaxMsgSize(m int) ServerOption {
- return MaxRecvMsgSize(m)
- }
- func MaxRecvMsgSize(m int) ServerOption {
- return newFuncServerOption(func(o *serverOptions) {
- o.maxReceiveMessageSize = m
- })
- }
- func MaxSendMsgSize(m int) ServerOption {
- return newFuncServerOption(func(o *serverOptions) {
- o.maxSendMessageSize = m
- })
- }
- func MaxConcurrentStreams(n uint32) ServerOption {
- return newFuncServerOption(func(o *serverOptions) {
- o.maxConcurrentStreams = n
- })
- }
- func Creds(c credentials.TransportCredentials) ServerOption {
- return newFuncServerOption(func(o *serverOptions) {
- o.creds = c
- })
- }
- func UnaryInterceptor(i UnaryServerInterceptor) ServerOption {
- return newFuncServerOption(func(o *serverOptions) {
- if o.unaryInt != nil {
- panic("The unary server interceptor was already set and may not be reset.")
- }
- o.unaryInt = i
- })
- }
- func ChainUnaryInterceptor(interceptors ...UnaryServerInterceptor) ServerOption {
- return newFuncServerOption(func(o *serverOptions) {
- o.chainUnaryInts = append(o.chainUnaryInts, interceptors...)
- })
- }
- func StreamInterceptor(i StreamServerInterceptor) ServerOption {
- return newFuncServerOption(func(o *serverOptions) {
- if o.streamInt != nil {
- panic("The stream server interceptor was already set and may not be reset.")
- }
- o.streamInt = i
- })
- }
- func ChainStreamInterceptor(interceptors ...StreamServerInterceptor) ServerOption {
- return newFuncServerOption(func(o *serverOptions) {
- o.chainStreamInts = append(o.chainStreamInts, interceptors...)
- })
- }
- func InTapHandle(h tap.ServerInHandle) ServerOption {
- return newFuncServerOption(func(o *serverOptions) {
- if o.inTapHandle != nil {
- panic("The tap handle was already set and may not be reset.")
- }
- o.inTapHandle = h
- })
- }
- func StatsHandler(h stats.Handler) ServerOption {
- return newFuncServerOption(func(o *serverOptions) {
- o.statsHandler = h
- })
- }
- func UnknownServiceHandler(streamHandler StreamHandler) ServerOption {
- return newFuncServerOption(func(o *serverOptions) {
- o.unknownStreamDesc = &StreamDesc{
- StreamName: "unknown_service_handler",
- Handler: streamHandler,
-
- ClientStreams: true,
- ServerStreams: true,
- }
- })
- }
- func ConnectionTimeout(d time.Duration) ServerOption {
- return newFuncServerOption(func(o *serverOptions) {
- o.connectionTimeout = d
- })
- }
- func MaxHeaderListSize(s uint32) ServerOption {
- return newFuncServerOption(func(o *serverOptions) {
- o.maxHeaderListSize = &s
- })
- }
- func HeaderTableSize(s uint32) ServerOption {
- return newFuncServerOption(func(o *serverOptions) {
- o.headerTableSize = &s
- })
- }
- func NumStreamWorkers(numServerWorkers uint32) ServerOption {
-
-
-
-
- return newFuncServerOption(func(o *serverOptions) {
- o.numServerWorkers = numServerWorkers
- })
- }
- const serverWorkerResetThreshold = 1 << 16
- func (s *Server) serverWorker(ch chan *serverWorkerData) {
-
-
- threshold := serverWorkerResetThreshold + grpcrand.Intn(serverWorkerResetThreshold)
- for completed := 0; completed < threshold; completed++ {
- data, ok := <-ch
- if !ok {
- return
- }
- s.handleStream(data.st, data.stream, s.traceInfo(data.st, data.stream))
- data.wg.Done()
- }
- go s.serverWorker(ch)
- }
- func (s *Server) initServerWorkers() {
- s.serverWorkerChannels = make([]chan *serverWorkerData, s.opts.numServerWorkers)
- for i := uint32(0); i < s.opts.numServerWorkers; i++ {
- s.serverWorkerChannels[i] = make(chan *serverWorkerData)
- go s.serverWorker(s.serverWorkerChannels[i])
- }
- }
- func (s *Server) stopServerWorkers() {
- for i := uint32(0); i < s.opts.numServerWorkers; i++ {
- close(s.serverWorkerChannels[i])
- }
- }
- func NewServer(opt ...ServerOption) *Server {
- opts := defaultServerOptions
- for _, o := range opt {
- o.apply(&opts)
- }
- s := &Server{
- lis: make(map[net.Listener]bool),
- opts: opts,
- conns: make(map[transport.ServerTransport]bool),
- m: make(map[string]*service),
- quit: grpcsync.NewEvent(),
- done: grpcsync.NewEvent(),
- czData: new(channelzData),
- }
- chainUnaryServerInterceptors(s)
- chainStreamServerInterceptors(s)
- s.cv = sync.NewCond(&s.mu)
- if EnableTracing {
- _, file, line, _ := runtime.Caller(1)
- s.events = trace.NewEventLog("grpc.Server", fmt.Sprintf("%s:%d", file, line))
- }
- if s.opts.numServerWorkers > 0 {
- s.initServerWorkers()
- }
- if channelz.IsOn() {
- s.channelzID = channelz.RegisterServer(&channelzServer{s}, "")
- }
- return s
- }
- func (s *Server) printf(format string, a ...interface{}) {
- if s.events != nil {
- s.events.Printf(format, a...)
- }
- }
- func (s *Server) errorf(format string, a ...interface{}) {
- if s.events != nil {
- s.events.Errorf(format, a...)
- }
- }
- func (s *Server) RegisterService(sd *ServiceDesc, ss interface{}) {
- ht := reflect.TypeOf(sd.HandlerType).Elem()
- st := reflect.TypeOf(ss)
- if !st.Implements(ht) {
- logger.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)
- if s.serve {
- logger.Fatalf("grpc: Server.RegisterService after Server.Serve for %q", sd.ServiceName)
- }
- if _, ok := s.m[sd.ServiceName]; ok {
- logger.Fatalf("grpc: Server.RegisterService found duplicate service registration for %q", sd.ServiceName)
- }
- srv := &service{
- server: ss,
- md: make(map[string]*MethodDesc),
- sd: make(map[string]*StreamDesc),
- mdata: sd.Metadata,
- }
- for i := range sd.Methods {
- d := &sd.Methods[i]
- srv.md[d.MethodName] = d
- }
- for i := range sd.Streams {
- d := &sd.Streams[i]
- srv.sd[d.StreamName] = d
- }
- s.m[sd.ServiceName] = srv
- }
- type MethodInfo struct {
-
- Name string
-
- IsClientStream bool
-
- IsServerStream bool
- }
- type ServiceInfo struct {
- Methods []MethodInfo
-
- Metadata interface{}
- }
- func (s *Server) GetServiceInfo() map[string]ServiceInfo {
- ret := make(map[string]ServiceInfo)
- for n, srv := range s.m {
- methods := make([]MethodInfo, 0, len(srv.md)+len(srv.sd))
- for m := range srv.md {
- methods = append(methods, MethodInfo{
- Name: m,
- IsClientStream: false,
- IsServerStream: false,
- })
- }
- for m, d := range srv.sd {
- methods = append(methods, MethodInfo{
- Name: m,
- IsClientStream: d.ClientStreams,
- IsServerStream: d.ServerStreams,
- })
- }
- ret[n] = ServiceInfo{
- Methods: methods,
- Metadata: srv.mdata,
- }
- }
- return ret
- }
- var ErrServerStopped = errors.New("grpc: the server has been stopped")
- func (s *Server) useTransportAuthenticator(rawConn net.Conn) (net.Conn, credentials.AuthInfo, error) {
- if s.opts.creds == nil {
- return rawConn, nil, nil
- }
- return s.opts.creds.ServerHandshake(rawConn)
- }
- type listenSocket struct {
- net.Listener
- channelzID int64
- }
- func (l *listenSocket) ChannelzMetric() *channelz.SocketInternalMetric {
- return &channelz.SocketInternalMetric{
- SocketOptions: channelz.GetSocketOption(l.Listener),
- LocalAddr: l.Listener.Addr(),
- }
- }
- func (l *listenSocket) Close() error {
- err := l.Listener.Close()
- if channelz.IsOn() {
- channelz.RemoveEntry(l.channelzID)
- }
- return err
- }
- func (s *Server) Serve(lis net.Listener) error {
- s.mu.Lock()
- s.printf("serving")
- s.serve = true
- if s.lis == nil {
-
- s.mu.Unlock()
- lis.Close()
- return ErrServerStopped
- }
- s.serveWG.Add(1)
- defer func() {
- s.serveWG.Done()
- if s.quit.HasFired() {
-
- <-s.done.Done()
- }
- }()
- ls := &listenSocket{Listener: lis}
- s.lis[ls] = true
- if channelz.IsOn() {
- ls.channelzID = channelz.RegisterListenSocket(ls, s.channelzID, lis.Addr().String())
- }
- s.mu.Unlock()
- defer func() {
- s.mu.Lock()
- if s.lis != nil && s.lis[ls] {
- ls.Close()
- delete(s.lis, ls)
- }
- s.mu.Unlock()
- }()
- var tempDelay time.Duration
- 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
- } else {
- tempDelay *= 2
- }
- if max := 1 * time.Second; tempDelay > max {
- tempDelay = max
- }
- s.mu.Lock()
- s.printf("Accept error: %v; retrying in %v", err, tempDelay)
- s.mu.Unlock()
- timer := time.NewTimer(tempDelay)
- select {
- case <-timer.C:
- case <-s.quit.Done():
- timer.Stop()
- return nil
- }
- continue
- }
- s.mu.Lock()
- s.printf("done serving; Accept = %v", err)
- s.mu.Unlock()
- if s.quit.HasFired() {
- return nil
- }
- return err
- }
- tempDelay = 0
-
-
-
-
-
- s.serveWG.Add(1)
- go func() {
- s.handleRawConn(rawConn)
- s.serveWG.Done()
- }()
- }
- }
- func (s *Server) handleRawConn(rawConn net.Conn) {
- if s.quit.HasFired() {
- rawConn.Close()
- return
- }
- rawConn.SetDeadline(time.Now().Add(s.opts.connectionTimeout))
- conn, authInfo, err := s.useTransportAuthenticator(rawConn)
- if err != nil {
-
-
- if err != credentials.ErrConnDispatched {
- s.mu.Lock()
- s.errorf("ServerHandshake(%q) failed: %v", rawConn.RemoteAddr(), err)
- s.mu.Unlock()
- channelz.Warningf(logger, s.channelzID, "grpc: Server.Serve failed to complete security handshake from %q: %v", rawConn.RemoteAddr(), err)
- rawConn.Close()
- }
- rawConn.SetDeadline(time.Time{})
- return
- }
-
- st := s.newHTTP2Transport(conn, authInfo)
- if st == nil {
- return
- }
- rawConn.SetDeadline(time.Time{})
- if !s.addConn(st) {
- return
- }
- go func() {
- s.serveStreams(st)
- s.removeConn(st)
- }()
- }
- func (s *Server) newHTTP2Transport(c net.Conn, authInfo credentials.AuthInfo) transport.ServerTransport {
- config := &transport.ServerConfig{
- MaxStreams: s.opts.maxConcurrentStreams,
- AuthInfo: authInfo,
- InTapHandle: s.opts.inTapHandle,
- StatsHandler: s.opts.statsHandler,
- KeepaliveParams: s.opts.keepaliveParams,
- KeepalivePolicy: s.opts.keepalivePolicy,
- InitialWindowSize: s.opts.initialWindowSize,
- InitialConnWindowSize: s.opts.initialConnWindowSize,
- WriteBufferSize: s.opts.writeBufferSize,
- ReadBufferSize: s.opts.readBufferSize,
- ChannelzParentID: s.channelzID,
- MaxHeaderListSize: s.opts.maxHeaderListSize,
- HeaderTableSize: s.opts.headerTableSize,
- }
- st, err := transport.NewServerTransport("http2", c, config)
- if err != nil {
- s.mu.Lock()
- s.errorf("NewServerTransport(%q) failed: %v", c.RemoteAddr(), err)
- s.mu.Unlock()
- c.Close()
- channelz.Warning(logger, s.channelzID, "grpc: Server.Serve failed to create ServerTransport: ", err)
- return nil
- }
- return st
- }
- func (s *Server) serveStreams(st transport.ServerTransport) {
- defer st.Close()
- var wg sync.WaitGroup
- var roundRobinCounter uint32
- st.HandleStreams(func(stream *transport.Stream) {
- wg.Add(1)
- if s.opts.numServerWorkers > 0 {
- data := &serverWorkerData{st: st, wg: &wg, stream: stream}
- select {
- case s.serverWorkerChannels[atomic.AddUint32(&roundRobinCounter, 1)%s.opts.numServerWorkers] <- data:
- default:
-
- go func() {
- s.handleStream(st, stream, s.traceInfo(st, stream))
- wg.Done()
- }()
- }
- } else {
- go func() {
- defer wg.Done()
- s.handleStream(st, stream, s.traceInfo(st, stream))
- }()
- }
- }, func(ctx context.Context, method string) context.Context {
- if !EnableTracing {
- return ctx
- }
- tr := trace.New("grpc.Recv."+methodFamily(method), method)
- return trace.NewContext(ctx, tr)
- })
- wg.Wait()
- }
- var _ http.Handler = (*Server)(nil)
- func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
- st, err := transport.NewServerHandlerTransport(w, r, s.opts.statsHandler)
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- if !s.addConn(st) {
- return
- }
- defer s.removeConn(st)
- s.serveStreams(st)
- }
- func (s *Server) traceInfo(st transport.ServerTransport, stream *transport.Stream) (trInfo *traceInfo) {
- if !EnableTracing {
- return nil
- }
- tr, ok := trace.FromContext(stream.Context())
- if !ok {
- return nil
- }
- trInfo = &traceInfo{
- tr: tr,
- firstLine: firstLine{
- client: false,
- remoteAddr: st.RemoteAddr(),
- },
- }
- if dl, ok := stream.Context().Deadline(); ok {
- trInfo.firstLine.deadline = time.Until(dl)
- }
- return trInfo
- }
- func (s *Server) addConn(st transport.ServerTransport) bool {
- s.mu.Lock()
- defer s.mu.Unlock()
- if s.conns == nil {
- st.Close()
- return false
- }
- if s.drain {
-
-
- st.Drain()
- }
- s.conns[st] = true
- return true
- }
- func (s *Server) removeConn(st transport.ServerTransport) {
- s.mu.Lock()
- defer s.mu.Unlock()
- if s.conns != nil {
- delete(s.conns, st)
- s.cv.Broadcast()
- }
- }
- func (s *Server) channelzMetric() *channelz.ServerInternalMetric {
- return &channelz.ServerInternalMetric{
- CallsStarted: atomic.LoadInt64(&s.czData.callsStarted),
- CallsSucceeded: atomic.LoadInt64(&s.czData.callsSucceeded),
- CallsFailed: atomic.LoadInt64(&s.czData.callsFailed),
- LastCallStartedTimestamp: time.Unix(0, atomic.LoadInt64(&s.czData.lastCallStartedTime)),
- }
- }
- func (s *Server) incrCallsStarted() {
- atomic.AddInt64(&s.czData.callsStarted, 1)
- atomic.StoreInt64(&s.czData.lastCallStartedTime, time.Now().UnixNano())
- }
- func (s *Server) incrCallsSucceeded() {
- atomic.AddInt64(&s.czData.callsSucceeded, 1)
- }
- func (s *Server) incrCallsFailed() {
- atomic.AddInt64(&s.czData.callsFailed, 1)
- }
- func (s *Server) sendResponse(t transport.ServerTransport, stream *transport.Stream, msg interface{}, cp Compressor, opts *transport.Options, comp encoding.Compressor) error {
- data, err := encode(s.getCodec(stream.ContentSubtype()), msg)
- if err != nil {
- channelz.Error(logger, s.channelzID, "grpc: server failed to encode response: ", err)
- return err
- }
- compData, err := compress(data, cp, comp)
- if err != nil {
- channelz.Error(logger, s.channelzID, "grpc: server failed to compress response: ", err)
- return err
- }
- hdr, payload := msgHeader(data, compData)
-
- if len(payload) > s.opts.maxSendMessageSize {
- return status.Errorf(codes.ResourceExhausted, "grpc: trying to send message larger than max (%d vs. %d)", len(payload), s.opts.maxSendMessageSize)
- }
- err = t.Write(stream, hdr, payload, opts)
- if err == nil && s.opts.statsHandler != nil {
- s.opts.statsHandler.HandleRPC(stream.Context(), outPayload(false, msg, data, payload, time.Now()))
- }
- return err
- }
- func chainUnaryServerInterceptors(s *Server) {
-
-
- interceptors := s.opts.chainUnaryInts
- if s.opts.unaryInt != nil {
- interceptors = append([]UnaryServerInterceptor{s.opts.unaryInt}, s.opts.chainUnaryInts...)
- }
- var chainedInt UnaryServerInterceptor
- if len(interceptors) == 0 {
- chainedInt = nil
- } else if len(interceptors) == 1 {
- chainedInt = interceptors[0]
- } else {
- chainedInt = func(ctx context.Context, req interface{}, info *UnaryServerInfo, handler UnaryHandler) (interface{}, error) {
- return interceptors[0](ctx, req, info, getChainUnaryHandler(interceptors, 0, info, handler))
- }
- }
- s.opts.unaryInt = chainedInt
- }
- func getChainUnaryHandler(interceptors []UnaryServerInterceptor, curr int, info *UnaryServerInfo, finalHandler UnaryHandler) UnaryHandler {
- if curr == len(interceptors)-1 {
- return finalHandler
- }
- return func(ctx context.Context, req interface{}) (interface{}, error) {
- return interceptors[curr+1](ctx, req, info, getChainUnaryHandler(interceptors, curr+1, info, finalHandler))
- }
- }
- func (s *Server) processUnaryRPC(t transport.ServerTransport, stream *transport.Stream, srv *service, md *MethodDesc, trInfo *traceInfo) (err error) {
- sh := s.opts.statsHandler
- if sh != nil || trInfo != nil || channelz.IsOn() {
- if channelz.IsOn() {
- s.incrCallsStarted()
- }
- var statsBegin *stats.Begin
- if sh != nil {
- beginTime := time.Now()
- statsBegin = &stats.Begin{
- BeginTime: beginTime,
- }
- sh.HandleRPC(stream.Context(), statsBegin)
- }
- if trInfo != nil {
- trInfo.tr.LazyLog(&trInfo.firstLine, false)
- }
-
-
-
-
-
-
-
-
-
-
- defer func() {
- if trInfo != nil {
- if err != nil && err != io.EOF {
- trInfo.tr.LazyLog(&fmtStringer{"%v", []interface{}{err}}, true)
- trInfo.tr.SetError()
- }
- trInfo.tr.Finish()
- }
- if sh != nil {
- end := &stats.End{
- BeginTime: statsBegin.BeginTime,
- EndTime: time.Now(),
- }
- if err != nil && err != io.EOF {
- end.Error = toRPCErr(err)
- }
- sh.HandleRPC(stream.Context(), end)
- }
- if channelz.IsOn() {
- if err != nil && err != io.EOF {
- s.incrCallsFailed()
- } else {
- s.incrCallsSucceeded()
- }
- }
- }()
- }
- binlog := binarylog.GetMethodLogger(stream.Method())
- if binlog != nil {
- ctx := stream.Context()
- md, _ := metadata.FromIncomingContext(ctx)
- logEntry := &binarylog.ClientHeader{
- Header: md,
- MethodName: stream.Method(),
- PeerAddr: nil,
- }
- if deadline, ok := ctx.Deadline(); ok {
- logEntry.Timeout = time.Until(deadline)
- if logEntry.Timeout < 0 {
- logEntry.Timeout = 0
- }
- }
- if a := md[":authority"]; len(a) > 0 {
- logEntry.Authority = a[0]
- }
- if peer, ok := peer.FromContext(ctx); ok {
- logEntry.PeerAddr = peer.Addr
- }
- binlog.Log(logEntry)
- }
-
-
-
-
- var comp, decomp encoding.Compressor
- var cp Compressor
- var dc Decompressor
-
-
- if rc := stream.RecvCompress(); s.opts.dc != nil && s.opts.dc.Type() == rc {
- dc = s.opts.dc
- } else if rc != "" && rc != encoding.Identity {
- decomp = encoding.GetCompressor(rc)
- if decomp == nil {
- st := status.Newf(codes.Unimplemented, "grpc: Decompressor is not installed for grpc-encoding %q", rc)
- t.WriteStatus(stream, st)
- return st.Err()
- }
- }
-
-
-
-
- if s.opts.cp != nil {
- cp = s.opts.cp
- stream.SetSendCompress(cp.Type())
- } else if rc := stream.RecvCompress(); rc != "" && rc != encoding.Identity {
-
- comp = encoding.GetCompressor(rc)
- if comp != nil {
- stream.SetSendCompress(rc)
- }
- }
- var payInfo *payloadInfo
- if sh != nil || binlog != nil {
- payInfo = &payloadInfo{}
- }
- d, err := recvAndDecompress(&parser{r: stream}, stream, dc, s.opts.maxReceiveMessageSize, payInfo, decomp)
- if err != nil {
- if st, ok := status.FromError(err); ok {
- if e := t.WriteStatus(stream, st); e != nil {
- channelz.Warningf(logger, s.channelzID, "grpc: Server.processUnaryRPC failed to write status %v", e)
- }
- }
- return err
- }
- if channelz.IsOn() {
- t.IncrMsgRecv()
- }
- df := func(v interface{}) error {
- if err := s.getCodec(stream.ContentSubtype()).Unmarshal(d, v); err != nil {
- return status.Errorf(codes.Internal, "grpc: error unmarshalling request: %v", err)
- }
- if sh != nil {
- sh.HandleRPC(stream.Context(), &stats.InPayload{
- RecvTime: time.Now(),
- Payload: v,
- WireLength: payInfo.wireLength,
- Data: d,
- Length: len(d),
- })
- }
- if binlog != nil {
- binlog.Log(&binarylog.ClientMessage{
- Message: d,
- })
- }
- if trInfo != nil {
- trInfo.tr.LazyLog(&payload{sent: false, msg: v}, true)
- }
- return nil
- }
- ctx := NewContextWithServerTransportStream(stream.Context(), stream)
- reply, appErr := md.Handler(srv.server, ctx, df, s.opts.unaryInt)
- if appErr != nil {
- appStatus, ok := status.FromError(appErr)
- if !ok {
-
- appErr = status.Error(codes.Unknown, appErr.Error())
- appStatus, _ = status.FromError(appErr)
- }
- if trInfo != nil {
- trInfo.tr.LazyLog(stringer(appStatus.Message()), true)
- trInfo.tr.SetError()
- }
- if e := t.WriteStatus(stream, appStatus); e != nil {
- channelz.Warningf(logger, s.channelzID, "grpc: Server.processUnaryRPC failed to write status: %v", e)
- }
- if binlog != nil {
- if h, _ := stream.Header(); h.Len() > 0 {
-
-
- binlog.Log(&binarylog.ServerHeader{
- Header: h,
- })
- }
- binlog.Log(&binarylog.ServerTrailer{
- Trailer: stream.Trailer(),
- Err: appErr,
- })
- }
- return appErr
- }
- if trInfo != nil {
- trInfo.tr.LazyLog(stringer("OK"), false)
- }
- opts := &transport.Options{Last: true}
- if err := s.sendResponse(t, stream, reply, cp, opts, comp); err != nil {
- if err == io.EOF {
-
- return err
- }
- if sts, ok := status.FromError(err); ok {
- if e := t.WriteStatus(stream, sts); e != nil {
- channelz.Warningf(logger, s.channelzID, "grpc: Server.processUnaryRPC failed to write status: %v", e)
- }
- } else {
- switch st := err.(type) {
- case transport.ConnectionError:
-
- default:
- panic(fmt.Sprintf("grpc: Unexpected error (%T) from sendResponse: %v", st, st))
- }
- }
- if binlog != nil {
- h, _ := stream.Header()
- binlog.Log(&binarylog.ServerHeader{
- Header: h,
- })
- binlog.Log(&binarylog.ServerTrailer{
- Trailer: stream.Trailer(),
- Err: appErr,
- })
- }
- return err
- }
- if binlog != nil {
- h, _ := stream.Header()
- binlog.Log(&binarylog.ServerHeader{
- Header: h,
- })
- binlog.Log(&binarylog.ServerMessage{
- Message: reply,
- })
- }
- if channelz.IsOn() {
- t.IncrMsgSent()
- }
- if trInfo != nil {
- trInfo.tr.LazyLog(&payload{sent: true, msg: reply}, true)
- }
-
-
-
- err = t.WriteStatus(stream, statusOK)
- if binlog != nil {
- binlog.Log(&binarylog.ServerTrailer{
- Trailer: stream.Trailer(),
- Err: appErr,
- })
- }
- return err
- }
- func chainStreamServerInterceptors(s *Server) {
-
-
- interceptors := s.opts.chainStreamInts
- if s.opts.streamInt != nil {
- interceptors = append([]StreamServerInterceptor{s.opts.streamInt}, s.opts.chainStreamInts...)
- }
- var chainedInt StreamServerInterceptor
- if len(interceptors) == 0 {
- chainedInt = nil
- } else if len(interceptors) == 1 {
- chainedInt = interceptors[0]
- } else {
- chainedInt = func(srv interface{}, ss ServerStream, info *StreamServerInfo, handler StreamHandler) error {
- return interceptors[0](srv, ss, info, getChainStreamHandler(interceptors, 0, info, handler))
- }
- }
- s.opts.streamInt = chainedInt
- }
- func getChainStreamHandler(interceptors []StreamServerInterceptor, curr int, info *StreamServerInfo, finalHandler StreamHandler) StreamHandler {
- if curr == len(interceptors)-1 {
- return finalHandler
- }
- return func(srv interface{}, ss ServerStream) error {
- return interceptors[curr+1](srv, ss, info, getChainStreamHandler(interceptors, curr+1, info, finalHandler))
- }
- }
- func (s *Server) processStreamingRPC(t transport.ServerTransport, stream *transport.Stream, srv *service, sd *StreamDesc, trInfo *traceInfo) (err error) {
- if channelz.IsOn() {
- s.incrCallsStarted()
- }
- sh := s.opts.statsHandler
- var statsBegin *stats.Begin
- if sh != nil {
- beginTime := time.Now()
- statsBegin = &stats.Begin{
- BeginTime: beginTime,
- }
- sh.HandleRPC(stream.Context(), statsBegin)
- }
- ctx := NewContextWithServerTransportStream(stream.Context(), stream)
- ss := &serverStream{
- ctx: ctx,
- t: t,
- s: stream,
- p: &parser{r: stream},
- codec: s.getCodec(stream.ContentSubtype()),
- maxReceiveMessageSize: s.opts.maxReceiveMessageSize,
- maxSendMessageSize: s.opts.maxSendMessageSize,
- trInfo: trInfo,
- statsHandler: sh,
- }
- if sh != nil || trInfo != nil || channelz.IsOn() {
-
- defer func() {
- if trInfo != nil {
- ss.mu.Lock()
- if err != nil && err != io.EOF {
- ss.trInfo.tr.LazyLog(&fmtStringer{"%v", []interface{}{err}}, true)
- ss.trInfo.tr.SetError()
- }
- ss.trInfo.tr.Finish()
- ss.trInfo.tr = nil
- ss.mu.Unlock()
- }
- if sh != nil {
- end := &stats.End{
- BeginTime: statsBegin.BeginTime,
- EndTime: time.Now(),
- }
- if err != nil && err != io.EOF {
- end.Error = toRPCErr(err)
- }
- sh.HandleRPC(stream.Context(), end)
- }
- if channelz.IsOn() {
- if err != nil && err != io.EOF {
- s.incrCallsFailed()
- } else {
- s.incrCallsSucceeded()
- }
- }
- }()
- }
- ss.binlog = binarylog.GetMethodLogger(stream.Method())
- if ss.binlog != nil {
- md, _ := metadata.FromIncomingContext(ctx)
- logEntry := &binarylog.ClientHeader{
- Header: md,
- MethodName: stream.Method(),
- PeerAddr: nil,
- }
- if deadline, ok := ctx.Deadline(); ok {
- logEntry.Timeout = time.Until(deadline)
- if logEntry.Timeout < 0 {
- logEntry.Timeout = 0
- }
- }
- if a := md[":authority"]; len(a) > 0 {
- logEntry.Authority = a[0]
- }
- if peer, ok := peer.FromContext(ss.Context()); ok {
- logEntry.PeerAddr = peer.Addr
- }
- ss.binlog.Log(logEntry)
- }
-
-
- if rc := stream.RecvCompress(); s.opts.dc != nil && s.opts.dc.Type() == rc {
- ss.dc = s.opts.dc
- } else if rc != "" && rc != encoding.Identity {
- ss.decomp = encoding.GetCompressor(rc)
- if ss.decomp == nil {
- st := status.Newf(codes.Unimplemented, "grpc: Decompressor is not installed for grpc-encoding %q", rc)
- t.WriteStatus(ss.s, st)
- return st.Err()
- }
- }
-
-
-
-
- if s.opts.cp != nil {
- ss.cp = s.opts.cp
- stream.SetSendCompress(s.opts.cp.Type())
- } else if rc := stream.RecvCompress(); rc != "" && rc != encoding.Identity {
-
- ss.comp = encoding.GetCompressor(rc)
- if ss.comp != nil {
- stream.SetSendCompress(rc)
- }
- }
- if trInfo != nil {
- trInfo.tr.LazyLog(&trInfo.firstLine, false)
- }
- var appErr error
- var server interface{}
- if srv != nil {
- server = srv.server
- }
- if s.opts.streamInt == nil {
- appErr = sd.Handler(server, ss)
- } else {
- info := &StreamServerInfo{
- FullMethod: stream.Method(),
- IsClientStream: sd.ClientStreams,
- IsServerStream: sd.ServerStreams,
- }
- appErr = s.opts.streamInt(server, ss, info, sd.Handler)
- }
- if appErr != nil {
- appStatus, ok := status.FromError(appErr)
- if !ok {
- appStatus = status.New(codes.Unknown, appErr.Error())
- appErr = appStatus.Err()
- }
- if trInfo != nil {
- ss.mu.Lock()
- ss.trInfo.tr.LazyLog(stringer(appStatus.Message()), true)
- ss.trInfo.tr.SetError()
- ss.mu.Unlock()
- }
- t.WriteStatus(ss.s, appStatus)
- if ss.binlog != nil {
- ss.binlog.Log(&binarylog.ServerTrailer{
- Trailer: ss.s.Trailer(),
- Err: appErr,
- })
- }
-
- return appErr
- }
- if trInfo != nil {
- ss.mu.Lock()
- ss.trInfo.tr.LazyLog(stringer("OK"), false)
- ss.mu.Unlock()
- }
- err = t.WriteStatus(ss.s, statusOK)
- if ss.binlog != nil {
- ss.binlog.Log(&binarylog.ServerTrailer{
- Trailer: ss.s.Trailer(),
- Err: appErr,
- })
- }
- return err
- }
- func (s *Server) handleStream(t transport.ServerTransport, stream *transport.Stream, trInfo *traceInfo) {
- sm := stream.Method()
- if sm != "" && sm[0] == '/' {
- sm = sm[1:]
- }
- pos := strings.LastIndex(sm, "/")
- if pos == -1 {
- if trInfo != nil {
- trInfo.tr.LazyLog(&fmtStringer{"Malformed method name %q", []interface{}{sm}}, true)
- trInfo.tr.SetError()
- }
- errDesc := fmt.Sprintf("malformed method name: %q", stream.Method())
- if err := t.WriteStatus(stream, status.New(codes.ResourceExhausted, errDesc)); err != nil {
- if trInfo != nil {
- trInfo.tr.LazyLog(&fmtStringer{"%v", []interface{}{err}}, true)
- trInfo.tr.SetError()
- }
- channelz.Warningf(logger, s.channelzID, "grpc: Server.handleStream failed to write status: %v", err)
- }
- if trInfo != nil {
- trInfo.tr.Finish()
- }
- return
- }
- service := sm[:pos]
- method := sm[pos+1:]
- srv, knownService := s.m[service]
- if knownService {
- if md, ok := srv.md[method]; ok {
- s.processUnaryRPC(t, stream, srv, md, trInfo)
- return
- }
- if sd, ok := srv.sd[method]; ok {
- s.processStreamingRPC(t, stream, srv, sd, trInfo)
- return
- }
- }
-
- if unknownDesc := s.opts.unknownStreamDesc; unknownDesc != nil {
- s.processStreamingRPC(t, stream, nil, unknownDesc, trInfo)
- return
- }
- var errDesc string
- if !knownService {
- errDesc = fmt.Sprintf("unknown service %v", service)
- } else {
- errDesc = fmt.Sprintf("unknown method %v for service %v", method, service)
- }
- if trInfo != nil {
- trInfo.tr.LazyPrintf("%s", errDesc)
- trInfo.tr.SetError()
- }
- if err := t.WriteStatus(stream, status.New(codes.Unimplemented, errDesc)); err != nil {
- if trInfo != nil {
- trInfo.tr.LazyLog(&fmtStringer{"%v", []interface{}{err}}, true)
- trInfo.tr.SetError()
- }
- channelz.Warningf(logger, s.channelzID, "grpc: Server.handleStream failed to write status: %v", err)
- }
- if trInfo != nil {
- trInfo.tr.Finish()
- }
- }
- type streamKey struct{}
- func NewContextWithServerTransportStream(ctx context.Context, stream ServerTransportStream) context.Context {
- return context.WithValue(ctx, streamKey{}, stream)
- }
- type ServerTransportStream interface {
- Method() string
- SetHeader(md metadata.MD) error
- SendHeader(md metadata.MD) error
- SetTrailer(md metadata.MD) error
- }
- func ServerTransportStreamFromContext(ctx context.Context) ServerTransportStream {
- s, _ := ctx.Value(streamKey{}).(ServerTransportStream)
- return s
- }
- func (s *Server) Stop() {
- s.quit.Fire()
- defer func() {
- s.serveWG.Wait()
- s.done.Fire()
- }()
- s.channelzRemoveOnce.Do(func() {
- if channelz.IsOn() {
- channelz.RemoveEntry(s.channelzID)
- }
- })
- s.mu.Lock()
- listeners := s.lis
- s.lis = nil
- st := s.conns
- s.conns = nil
-
- s.cv.Broadcast()
- s.mu.Unlock()
- for lis := range listeners {
- lis.Close()
- }
- for c := range st {
- c.Close()
- }
- if s.opts.numServerWorkers > 0 {
- s.stopServerWorkers()
- }
- s.mu.Lock()
- if s.events != nil {
- s.events.Finish()
- s.events = nil
- }
- s.mu.Unlock()
- }
- func (s *Server) GracefulStop() {
- s.quit.Fire()
- defer s.done.Fire()
- s.channelzRemoveOnce.Do(func() {
- if channelz.IsOn() {
- channelz.RemoveEntry(s.channelzID)
- }
- })
- s.mu.Lock()
- if s.conns == nil {
- s.mu.Unlock()
- return
- }
- for lis := range s.lis {
- lis.Close()
- }
- s.lis = nil
- if !s.drain {
- for st := range s.conns {
- st.Drain()
- }
- s.drain = true
- }
-
-
- s.mu.Unlock()
- s.serveWG.Wait()
- s.mu.Lock()
- for len(s.conns) != 0 {
- s.cv.Wait()
- }
- s.conns = nil
- if s.events != nil {
- s.events.Finish()
- s.events = nil
- }
- s.mu.Unlock()
- }
- func (s *Server) getCodec(contentSubtype string) baseCodec {
- if s.opts.codec != nil {
- return s.opts.codec
- }
- if contentSubtype == "" {
- return encoding.GetCodec(proto.Name)
- }
- codec := encoding.GetCodec(contentSubtype)
- if codec == nil {
- return encoding.GetCodec(proto.Name)
- }
- return codec
- }
- func SetHeader(ctx context.Context, md metadata.MD) error {
- if md.Len() == 0 {
- return nil
- }
- stream := ServerTransportStreamFromContext(ctx)
- if stream == nil {
- return status.Errorf(codes.Internal, "grpc: failed to fetch the stream from the context %v", ctx)
- }
- return stream.SetHeader(md)
- }
- func SendHeader(ctx context.Context, md metadata.MD) error {
- stream := ServerTransportStreamFromContext(ctx)
- if stream == nil {
- return status.Errorf(codes.Internal, "grpc: failed to fetch the stream from the context %v", ctx)
- }
- if err := stream.SendHeader(md); err != nil {
- return toRPCErr(err)
- }
- return nil
- }
- func SetTrailer(ctx context.Context, md metadata.MD) error {
- if md.Len() == 0 {
- return nil
- }
- stream := ServerTransportStreamFromContext(ctx)
- if stream == nil {
- return status.Errorf(codes.Internal, "grpc: failed to fetch the stream from the context %v", ctx)
- }
- return stream.SetTrailer(md)
- }
- func Method(ctx context.Context) (string, bool) {
- s := ServerTransportStreamFromContext(ctx)
- if s == nil {
- return "", false
- }
- return s.Method(), true
- }
- type channelzServer struct {
- s *Server
- }
- func (c *channelzServer) ChannelzMetric() *channelz.ServerInternalMetric {
- return c.s.channelzMetric()
- }
|