parent
c356df3e51
commit
7be240ffe5
@ -0,0 +1 @@ |
||||
.idea |
@ -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 |
||||
) |
@ -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= |
@ -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") |
||||
} |
Loading…
Reference in new issue