2020-01-22 13:02:06 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2020-03-03 14:12:33 +00:00
|
|
|
"context"
|
2021-09-17 23:39:15 +00:00
|
|
|
"crypto/tls"
|
2020-11-27 13:00:45 +00:00
|
|
|
"fmt"
|
2020-01-22 13:02:06 +00:00
|
|
|
"net"
|
2021-09-17 23:39:15 +00:00
|
|
|
"net/http"
|
2020-01-22 13:02:06 +00:00
|
|
|
"os"
|
2020-11-27 13:00:45 +00:00
|
|
|
"os/signal"
|
2021-09-30 14:56:51 +00:00
|
|
|
"reflect"
|
2020-11-08 18:28:33 +00:00
|
|
|
"strings"
|
2020-01-22 13:02:06 +00:00
|
|
|
"sync"
|
2020-11-27 13:00:45 +00:00
|
|
|
"syscall"
|
2020-11-08 18:28:33 +00:00
|
|
|
"time"
|
2020-01-22 13:02:06 +00:00
|
|
|
|
2021-09-17 23:39:15 +00:00
|
|
|
"github.com/fullstorydev/grpchan/inprocgrpc"
|
2020-11-08 18:28:33 +00:00
|
|
|
"github.com/google/uuid"
|
2020-01-22 13:02:06 +00:00
|
|
|
grpcmiddleware "github.com/grpc-ecosystem/go-grpc-middleware"
|
2021-10-13 15:05:59 +00:00
|
|
|
"github.com/justinas/alice"
|
2023-07-10 14:01:40 +00:00
|
|
|
"github.com/pires/go-proxyproto"
|
2021-09-30 14:56:51 +00:00
|
|
|
"github.com/rs/cors"
|
2021-09-17 23:39:15 +00:00
|
|
|
"github.com/soheilhy/cmux"
|
2020-03-03 13:54:53 +00:00
|
|
|
"go.uber.org/multierr"
|
2023-09-16 14:21:51 +00:00
|
|
|
"golang.org/x/net/http2"
|
|
|
|
"golang.org/x/net/http2/h2c"
|
2023-07-07 23:29:18 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2020-01-22 13:02:06 +00:00
|
|
|
"google.golang.org/grpc"
|
2022-03-10 11:46:36 +00:00
|
|
|
"google.golang.org/grpc/health"
|
|
|
|
"google.golang.org/grpc/health/grpc_health_v1"
|
2021-09-30 14:56:51 +00:00
|
|
|
greflect "google.golang.org/grpc/reflection"
|
2020-11-08 18:28:33 +00:00
|
|
|
|
2024-10-17 15:15:05 +00:00
|
|
|
"go.linka.cloud/grpc-toolkit/internal/injectlogger"
|
2023-07-07 23:33:10 +00:00
|
|
|
"go.linka.cloud/grpc-toolkit/logger"
|
|
|
|
"go.linka.cloud/grpc-toolkit/registry"
|
|
|
|
"go.linka.cloud/grpc-toolkit/registry/noop"
|
2020-01-22 13:02:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Service interface {
|
2022-03-11 12:42:02 +00:00
|
|
|
greflect.GRPCServer
|
|
|
|
|
2020-01-22 13:02:06 +00:00
|
|
|
Options() Options
|
|
|
|
Start() error
|
2023-08-25 15:15:52 +00:00
|
|
|
Serve(lis net.Listener) error
|
2020-01-22 13:02:06 +00:00
|
|
|
Stop() error
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(opts ...Option) (Service, error) {
|
|
|
|
return newService(opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
type service struct {
|
2021-09-17 23:39:15 +00:00
|
|
|
opts *options
|
|
|
|
cancel context.CancelFunc
|
|
|
|
|
2020-03-03 14:12:33 +00:00
|
|
|
server *grpc.Server
|
|
|
|
mu sync.Mutex
|
2020-01-22 13:02:06 +00:00
|
|
|
running bool
|
2020-11-08 18:28:33 +00:00
|
|
|
|
2021-09-17 23:39:15 +00:00
|
|
|
// inproc Channel is used to serve grpc gateway
|
2022-03-11 12:42:02 +00:00
|
|
|
inproc *inprocgrpc.Channel
|
|
|
|
services map[string]*serviceInfo
|
2021-09-17 23:39:15 +00:00
|
|
|
|
2022-09-27 15:06:18 +00:00
|
|
|
healthServer *health.Server
|
|
|
|
|
2020-11-08 18:28:33 +00:00
|
|
|
id string
|
|
|
|
regSvc *registry.Service
|
2023-08-04 16:51:17 +00:00
|
|
|
o sync.Once
|
2020-11-08 18:28:33 +00:00
|
|
|
closed chan struct{}
|
2020-01-22 13:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newService(opts ...Option) (*service, error) {
|
|
|
|
s := &service{
|
2022-03-11 12:42:02 +00:00
|
|
|
opts: NewOptions(),
|
|
|
|
id: uuid.New().String(),
|
|
|
|
inproc: &inprocgrpc.Channel{},
|
|
|
|
services: make(map[string]*serviceInfo),
|
2020-01-22 13:02:06 +00:00
|
|
|
}
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
for _, f := range opts {
|
|
|
|
f(s.opts)
|
|
|
|
}
|
2024-10-17 15:15:05 +00:00
|
|
|
s.opts.ctx, s.cancel = context.WithCancel(s.opts.ctx)
|
2022-07-15 13:48:09 +00:00
|
|
|
|
|
|
|
md := md(s.opts)
|
|
|
|
if md != nil {
|
2024-10-17 15:15:05 +00:00
|
|
|
s.opts.unaryServerInterceptors = append([]grpc.UnaryServerInterceptor{
|
|
|
|
md.UnaryServerInterceptor(),
|
|
|
|
injectlogger.New(s.opts.ctx).UnaryServerInterceptor()},
|
|
|
|
s.opts.unaryServerInterceptors...,
|
|
|
|
)
|
|
|
|
s.opts.unaryClientInterceptors = append([]grpc.UnaryClientInterceptor{
|
|
|
|
md.UnaryClientInterceptor(),
|
|
|
|
injectlogger.New(s.opts.ctx).UnaryClientInterceptor()},
|
|
|
|
s.opts.unaryClientInterceptors...,
|
|
|
|
)
|
|
|
|
s.opts.streamServerInterceptors = append([]grpc.StreamServerInterceptor{
|
|
|
|
md.StreamServerInterceptor(),
|
|
|
|
injectlogger.New(s.opts.ctx).StreamServerInterceptor()},
|
|
|
|
s.opts.streamServerInterceptors...,
|
|
|
|
)
|
|
|
|
s.opts.streamClientInterceptors = append([]grpc.StreamClientInterceptor{
|
|
|
|
md.StreamClientInterceptor(),
|
|
|
|
injectlogger.New(s.opts.ctx).StreamClientInterceptor()},
|
|
|
|
s.opts.streamClientInterceptors...,
|
|
|
|
)
|
2022-03-10 11:46:36 +00:00
|
|
|
}
|
2022-07-15 13:48:09 +00:00
|
|
|
|
2020-03-03 13:54:53 +00:00
|
|
|
if s.opts.error != nil {
|
|
|
|
return nil, s.opts.error
|
|
|
|
}
|
2020-01-22 13:02:06 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-s.opts.ctx.Done():
|
|
|
|
s.Stop()
|
2023-08-04 16:51:17 +00:00
|
|
|
return
|
2020-01-22 13:02:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2020-11-08 18:28:33 +00:00
|
|
|
if s.opts.registry == nil {
|
|
|
|
s.opts.registry = noop.New()
|
|
|
|
}
|
2021-09-17 23:39:15 +00:00
|
|
|
|
2020-01-22 13:02:06 +00:00
|
|
|
if err := s.opts.parseTLSConfig(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-10 11:46:36 +00:00
|
|
|
|
2021-11-21 13:58:49 +00:00
|
|
|
ui := grpcmiddleware.ChainUnaryServer(s.opts.unaryServerInterceptors...)
|
2021-09-17 23:39:15 +00:00
|
|
|
s.inproc = s.inproc.WithServerUnaryInterceptor(ui)
|
|
|
|
|
2022-03-10 11:46:36 +00:00
|
|
|
si := grpcmiddleware.ChainStreamServer(s.opts.streamServerInterceptors...)
|
2021-09-17 23:39:15 +00:00
|
|
|
s.inproc = s.inproc.WithServerStreamInterceptor(si)
|
|
|
|
|
2020-08-07 08:59:27 +00:00
|
|
|
gopts := []grpc.ServerOption{
|
2021-09-17 23:39:15 +00:00
|
|
|
grpc.StreamInterceptor(si),
|
|
|
|
grpc.UnaryInterceptor(ui),
|
2020-08-07 08:59:27 +00:00
|
|
|
}
|
2020-01-22 13:02:06 +00:00
|
|
|
s.server = grpc.NewServer(append(gopts, s.opts.serverOpts...)...)
|
2020-08-07 08:59:27 +00:00
|
|
|
if s.opts.reflection {
|
2021-09-30 14:56:51 +00:00
|
|
|
greflect.Register(s.server)
|
2020-08-07 08:59:27 +00:00
|
|
|
}
|
2022-03-10 11:46:36 +00:00
|
|
|
if s.opts.health {
|
2022-09-27 15:06:18 +00:00
|
|
|
s.healthServer = health.NewServer()
|
|
|
|
s.registerService(&grpc_health_v1.Health_ServiceDesc, s.healthServer)
|
2022-03-10 11:46:36 +00:00
|
|
|
}
|
2021-09-17 23:39:15 +00:00
|
|
|
if err := s.gateway(s.opts.gatewayOpts...); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-06-03 11:12:19 +00:00
|
|
|
if err := s.reactApp(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-17 23:39:15 +00:00
|
|
|
// we do not configure grpc web here as the grpc handlers are not yet registered
|
2020-01-22 13:02:06 +00:00
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *service) Options() Options {
|
|
|
|
return s.opts
|
|
|
|
}
|
|
|
|
|
2023-08-04 16:51:17 +00:00
|
|
|
func (s *service) start() (*errgroup.Group, error) {
|
2021-09-17 23:39:15 +00:00
|
|
|
s.mu.Lock()
|
2023-08-04 16:51:17 +00:00
|
|
|
defer s.mu.Unlock()
|
2021-09-17 23:39:15 +00:00
|
|
|
s.closed = make(chan struct{})
|
2020-11-08 18:28:33 +00:00
|
|
|
|
2021-09-17 23:39:15 +00:00
|
|
|
// configure grpc web now that we are ready to go
|
|
|
|
if err := s.grpcWeb(s.opts.grpcWebOpts...); err != nil {
|
2023-08-04 16:51:17 +00:00
|
|
|
return nil, err
|
2020-11-08 18:28:33 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 10:33:01 +00:00
|
|
|
network := "tcp"
|
|
|
|
if strings.HasPrefix(s.opts.address, "unix://") {
|
|
|
|
network = "unix"
|
|
|
|
s.opts.address = strings.TrimPrefix(s.opts.address, "unix://")
|
|
|
|
}
|
|
|
|
|
2022-09-05 13:32:33 +00:00
|
|
|
if s.opts.lis == nil {
|
|
|
|
lis, err := net.Listen(network, s.opts.address)
|
|
|
|
if err != nil {
|
2023-08-04 16:51:17 +00:00
|
|
|
return nil, err
|
2022-09-05 13:32:33 +00:00
|
|
|
}
|
|
|
|
if s.opts.tlsConfig != nil {
|
|
|
|
lis = tls.NewListener(lis, s.opts.tlsConfig)
|
|
|
|
}
|
|
|
|
s.opts.lis = lis
|
|
|
|
s.opts.address = lis.Addr().String()
|
|
|
|
} else {
|
|
|
|
s.opts.address = s.opts.lis.Addr().String()
|
2020-11-08 18:28:33 +00:00
|
|
|
}
|
|
|
|
|
2023-07-10 14:01:40 +00:00
|
|
|
if s.opts.proxyProtocol {
|
|
|
|
p := func(upstream net.Addr) (proxyproto.Policy, error) {
|
|
|
|
u, _, err := net.SplitHostPort(upstream.String())
|
|
|
|
if err != nil {
|
|
|
|
return proxyproto.REJECT, err
|
|
|
|
}
|
|
|
|
ip := net.ParseIP(u)
|
|
|
|
if ip == nil {
|
|
|
|
return proxyproto.REJECT, fmt.Errorf("proxyproto: invalid IP address")
|
|
|
|
}
|
|
|
|
if ip.IsPrivate() || ip.IsLoopback() {
|
|
|
|
return proxyproto.USE, nil
|
|
|
|
}
|
|
|
|
return proxyproto.REJECT, nil
|
|
|
|
}
|
|
|
|
if len(s.opts.proxyProtocolAddrs) > 0 {
|
|
|
|
var err error
|
|
|
|
p, err = proxyproto.StrictWhiteListPolicy(s.opts.proxyProtocolAddrs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.opts.lis = &proxyproto.Listener{
|
|
|
|
Listener: s.opts.lis,
|
|
|
|
Policy: p,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 13:02:06 +00:00
|
|
|
for i := range s.opts.beforeStart {
|
|
|
|
if err := s.opts.beforeStart[i](); err != nil {
|
2023-08-04 16:51:17 +00:00
|
|
|
return nil, err
|
2020-01-22 13:02:06 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-08 18:28:33 +00:00
|
|
|
|
|
|
|
if err := s.register(); err != nil {
|
2023-08-04 16:51:17 +00:00
|
|
|
return nil, err
|
2020-11-08 18:28:33 +00:00
|
|
|
}
|
|
|
|
s.running = true
|
2020-11-27 13:00:45 +00:00
|
|
|
|
2021-09-30 14:56:51 +00:00
|
|
|
if reflect.DeepEqual(s.opts.cors, cors.Options{}) {
|
|
|
|
s.opts.cors = cors.Options{
|
2021-11-21 13:58:49 +00:00
|
|
|
AllowedHeaders: []string{"*"},
|
|
|
|
AllowedMethods: []string{
|
2021-09-30 14:56:51 +00:00
|
|
|
http.MethodGet,
|
|
|
|
http.MethodPost,
|
|
|
|
http.MethodPut,
|
|
|
|
http.MethodPatch,
|
|
|
|
http.MethodDelete,
|
|
|
|
http.MethodOptions,
|
|
|
|
http.MethodHead,
|
|
|
|
},
|
|
|
|
AllowedOrigins: []string{"*"},
|
|
|
|
AllowCredentials: true,
|
|
|
|
}
|
|
|
|
}
|
2021-09-17 23:39:15 +00:00
|
|
|
|
2023-09-16 14:21:51 +00:00
|
|
|
fn := s.runWithCmux
|
|
|
|
if s.opts.withoutCmux {
|
|
|
|
fn = s.runWithoutCmux
|
2023-07-07 23:29:18 +00:00
|
|
|
}
|
|
|
|
|
2023-09-16 14:21:51 +00:00
|
|
|
g, ctx := errgroup.WithContext(s.opts.ctx)
|
|
|
|
if err := fn(ctx, g); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-07-07 23:29:18 +00:00
|
|
|
|
2022-09-27 15:06:18 +00:00
|
|
|
if s.healthServer != nil {
|
|
|
|
for k := range s.services {
|
|
|
|
s.healthServer.SetServingStatus(k, grpc_health_v1.HealthCheckResponse_SERVING)
|
|
|
|
}
|
|
|
|
defer func() {
|
2023-09-16 14:21:51 +00:00
|
|
|
for k := range s.services {
|
|
|
|
s.healthServer.SetServingStatus(k, grpc_health_v1.HealthCheckResponse_NOT_SERVING)
|
2022-09-27 15:06:18 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2020-01-22 13:02:06 +00:00
|
|
|
for i := range s.opts.afterStart {
|
|
|
|
if err := s.opts.afterStart[i](); err != nil {
|
2023-08-04 16:51:17 +00:00
|
|
|
return nil, err
|
2020-01-22 13:02:06 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-04 16:51:17 +00:00
|
|
|
return g, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *service) run() error {
|
|
|
|
g, err := s.start()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-27 13:00:45 +00:00
|
|
|
sigs := s.notify()
|
2023-07-07 23:29:18 +00:00
|
|
|
|
|
|
|
errs := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
errs <- g.Wait()
|
|
|
|
}()
|
2020-11-27 13:00:45 +00:00
|
|
|
select {
|
|
|
|
case sig := <-sigs:
|
|
|
|
fmt.Println()
|
2022-07-02 10:39:52 +00:00
|
|
|
logger.C(s.opts.ctx).Warnf("received %v", sig)
|
2020-11-27 13:00:45 +00:00
|
|
|
return s.Close()
|
|
|
|
case err := <-errs:
|
2023-07-07 23:29:18 +00:00
|
|
|
if !isMuxError(err) {
|
2022-07-02 10:39:52 +00:00
|
|
|
logger.C(s.opts.ctx).Error(err)
|
2020-11-27 13:00:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2020-11-08 18:28:33 +00:00
|
|
|
}
|
2020-01-22 13:02:06 +00:00
|
|
|
}
|
|
|
|
|
2023-09-16 14:21:51 +00:00
|
|
|
func (s *service) runWithoutCmux(ctx context.Context, g *errgroup.Group) error {
|
|
|
|
if s.opts.mux != nil {
|
|
|
|
handler := alice.New(s.opts.middlewares...).Then(cors.New(s.opts.cors).Handler(s.opts.mux))
|
|
|
|
hServer := &http.Server{
|
|
|
|
Handler: h2c.NewHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.ProtoMajor == 2 && r.Header.Get("Content-Type") == "application/grpc" {
|
|
|
|
s.server.ServeHTTP(w, r)
|
|
|
|
} else {
|
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
}), &http2.Server{}),
|
|
|
|
}
|
|
|
|
if err := http2.ConfigureServer(hServer, &http2.Server{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
g.Go(func() error {
|
|
|
|
defer hServer.Shutdown(ctx)
|
|
|
|
return hServer.Serve(s.opts.lis)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
g.Go(func() error {
|
|
|
|
return s.server.Serve(s.opts.lis)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *service) runWithCmux(ctx context.Context, g *errgroup.Group) error {
|
|
|
|
mux := cmux.New(s.opts.lis)
|
|
|
|
mux.SetReadTimeout(5 * time.Second)
|
|
|
|
|
|
|
|
gLis := mux.MatchWithWriters(cmux.HTTP2MatchHeaderFieldSendSettings("content-type", "application/grpc"))
|
|
|
|
hList := mux.Match(cmux.Any())
|
|
|
|
|
|
|
|
if s.opts.mux != nil {
|
|
|
|
hServer := &http.Server{
|
2024-12-21 13:44:56 +00:00
|
|
|
Handler: alice.New(s.opts.middlewares...).Then(cors.New(s.opts.cors).Handler(s.opts.mux)),
|
2023-09-16 14:21:51 +00:00
|
|
|
}
|
|
|
|
g.Go(func() error {
|
|
|
|
defer hServer.Shutdown(ctx)
|
|
|
|
return hServer.Serve(hList)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
g.Go(func() error {
|
|
|
|
return s.server.Serve(gLis)
|
|
|
|
})
|
|
|
|
|
|
|
|
g.Go(func() error {
|
|
|
|
return ignoreMuxError(mux.Serve())
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-25 15:15:52 +00:00
|
|
|
func (s *service) Serve(lis net.Listener) error {
|
|
|
|
s.mu.Lock()
|
|
|
|
s.opts.lis = lis
|
|
|
|
s.mu.Unlock()
|
|
|
|
return s.run()
|
|
|
|
}
|
|
|
|
|
2020-01-22 13:02:06 +00:00
|
|
|
func (s *service) Start() error {
|
2022-03-10 11:46:36 +00:00
|
|
|
return s.run()
|
2020-01-22 13:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *service) Stop() error {
|
2023-08-04 16:51:17 +00:00
|
|
|
var err error
|
|
|
|
s.o.Do(func() {
|
|
|
|
err = s.stop()
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
func (s *service) stop() error {
|
2022-07-02 10:39:52 +00:00
|
|
|
log := logger.C(s.opts.ctx)
|
2020-01-22 13:02:06 +00:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2020-08-07 08:59:27 +00:00
|
|
|
if !s.running {
|
2020-01-22 13:02:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for i := range s.opts.beforeStop {
|
|
|
|
if err := s.opts.beforeStop[i](); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-11-15 14:51:29 +00:00
|
|
|
if err := s.opts.registry.Deregister(s.regSvc); err != nil {
|
2022-07-02 10:39:52 +00:00
|
|
|
log.Errorf("failed to deregister service: %v", err)
|
2020-11-15 14:51:29 +00:00
|
|
|
}
|
|
|
|
defer close(s.closed)
|
2023-08-04 16:51:17 +00:00
|
|
|
t := time.NewTimer(5 * time.Second)
|
|
|
|
defer t.Stop()
|
2020-11-27 13:00:45 +00:00
|
|
|
sigs := s.notify()
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
2022-05-15 12:12:38 +00:00
|
|
|
defer close(done)
|
|
|
|
// TODO(adphi): find a better solution
|
|
|
|
defer func() {
|
|
|
|
// catch: Drain() is not implemented
|
|
|
|
recover()
|
|
|
|
}()
|
2022-07-02 10:39:52 +00:00
|
|
|
log.Warn("shutting down gracefully")
|
2020-11-27 13:00:45 +00:00
|
|
|
s.server.GracefulStop()
|
|
|
|
}()
|
|
|
|
select {
|
2023-08-04 16:51:17 +00:00
|
|
|
case <-t.C:
|
|
|
|
log.Warnf("timeout waiting for server to stop")
|
|
|
|
s.server.Stop()
|
2020-11-27 13:00:45 +00:00
|
|
|
case sig := <-sigs:
|
|
|
|
fmt.Println()
|
2022-07-02 10:39:52 +00:00
|
|
|
log.Warnf("received %v", sig)
|
|
|
|
log.Warn("forcing shutdown")
|
2020-11-27 13:00:45 +00:00
|
|
|
s.server.Stop()
|
|
|
|
case <-done:
|
|
|
|
}
|
2020-01-22 13:02:06 +00:00
|
|
|
s.running = false
|
2020-11-15 14:51:29 +00:00
|
|
|
s.cancel()
|
2020-01-22 13:02:06 +00:00
|
|
|
for i := range s.opts.afterStop {
|
|
|
|
if err := s.opts.afterStop[i](); err != nil {
|
2020-11-08 18:28:33 +00:00
|
|
|
return err
|
2020-01-22 13:02:06 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-02 10:39:52 +00:00
|
|
|
log.Info("server stopped")
|
2020-01-22 13:02:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-27 13:00:45 +00:00
|
|
|
func (s *service) RegisterService(desc *grpc.ServiceDesc, impl interface{}) {
|
2022-03-11 12:42:02 +00:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
s.registerService(desc, impl)
|
|
|
|
}
|
|
|
|
|
|
|
|
// serviceInfo wraps information about a service. It is very similar to
|
|
|
|
// ServiceDesc and is constructed from it for internal purposes.
|
|
|
|
type serviceInfo struct {
|
|
|
|
// Contains the implementation for the methods in this service.
|
|
|
|
serviceImpl interface{}
|
|
|
|
methods map[string]*grpc.MethodDesc
|
|
|
|
streams map[string]*grpc.StreamDesc
|
|
|
|
mdata interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *service) registerService(sd *grpc.ServiceDesc, ss interface{}) {
|
|
|
|
s.server.RegisterService(sd, ss)
|
|
|
|
s.inproc.RegisterService(sd, ss)
|
|
|
|
|
|
|
|
if _, ok := s.services[sd.ServiceName]; ok {
|
2022-07-02 10:39:52 +00:00
|
|
|
logger.C(s.opts.ctx).Fatalf("grpc: Service.RegisterService found duplicate service registration for %q", sd.ServiceName)
|
2022-03-11 12:42:02 +00:00
|
|
|
}
|
|
|
|
info := &serviceInfo{
|
|
|
|
serviceImpl: ss,
|
|
|
|
methods: make(map[string]*grpc.MethodDesc),
|
|
|
|
streams: make(map[string]*grpc.StreamDesc),
|
|
|
|
mdata: sd.Metadata,
|
|
|
|
}
|
|
|
|
for i := range sd.Methods {
|
|
|
|
d := &sd.Methods[i]
|
|
|
|
info.methods[d.MethodName] = d
|
|
|
|
}
|
|
|
|
for i := range sd.Streams {
|
|
|
|
d := &sd.Streams[i]
|
|
|
|
info.streams[d.StreamName] = d
|
|
|
|
}
|
|
|
|
s.services[sd.ServiceName] = info
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *service) GetServiceInfo() map[string]grpc.ServiceInfo {
|
|
|
|
ret := make(map[string]grpc.ServiceInfo)
|
|
|
|
for n, srv := range s.services {
|
|
|
|
methods := make([]grpc.MethodInfo, 0, len(srv.methods)+len(srv.streams))
|
|
|
|
for m := range srv.methods {
|
|
|
|
methods = append(methods, grpc.MethodInfo{
|
|
|
|
Name: m,
|
|
|
|
IsClientStream: false,
|
|
|
|
IsServerStream: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for m, d := range srv.streams {
|
|
|
|
methods = append(methods, grpc.MethodInfo{
|
|
|
|
Name: m,
|
|
|
|
IsClientStream: d.ClientStreams,
|
|
|
|
IsServerStream: d.ServerStreams,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
ret[n] = grpc.ServiceInfo{
|
|
|
|
Methods: methods,
|
|
|
|
Metadata: srv.mdata,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
2020-11-27 13:00:45 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 13:02:06 +00:00
|
|
|
func (s *service) Close() error {
|
2020-03-03 13:54:53 +00:00
|
|
|
err := multierr.Combine(s.Stop())
|
2020-11-08 18:28:33 +00:00
|
|
|
<-s.closed
|
2020-03-03 13:54:53 +00:00
|
|
|
return err
|
2020-01-22 13:02:06 +00:00
|
|
|
}
|
2020-11-27 13:00:45 +00:00
|
|
|
|
|
|
|
func (s *service) notify() <-chan os.Signal {
|
|
|
|
sigs := make(chan os.Signal, 2)
|
2023-04-19 12:25:44 +00:00
|
|
|
signal.Notify(sigs, os.Interrupt, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT)
|
2020-11-27 13:00:45 +00:00
|
|
|
return sigs
|
|
|
|
}
|
2021-09-17 23:39:15 +00:00
|
|
|
|
2023-07-07 23:29:18 +00:00
|
|
|
func (s *service) lazyMux() ServeMux {
|
|
|
|
if s.opts.mux == nil {
|
|
|
|
s.opts.mux = http.NewServeMux()
|
|
|
|
}
|
|
|
|
return s.opts.mux
|
|
|
|
}
|
|
|
|
|
|
|
|
func ignoreMuxError(err error) error {
|
|
|
|
if !isMuxError(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isMuxError(err error) bool {
|
2021-09-17 23:39:15 +00:00
|
|
|
if err == nil {
|
2023-07-07 23:29:18 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if strings.Contains(err.Error(), "use of closed network connection") ||
|
|
|
|
strings.Contains(err.Error(), "mux: server closed") {
|
2021-09-17 23:39:15 +00:00
|
|
|
return true
|
|
|
|
}
|
2023-07-07 23:29:18 +00:00
|
|
|
return false
|
2021-09-17 23:39:15 +00:00
|
|
|
}
|