Вынес в отдельный пакет
This commit is contained in:
parent
5a641b6ce4
commit
40fdfee21e
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
.idea
|
50
context_shutdowner.go
Normal file
50
context_shutdowner.go
Normal file
@ -0,0 +1,50 @@
|
||||
package graceful_shutdown
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.rinsvent.ru/rinsvent/gol"
|
||||
"go.uber.org/zap"
|
||||
"os"
|
||||
"os/signal"
|
||||
"sync"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
type ContextShutdowner struct {
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
wg *sync.WaitGroup
|
||||
}
|
||||
|
||||
func (sh *ContextShutdowner) IncLock() {
|
||||
sh.wg.Add(1)
|
||||
}
|
||||
|
||||
func (sh *ContextShutdowner) DecLock() {
|
||||
sh.wg.Add(-1)
|
||||
}
|
||||
|
||||
func (sh *ContextShutdowner) GetContext() context.Context {
|
||||
return sh.ctx
|
||||
}
|
||||
|
||||
func (sh *ContextShutdowner) Wait() {
|
||||
sigChan := make(chan os.Signal, 2)
|
||||
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
||||
sig := <-sigChan
|
||||
|
||||
gol.With(zap.String("sig", sig.String())).Info("Gracefully shutting down")
|
||||
|
||||
sh.cancel()
|
||||
sh.wg.Wait()
|
||||
}
|
||||
|
||||
func NewContextShutdowner() ContextShutdowner {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
var wg sync.WaitGroup
|
||||
return ContextShutdowner{
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
wg: &wg,
|
||||
}
|
||||
}
|
25
func_shutdowner.go
Normal file
25
func_shutdowner.go
Normal file
@ -0,0 +1,25 @@
|
||||
package graceful_shutdown
|
||||
|
||||
import (
|
||||
"git.rinsvent.ru/rinsvent/gol"
|
||||
"go.uber.org/zap"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
type FuncShutdowner struct{}
|
||||
|
||||
func (sh *FuncShutdowner) Wait(f func()) {
|
||||
sigChan := make(chan os.Signal, 2)
|
||||
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
||||
sig := <-sigChan
|
||||
|
||||
gol.With(zap.String("sig", sig.String())).Info("Gracefully shutting down")
|
||||
|
||||
f()
|
||||
}
|
||||
|
||||
func NewFuncShutdowner() FuncShutdowner {
|
||||
return FuncShutdowner{}
|
||||
}
|
14
go.mod
Normal file
14
go.mod
Normal file
@ -0,0 +1,14 @@
|
||||
module git.rinsvent.ru/rinsvent/graceful-shutdown-go
|
||||
|
||||
go 1.23
|
||||
|
||||
require (
|
||||
git.rinsvent.ru/rinsvent/gol v0.0.0-20240921094550-1489c64a01e7 // indirect
|
||||
github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d // indirect
|
||||
github.com/getsentry/raven-go v0.2.0 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/tchap/zapext v1.0.0 // indirect
|
||||
go.uber.org/multierr v1.10.0 // indirect
|
||||
go.uber.org/zap v1.27.0 // indirect
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
|
||||
)
|
16
go.sum
Normal file
16
go.sum
Normal file
@ -0,0 +1,16 @@
|
||||
git.rinsvent.ru/rinsvent/gol v0.0.0-20240921094550-1489c64a01e7 h1:RASDTYghzGaJviqPiAEh56q9kN5a0p+KCnZcR6GgqLA=
|
||||
git.rinsvent.ru/rinsvent/gol v0.0.0-20240921094550-1489c64a01e7/go.mod h1:U9bes/stAbl7rZ+3rZRCq2GcmzZIUAk6xgE4l8zis9o=
|
||||
github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d h1:S2NE3iHSwP0XV47EEXL8mWmRdEfGscSJ+7EgePNgt0s=
|
||||
github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d/go.mod h1:sGbDF6GwGcLpkNXPUTkMRoywsNa/ol15pxFe6ERfguA=
|
||||
github.com/getsentry/raven-go v0.2.0 h1:no+xWJRb5ZI7eE8TWgIq1jLulQiIoLG0IfYxv5JYMGs=
|
||||
github.com/getsentry/raven-go v0.2.0/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/tchap/zapext v1.0.0 h1:qPxfRLzqYzemT+Pgs5VoH8NGU5YS7cgCnhcqRGkmrXc=
|
||||
github.com/tchap/zapext v1.0.0/go.mod h1:0VgDSQ0xHJRqkxrwu3G2i2762jSnAJMz7rYxiZGpW1U=
|
||||
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
|
||||
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
|
||||
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
|
||||
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
|
Loading…
Reference in New Issue
Block a user