From 7be240ffe5b29f5c3e0862bf8eb839e636f14bfb Mon Sep 17 00:00:00 2001 From: Rinsvent Date: Sat, 21 Sep 2024 21:05:34 +0700 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=8B=D0=BD=D0=B5=D1=81=20thread=20?= =?UTF-8?q?=D0=B2=20=D0=BE=D1=82=D0=B4=D0=B5=D0=BB=D1=8C=D0=BD=D1=8B=D0=B9?= =?UTF-8?q?=20=D0=BF=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 + go.mod | 14 ++++++++++++++ go.sum | 16 ++++++++++++++++ thread.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+) create mode 100644 .gitignore create mode 100644 go.mod create mode 100644 go.sum create mode 100644 thread.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..471c7c3 --- /dev/null +++ b/go.mod @@ -0,0 +1,14 @@ +module git.rinsvent.ru/rinsvent/thread + +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= diff --git a/thread.go b/thread.go new file mode 100644 index 0000000..8064df7 --- /dev/null +++ b/thread.go @@ -0,0 +1,51 @@ +package thread + +import ( + "context" + "git.rinsvent.ru/rinsvent/gol" + "sync/atomic" +) + +type Manager struct { + Count int32 + runningCount atomic.Int32 + stop bool + stopped chan bool +} + +func (m *Manager) Start(f func()) { + m.stopped = make(chan bool, 1) + for { + if m.runningCount.Load() >= m.Count { + continue + } + + if m.stop { + if m.runningCount.Load() <= 1 { + gol.Debug("thread manager stopped") + m.stopped <- true + break + } + gol.Debug("thread manager stopping") + continue + } + + if m.runningCount.Add(1) > m.Count { + gol.Debug("thread manager atomic skip") + continue + } + + go func() { + defer m.runningCount.Add(-1) + f() + }() + } +} + +func (m *Manager) Wait(ctx context.Context) { + <-ctx.Done() + m.stop = true + gol.Debug("thread manager wait stopped") + <-m.stopped + gol.Debug("thread manager wait break") +}