style: 修改 timer.Timer 名字为 timer.Pool

This commit is contained in:
kercylan98 2023-12-21 14:37:17 +08:00
parent 1ae1c8d65c
commit 50181c7ecb
3 changed files with 70 additions and 65 deletions

61
utils/timer/pool.go Normal file
View File

@ -0,0 +1,61 @@
package timer
import (
"fmt"
"sync"
"github.com/RussellLuo/timingwheel"
)
// NewPool 创建一个定时器池,当 tickerPoolSize 小于等于 0 时,将会引发 panic可指定为 DefaultTickerPoolSize
func NewPool(tickerPoolSize int) *Pool {
if tickerPoolSize <= 0 {
panic(fmt.Errorf("timer tickerPoolSize must greater than 0, got: %d", tickerPoolSize))
}
return &Pool{
tickerPoolSize: tickerPoolSize,
}
}
// Pool 定时器池
type Pool struct {
tickers []*Ticker
lock sync.Mutex
tickerPoolSize int
}
// ChangePoolSize 改变定时器池大小
// - 当传入的大小小于或等于 0 时,将会返回错误,并且不会发生任何改变
func (slf *Pool) ChangePoolSize(size int) error {
if size <= 0 {
return fmt.Errorf("timer tickerPoolSize must greater than 0, got: %d", tickerPoolSize)
}
slf.lock.Lock()
defer slf.lock.Unlock()
slf.tickerPoolSize = size
return nil
}
// GetTicker 获取一个新的定时器
func (slf *Pool) GetTicker(size int, options ...Option) *Ticker {
slf.lock.Lock()
defer slf.lock.Unlock()
var ticker *Ticker
if len(slf.tickers) > 0 {
ticker = slf.tickers[0]
slf.tickers = slf.tickers[1:]
return ticker
}
ticker = &Ticker{
timer: slf,
wheel: timingwheel.NewTimingWheel(timingWheelTick, int64(size)),
timers: make(map[string]*Scheduler),
}
for _, option := range options {
option(ticker)
}
ticker.wheel.Start()
return ticker
}

View File

@ -11,10 +11,11 @@ import (
// Ticker 定时器
type Ticker struct {
timer *Timer
timer *Pool
wheel *timingwheel.TimingWheel
timers map[string]*Scheduler
lock sync.RWMutex
handle func(name string, caller func())
mark string
}
@ -25,7 +26,7 @@ func (slf *Ticker) Mark() string {
return slf.mark
}
// Release 释放定时器,并将定时器重新放回 Timer 池中
// Release 释放定时器,并将定时器重新放回 Pool 池中
func (slf *Ticker) Release() {
slf.timer.lock.Lock()
defer slf.timer.lock.Unlock()

View File

@ -1,74 +1,17 @@
package timer
import (
"fmt"
"sync"
"github.com/RussellLuo/timingwheel"
)
var (
tickerPoolSize = DefaultTickerPoolSize
standardTimer = NewTimer(tickerPoolSize)
standardPool = NewPool(tickerPoolSize)
)
// SetTickerPoolSize 设置定时器池大小
// SetPoolSize 设置标准池定时器池大小
// - 默认值为 DefaultTickerPoolSize当定时器池中的定时器不足时会自动创建新的定时器当定时器释放时会将多余的定时器进行释放否则将放入定时器池中
func SetTickerPoolSize(size int) {
_ = standardTimer.ChangeTickerPoolSize(size)
func SetPoolSize(size int) {
_ = standardPool.ChangePoolSize(size)
}
// GetTicker 获取标准池中的一个定时器
func GetTicker(size int, options ...Option) *Ticker {
return standardTimer.NewTicker(size, options...)
}
func NewTimer(tickerPoolSize int) *Timer {
if tickerPoolSize <= 0 {
panic(fmt.Errorf("timer tickerPoolSize must greater than 0, got: %d", tickerPoolSize))
}
return &Timer{
tickerPoolSize: tickerPoolSize,
}
}
type Timer struct {
tickers []*Ticker
lock sync.Mutex
tickerPoolSize int
}
// ChangeTickerPoolSize 改变定时器池大小
// - 当传入的大小小于或等于 0 时,将会返回错误,并且不会发生任何改变
func (slf *Timer) ChangeTickerPoolSize(size int) error {
if size <= 0 {
return fmt.Errorf("timer tickerPoolSize must greater than 0, got: %d", tickerPoolSize)
}
slf.lock.Lock()
defer slf.lock.Unlock()
slf.tickerPoolSize = size
return nil
}
// NewTicker 获取一个新的定时器
func (slf *Timer) NewTicker(size int, options ...Option) *Ticker {
slf.lock.Lock()
defer slf.lock.Unlock()
var ticker *Ticker
if len(slf.tickers) > 0 {
ticker = slf.tickers[0]
slf.tickers = slf.tickers[1:]
return ticker
}
ticker = &Ticker{
timer: slf,
wheel: timingwheel.NewTimingWheel(timingWheelTick, int64(size)),
timers: make(map[string]*Scheduler),
}
for _, option := range options {
option(ticker)
}
ticker.wheel.Start()
return ticker
return standardPool.GetTicker(size, options...)
}