@@ -16,22 +16,25 @@ func init() {
1616type Limiter struct {
1717 readLimiter * rate.Limiter
1818 writeLimiter * rate.Limiter
19- readEnabled atomic.Bool // 读限速开关
20- writeEnabled atomic.Bool // 写限速开关
21- readCount atomic. Int64 // 读取计数器
22- writeCount atomic. Int64 // 写入计数器
19+ readEnabled atomic.Value // 读限速开关
20+ writeEnabled atomic.Value // 写限速开关
21+ readCount int64 // 读取计数器
22+ writeCount int64 // 写入计数器
2323}
2424
2525func NewLimiter (readRate , writeRate rate.Limit , burstSize int ) * Limiter {
26- return & Limiter {
26+ l := & Limiter {
2727 readLimiter : rate .NewLimiter (readRate , burstSize ),
2828 writeLimiter : rate .NewLimiter (writeRate , burstSize ),
2929 }
30+ l .readEnabled .Store (false )
31+ l .writeEnabled .Store (false )
32+ return l
3033}
3134
3235// GetCounts 获取读写计数
3336func (l * Limiter ) GetCounts () (readCount , writeCount int64 ) {
34- return l .readCount . Load ( ), l .writeCount . Load ( )
37+ return atomic . LoadInt64 ( & l .readCount ), atomic . LoadInt64 ( & l .writeCount )
3538}
3639
3740// SetReadRate 设置读取速率
@@ -61,10 +64,10 @@ func (l *Limiter) GetLimits() (readLimit, writeLimit rate.Limit) {
6164
6265// IsReadEnabled 检查读限速是否启用
6366func (l * Limiter ) IsReadEnabled () bool {
64- return l .readEnabled .Load ()
67+ return l .readEnabled .Load ().( bool )
6568}
6669
6770// IsWriteEnabled 检查写限速是否启用
6871func (l * Limiter ) IsWriteEnabled () bool {
69- return l .writeEnabled .Load ()
72+ return l .writeEnabled .Load ().( bool )
7073}
0 commit comments