fix hanging on graceful shutdown

This commit is contained in:
Maxim Merzhanov committed 2021-04-21 16:47:59 +03:00
1 parent a5134181be
commit 52705f6982
3 files changed
+49 -22

No files matched your search

+2 -3
View File
@@ -1,6 +1,6 @@
module isp-config-service
go 1.13
go 1.16
require (
github.com/asaskevich/govalidator v0.0.0-20180315120708-ccb8e960c48f
@@ -13,13 +13,12 @@ require (
github.com/integration-system/isp-lib-test v1.6.3
github.com/integration-system/isp-lib/v2 v2.8.4
github.com/integration-system/isp-log v1.1.6
github.com/integration-system/net-mux v1.0.0
github.com/integration-system/net-mux v1.0.1-0.20210421105716-b82154781dac
github.com/json-iterator/go v1.1.10
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826
github.com/pkg/errors v0.9.1
github.com/satori/go.uuid v1.2.0
github.com/stretchr/testify v1.7.0
github.com/thecodeteam/goodbye v0.0.0-20170927022442-a83968bda2d3
github.com/xeipuuv/gojsonschema v1.2.0
google.golang.org/grpc v1.33.2
)
+6 -5
View File
@@ -90,7 +90,6 @@ github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/creack/pty v1.1.11 h1:07n33Z8lZxZ2qwegKbObQohDhXDQxiMMz1NOUGYlesw=
github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
@@ -172,7 +171,6 @@ github.com/go-pg/urlstruct v0.3.0 h1:ORiTb205uT7v7lvq4VUOQ4ddJ5TjFRlVRWqWsMBgek4
github.com/go-pg/urlstruct v0.3.0/go.mod h1:/XKyiUOUUS3onjF+LJxbfmSywYAdl6qMfVbX33Q8rgg=
github.com/go-pg/zerochecker v0.1.1 h1:av77Qe7Gs+1oYGGh51k0sbZ0bUaxJEdeP0r8YE64Dco=
github.com/go-pg/zerochecker v0.1.1/go.mod h1:NJZ4wKL0NmTtz0GKCoJ8kym6Xn/EQzXRl2OnAe7MmDo=
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
@@ -184,7 +182,6 @@ github.com/go-redis/redis v6.15.6+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8w
github.com/go-redis/redis/v8 v8.0.0-beta.2/go.mod h1:o1M7JtsgfDYyv3o+gBn/jJ1LkqpnCrmil7PSppZGBak=
github.com/go-redis/redis/v8 v8.4.4/go.mod h1:nA0bQuF0i5JFx4Ta9RZxGKXFrQ8cRWntra97f0196iY=
github.com/go-sourcemap/sourcemap v2.1.3+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg=
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee h1:s+21KNqlpePfkah2I+gwHF8xmJWRjooY+5248k6m4A0=
@@ -323,8 +320,8 @@ github.com/integration-system/isp-log v1.1.6/go.mod h1:TpUZ/Tx84sRipAT/YXZ8V7Z21
github.com/integration-system/jsonschema v1.0.0/go.mod h1:EoqDH7RmC8Epg5EgjboIxDo7Udv3WX2exGqtIVB4hI0=
github.com/integration-system/jsonschema v1.0.1 h1:nR78BEDG9djX2uXQ1YP66a/9yQOMmp2HFmw8VPUdAa8=
github.com/integration-system/jsonschema v1.0.1/go.mod h1:EoqDH7RmC8Epg5EgjboIxDo7Udv3WX2exGqtIVB4hI0=
github.com/integration-system/net-mux v1.0.0 h1:UY5y9cDq4ngfYaVzwqcIZ2FDw2l0H4slVDXp1E2gopc=
github.com/integration-system/net-mux v1.0.0/go.mod h1:R2PRrXFCOQRbjYIeiJFA6y2/3CfMYEX20H6yrIN6c7g=
github.com/integration-system/net-mux v1.0.1-0.20210421105716-b82154781dac h1:g0cYcgPGnkNxhsBC6x8/rQRMUNUh3MPHoG1aG1HNKBY=
github.com/integration-system/net-mux v1.0.1-0.20210421105716-b82154781dac/go.mod h1:0iEskyZB58/VwtFu8BATYMmPse2ZU9WWv6agidMEAs0=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
@@ -612,6 +609,8 @@ go.opentelemetry.io/otel v0.5.0/go.mod h1:jzBIgIzK43Iu1BpDAXwqOd6UPsSAk+ewVZ5ofS
go.opentelemetry.io/otel v0.15.0/go.mod h1:e4GKElweB8W2gWUqbghw0B8t5MCTccc9212eNHnOHwA=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/goleak v1.1.10 h1:z+mqJhf6ss6BSfSM671tgKyZBFPTTJM+HLxnhPC3wu0=
go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4=
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA=
@@ -650,6 +649,7 @@ golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTk
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/lint v0.0.0-20190409202823-959b441ac422/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/lint v0.0.0-20190909230951-414d861bb4ac/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o=
@@ -778,6 +778,7 @@ golang.org/x/tools v0.0.0-20190911174233-4f2ddba30aff/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.0.0-20191012152004-8de300cfc20a/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191108193012-7d206e10da11/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191112195655-aa38f8e97acc/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191130070609-6e064ea0cf2d/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
+41 -14
View File
@@ -6,6 +6,9 @@ import (
"net"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/integration-system/isp-etp-go/v2"
"github.com/integration-system/isp-lib/v2/backend"
@@ -16,7 +19,6 @@ import (
"github.com/integration-system/isp-lib/v2/utils"
log "github.com/integration-system/isp-log"
mux "github.com/integration-system/net-mux"
"github.com/thecodeteam/goodbye"
"isp-config-service/cluster"
"isp-config-service/codes"
"isp-config-service/conf"
@@ -40,8 +42,7 @@ const (
var (
version = "0.1.0"
shutdownChan = make(chan struct{})
muxer mux.Mux
muxer mux.Mux
)
func init() {
@@ -63,7 +64,6 @@ func init() {
//go:generate swag init --parseDependency
//go:generate rm -f docs/swagger.json
func main() {
ctx := context.Background()
cfg := config.Get().(*conf.Configuration)
handlers := helper.GetHandlers()
endpoints := backend.GetEndpoints(cfg.ModuleName, handlers)
@@ -99,18 +99,14 @@ func main() {
}
_, raftStore := initRaft(raftListener, cfg.Cluster, declaration)
initWebsocket(ctx, connectionReadLimit, httpListener, raftStore)
initWebsocket(connectionReadLimit, httpListener, raftStore)
initGrpc(cfg.WS.Grpc, raftStore)
metric.InitProfiling(cfg.ModuleName)
metric.InitCollectors(cfg.Metrics, structure.MetricConfiguration{})
metric.InitHttpServer(cfg.Metrics)
defer goodbye.Exit(ctx, -1)
goodbye.Notify(ctx)
goodbye.Register(onShutdown)
<-shutdownChan
gracefulShutdown()
}
func initMultiplexer(addressConfiguration structure.AddressConfiguration) (net.Listener, net.Listener, error) {
@@ -135,12 +131,12 @@ func initMultiplexer(addressConfiguration structure.AddressConfiguration) (net.L
return httpListener, raftListener, nil
}
func initWebsocket(ctx context.Context, connectionReadLimit int64, listener net.Listener, raftStore *store.Store) {
func initWebsocket(connectionReadLimit int64, listener net.Listener, raftStore *store.Store) {
etpConfig := etp.ServerConfig{
InsecureSkipVerify: true,
ConnectionReadLimit: connectionReadLimit,
}
etpServer := etp.NewServer(ctx, etpConfig)
etpServer := etp.NewServer(context.Background(), etpConfig)
subs.NewSocketEventHandler(etpServer, raftStore).SubscribeAll()
mux := http.NewServeMux()
@@ -204,9 +200,36 @@ func initGrpc(bindAddress structure.AddressConfiguration, raftStore *store.Store
backend.StartBackendGrpcServer(bindAddress, defaultService)
}
func onShutdown(ctx context.Context, _ os.Signal) {
defer close(shutdownChan)
func gracefulShutdown() {
const (
gracefulTimeout = 3 * time.Second
terminateTimeout = 4 * time.Second
)
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
log.Infof(0, "received signal '%s'", <-quit)
finishedCh := make(chan struct{})
go func() {
select {
case <-time.After(terminateTimeout):
log.Fatal(0, "exit timeout reached: terminating")
case <-finishedCh:
// ok
case sig := <-quit:
log.Fatalf(0, "received duplicate exit signal '%s': terminating", sig)
}
}()
ctx, cancel := context.WithTimeout(context.Background(), gracefulTimeout)
defer cancel()
onShutdown(ctx)
finishedCh <- struct{}{}
log.Info(0, "application exited normally")
}
func onShutdown(ctx context.Context) {
backend.StopGrpcServer()
holder.EtpServer.Close()
@@ -222,6 +245,10 @@ func onShutdown(ctx context.Context, _ os.Signal) {
log.Info(codes.RaftShutdownInfo, "raft shutdown success")
}
if err := model.DbClient.Close(); err != nil {
log.Warnf(0, "database shutdown err: %v", err)
}
_ = muxer.Close()
}