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"
|
|
|
|
"github.com/jinzhu/gorm"
|
2021-10-13 15:05:59 +00:00
|
|
|
"github.com/justinas/alice"
|
2021-09-30 14:56:51 +00:00
|
|
|
"github.com/rs/cors"
|
2020-11-08 18:28:33 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-09-17 23:39:15 +00:00
|
|
|
"github.com/soheilhy/cmux"
|
2020-01-22 13:02:06 +00:00
|
|
|
"github.com/spf13/cobra"
|
2020-03-03 13:54:53 +00:00
|
|
|
"go.uber.org/multierr"
|
2020-01-22 13:02:06 +00:00
|
|
|
"google.golang.org/grpc"
|
2021-09-30 14:56:51 +00:00
|
|
|
greflect "google.golang.org/grpc/reflection"
|
2020-11-08 18:28:33 +00:00
|
|
|
|
2021-07-17 20:40:06 +00:00
|
|
|
"go.linka.cloud/grpc/registry"
|
|
|
|
"go.linka.cloud/grpc/registry/noop"
|
2020-01-22 13:02:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Service interface {
|
|
|
|
Options() Options
|
|
|
|
DB() *gorm.DB
|
|
|
|
Start() error
|
|
|
|
Stop() error
|
|
|
|
Close() error
|
|
|
|
Cmd() *cobra.Command
|
2020-11-27 13:00:45 +00:00
|
|
|
|
|
|
|
RegisterService(desc *grpc.ServiceDesc, impl interface{})
|
2020-01-22 13:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(opts ...Option) (Service, error) {
|
|
|
|
return newService(opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
type service struct {
|
2021-09-17 23:39:15 +00:00
|
|
|
cmd *cobra.Command
|
|
|
|
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
|
|
|
|
inproc *inprocgrpc.Channel
|
|
|
|
|
2020-11-08 18:28:33 +00:00
|
|
|
id string
|
|
|
|
regSvc *registry.Service
|
|
|
|
closed chan struct{}
|
2020-01-22 13:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newService(opts ...Option) (*service, error) {
|
2020-11-27 13:00:45 +00:00
|
|
|
if err := cmd.ParseFlags(os.Args); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-22 13:02:06 +00:00
|
|
|
s := &service{
|
2021-09-17 23:39:15 +00:00
|
|
|
opts: parseFlags(NewOptions()),
|
|
|
|
cmd: cmd,
|
|
|
|
id: uuid.New().String(),
|
|
|
|
inproc: &inprocgrpc.Channel{},
|
2020-01-22 13:02:06 +00:00
|
|
|
}
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
for _, f := range opts {
|
|
|
|
f(s.opts)
|
|
|
|
}
|
2021-10-13 15:05:59 +00:00
|
|
|
if s.opts.mux == nil {
|
|
|
|
s.opts.mux = http.NewServeMux()
|
|
|
|
}
|
2020-03-03 13:54:53 +00:00
|
|
|
if s.opts.error != nil {
|
|
|
|
return nil, s.opts.error
|
|
|
|
}
|
2020-03-03 14:12:33 +00:00
|
|
|
s.opts.ctx, s.cancel = context.WithCancel(s.opts.ctx)
|
2020-01-22 13:02:06 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-s.opts.ctx.Done():
|
|
|
|
s.Stop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
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
|
|
|
|
}
|
|
|
|
cmd.Use = s.opts.name
|
|
|
|
cmd.RunE = func(cmd *cobra.Command, args []string) error {
|
|
|
|
if cmd.Use == "" {
|
|
|
|
cmd.Use = os.Args[0]
|
|
|
|
}
|
|
|
|
return s.run()
|
|
|
|
}
|
2021-09-17 23:39:15 +00:00
|
|
|
ui := grpcmiddleware.ChainUnaryServer(s.opts.serverInterceptors...)
|
|
|
|
s.inproc = s.inproc.WithServerUnaryInterceptor(ui)
|
|
|
|
|
|
|
|
si := grpcmiddleware.ChainStreamServer(/*TODO(adphi): add to options*/)
|
|
|
|
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
|
|
|
}
|
2021-09-17 23:39:15 +00:00
|
|
|
if err := s.gateway(s.opts.gatewayOpts...); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *service) DB() *gorm.DB {
|
|
|
|
return s.opts.db
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *service) Cmd() *cobra.Command {
|
|
|
|
return s.cmd
|
|
|
|
}
|
|
|
|
|
2021-09-17 23:39:15 +00:00
|
|
|
func (s *service) run() error {
|
|
|
|
s.mu.Lock()
|
|
|
|
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 {
|
|
|
|
return err
|
2020-11-08 18:28:33 +00:00
|
|
|
}
|
|
|
|
|
2021-09-17 23:39:15 +00:00
|
|
|
lis, err := net.Listen("tcp", s.opts.address)
|
2020-11-08 18:28:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-09-17 23:39:15 +00:00
|
|
|
if s.opts.tlsConfig != nil {
|
|
|
|
lis = tls.NewListener(lis, s.opts.tlsConfig)
|
2020-11-08 18:28:33 +00:00
|
|
|
}
|
|
|
|
|
2021-09-17 23:39:15 +00:00
|
|
|
s.opts.address = lis.Addr().String()
|
2020-11-08 18:28:33 +00:00
|
|
|
|
2021-09-17 23:39:15 +00:00
|
|
|
mux := cmux.New(lis)
|
|
|
|
mux.SetReadTimeout(5 * time.Second)
|
2020-11-08 18:28:33 +00:00
|
|
|
|
2021-09-17 23:39:15 +00:00
|
|
|
gLis := mux.MatchWithWriters(cmux.HTTP2MatchHeaderFieldSendSettings("content-type", "application/grpc"))
|
|
|
|
hList := mux.Match(cmux.Any())
|
2020-11-08 18:28:33 +00:00
|
|
|
|
2020-01-22 13:02:06 +00:00
|
|
|
for i := range s.opts.beforeStart {
|
|
|
|
if err := s.opts.beforeStart[i](); err != nil {
|
2020-11-08 18:28:33 +00:00
|
|
|
s.mu.Unlock()
|
2020-01-22 13:02:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-11-08 18:28:33 +00:00
|
|
|
|
|
|
|
if err := s.register(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.running = true
|
2020-11-27 13:00:45 +00:00
|
|
|
|
2021-09-17 23:39:15 +00:00
|
|
|
errs := make(chan error, 3)
|
|
|
|
|
2021-09-30 14:56:51 +00:00
|
|
|
if reflect.DeepEqual(s.opts.cors, cors.Options{}) {
|
|
|
|
s.opts.cors = cors.Options{
|
|
|
|
AllowedHeaders: []string{"*"},
|
|
|
|
AllowedMethods: []string{
|
|
|
|
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
|
|
|
hServer := &http.Server{
|
2021-10-13 15:05:59 +00:00
|
|
|
Handler: alice.New(s.opts.middlewares...).Then(cors.New(s.opts.cors).Handler(s.opts.mux)),
|
2021-09-17 23:39:15 +00:00
|
|
|
}
|
|
|
|
if s.opts.Gateway() || s.opts.grpcWeb {
|
|
|
|
go func() {
|
|
|
|
errs <- hServer.Serve(hList)
|
|
|
|
hServer.Shutdown(s.opts.ctx)
|
|
|
|
}()
|
|
|
|
}
|
2020-01-22 13:02:06 +00:00
|
|
|
go func() {
|
2021-09-17 23:39:15 +00:00
|
|
|
errs <- s.server.Serve(gLis)
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := mux.Serve(); err != nil {
|
|
|
|
// TODO(adphi): find more elegant solution
|
|
|
|
if ignoreMuxError(err) {
|
|
|
|
errs <- nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
errs <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
errs <- nil
|
2020-01-22 13:02:06 +00:00
|
|
|
}()
|
|
|
|
for i := range s.opts.afterStart {
|
|
|
|
if err := s.opts.afterStart[i](); err != nil {
|
|
|
|
s.mu.Unlock()
|
|
|
|
s.Stop()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.mu.Unlock()
|
2020-11-27 13:00:45 +00:00
|
|
|
sigs := s.notify()
|
|
|
|
select {
|
|
|
|
case sig := <-sigs:
|
|
|
|
fmt.Println()
|
|
|
|
logrus.Warnf("received %v", sig)
|
|
|
|
return s.Close()
|
|
|
|
case err := <-errs:
|
2021-09-30 10:00:22 +00:00
|
|
|
if err != nil && !ignoreMuxError(err){
|
2020-11-27 13:00:45 +00:00
|
|
|
logrus.Error(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2020-11-08 18:28:33 +00:00
|
|
|
}
|
2020-01-22 13:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *service) Start() error {
|
|
|
|
return s.cmd.Execute()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *service) Stop() error {
|
|
|
|
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 {
|
|
|
|
logrus.Errorf("failed to deregister service: %v", err)
|
|
|
|
}
|
|
|
|
defer close(s.closed)
|
2020-11-27 13:00:45 +00:00
|
|
|
sigs := s.notify()
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
logrus.Warn("shutting down gracefully")
|
|
|
|
s.server.GracefulStop()
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case sig := <-sigs:
|
|
|
|
fmt.Println()
|
|
|
|
logrus.Warnf("received %v", sig)
|
|
|
|
logrus.Warn("forcing shutdown")
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2020-11-08 18:28:33 +00:00
|
|
|
logrus.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{}) {
|
|
|
|
s.server.RegisterService(desc, impl)
|
2021-09-17 23:39:15 +00:00
|
|
|
s.inproc.RegisterService(desc, impl)
|
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())
|
|
|
|
if s.opts.db != nil {
|
|
|
|
err = multierr.Append(s.opts.db.Close(), err)
|
|
|
|
}
|
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)
|
|
|
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGQUIT)
|
|
|
|
return sigs
|
|
|
|
}
|
2021-09-17 23:39:15 +00:00
|
|
|
|
|
|
|
func ignoreMuxError(err error) bool {
|
|
|
|
if err == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return strings.Contains(err.Error(), "use of closed network connection") ||
|
|
|
|
strings.Contains(err.Error(), "mux: server closed")
|
|
|
|
}
|