From ae98963ecc168f099d03e7c379736c2d567b6ace Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Thu, 21 Dec 2023 14:43:55 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20timer.Pool=20=E6=96=B0=E5=A2=9E=20Relea?= =?UTF-8?q?se=20=E5=87=BD=E6=95=B0=EF=BC=8C=E5=8F=AF=E4=B8=BB=E5=8A=A8?= =?UTF-8?q?=E9=87=8A=E6=94=BE=E6=B1=A0=E4=B8=AD=E7=9A=84=E6=89=80=E6=9C=89?= =?UTF-8?q?=E5=AE=9A=E6=97=B6=E5=99=A8=E5=8F=8A=E6=B1=A0=E5=AD=90=E6=9C=AC?= =?UTF-8?q?=E8=BA=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/timer/pool.go | 15 +++++++++++++++ utils/timer/ticker.go | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/utils/timer/pool.go b/utils/timer/pool.go index 390d3fa..dc987ac 100644 --- a/utils/timer/pool.go +++ b/utils/timer/pool.go @@ -22,6 +22,7 @@ type Pool struct { tickers []*Ticker lock sync.Mutex tickerPoolSize int + closed bool } // ChangePoolSize 改变定时器池大小 @@ -59,3 +60,17 @@ func (slf *Pool) GetTicker(size int, options ...Option) *Ticker { ticker.wheel.Start() return ticker } + +// Release 释放定时器池的资源,释放后由其产生的 Ticker 在 Ticker.Release 后将不再回到池中,而是直接释放 +// - 虽然定时器池已被释放,但是依旧可以产出 Ticker +func (slf *Pool) Release() { + slf.lock.Lock() + defer slf.lock.Unlock() + slf.closed = true + for _, ticker := range slf.tickers { + ticker.wheel.Stop() + } + slf.tickers = nil + slf.tickerPoolSize = 0 + return +} diff --git a/utils/timer/ticker.go b/utils/timer/ticker.go index 4e05840..337419c 100644 --- a/utils/timer/ticker.go +++ b/utils/timer/ticker.go @@ -39,7 +39,7 @@ func (slf *Ticker) Release() { } slf.lock.Unlock() - if len(slf.timer.tickers) < tickerPoolSize { + if len(slf.timer.tickers) < tickerPoolSize && !slf.timer.closed { slf.timer.tickers = append(slf.timer.tickers, slf) } else { slf.wheel.Stop()