From 40fdfee21e3cf480593a310cb6c1bc4f1655218d Mon Sep 17 00:00:00 2001 From: Rinsvent Date: Sat, 21 Sep 2024 20:37:42 +0700 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=8B=D0=BD=D0=B5=D1=81=20=D0=B2=20?= =?UTF-8?q?=D0=BE=D1=82=D0=B4=D0=B5=D0=BB=D1=8C=D0=BD=D1=8B=D0=B9=20=D0=BF?= =?UTF-8?q?=D0=B0=D0=BA=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + context_shutdowner.go | 50 +++++++++++++++++++++++++++++++++++++++++++ func_shutdowner.go | 25 ++++++++++++++++++++++ go.mod | 14 ++++++++++++ go.sum | 16 ++++++++++++++ 5 files changed, 106 insertions(+) create mode 100644 .gitignore create mode 100644 context_shutdowner.go create mode 100644 func_shutdowner.go create mode 100644 go.mod create mode 100644 go.sum diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/context_shutdowner.go b/context_shutdowner.go new file mode 100644 index 0000000..ae8fa69 --- /dev/null +++ b/context_shutdowner.go @@ -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, + } +} diff --git a/func_shutdowner.go b/func_shutdowner.go new file mode 100644 index 0000000..6949bae --- /dev/null +++ b/func_shutdowner.go @@ -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{} +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..00f6ce7 --- /dev/null +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..3e2eadd --- /dev/null +++ b/go.sum @@ -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=