mirror of
				https://github.com/xiaoqidun/limit.git
				synced 2025-11-04 17:34:21 +08:00 
			
		
		
		
	Compare commits
	
		
			2 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| c3e04c1cc6 | |||
| cb0ec0059c | 
							
								
								
									
										4
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								go.mod
									
									
									
									
									
								
							@@ -1,5 +1,5 @@
 | 
				
			|||||||
module github.com/xiaoqidun/limit
 | 
					module github.com/xiaoqidun/limit
 | 
				
			||||||
 | 
					
 | 
				
			||||||
go 1.25.1
 | 
					go 1.25.3
 | 
				
			||||||
 | 
					
 | 
				
			||||||
require golang.org/x/time v0.13.0
 | 
					require golang.org/x/time v0.14.0
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										4
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								go.sum
									
									
									
									
									
								
							@@ -1,2 +1,2 @@
 | 
				
			|||||||
golang.org/x/time v0.13.0 h1:eUlYslOIt32DgYD6utsuUeHs4d7AsEYLuIAdg7FlYgI=
 | 
					golang.org/x/time v0.14.0 h1:MRx4UaLrDotUKUdCIqzPC48t1Y9hANFKIRpNx+Te8PI=
 | 
				
			||||||
golang.org/x/time v0.13.0/go.mod h1:eL/Oa2bBBK0TkX57Fyni+NgnyQQN4LitPmob2Hjnqw4=
 | 
					golang.org/x/time v0.14.0/go.mod h1:eL/Oa2bBBK0TkX57Fyni+NgnyQQN4LitPmob2Hjnqw4=
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										8
									
								
								limit.go
									
									
									
									
									
								
							
							
						
						
									
										8
									
								
								limit.go
									
									
									
									
									
								
							@@ -69,17 +69,14 @@ func NewWithConfig(config Config) *Limiter {
 | 
				
			|||||||
	if config.Expiration == 0 {
 | 
						if config.Expiration == 0 {
 | 
				
			||||||
		config.Expiration = 30 * time.Minute
 | 
							config.Expiration = 30 * time.Minute
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					 | 
				
			||||||
	// 确保分片数量是2的幂,以便进行高效的位运算
 | 
						// 确保分片数量是2的幂,以便进行高效的位运算
 | 
				
			||||||
	if config.ShardCount <= 0 || (config.ShardCount&(config.ShardCount-1)) != 0 {
 | 
						if config.ShardCount <= 0 || (config.ShardCount&(config.ShardCount-1)) != 0 {
 | 
				
			||||||
		config.ShardCount = defaultShardCount
 | 
							config.ShardCount = defaultShardCount
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					 | 
				
			||||||
	l := &Limiter{
 | 
						l := &Limiter{
 | 
				
			||||||
		shards: make([]*shard, config.ShardCount),
 | 
							shards: make([]*shard, config.ShardCount),
 | 
				
			||||||
		config: config,
 | 
							config: config,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					 | 
				
			||||||
	// 初始化所有分片
 | 
						// 初始化所有分片
 | 
				
			||||||
	for i := 0; i < config.ShardCount; i++ {
 | 
						for i := 0; i < config.ShardCount; i++ {
 | 
				
			||||||
		l.shards[i] = newShard(config.GCInterval, config.Expiration)
 | 
							l.shards[i] = newShard(config.GCInterval, config.Expiration)
 | 
				
			||||||
@@ -166,7 +163,6 @@ func (s *shard) gc(interval, expiration time.Duration) {
 | 
				
			|||||||
			return
 | 
								return
 | 
				
			||||||
		default:
 | 
							default:
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					 | 
				
			||||||
		select {
 | 
							select {
 | 
				
			||||||
		case <-ticker.C:
 | 
							case <-ticker.C:
 | 
				
			||||||
			s.mutex.Lock()
 | 
								s.mutex.Lock()
 | 
				
			||||||
@@ -239,20 +235,16 @@ func (s *shard) stop() {
 | 
				
			|||||||
	s.stopOnce.Do(func() {
 | 
						s.stopOnce.Do(func() {
 | 
				
			||||||
		close(s.stopCh)
 | 
							close(s.stopCh)
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
 | 
					 | 
				
			||||||
	// 等待 gc goroutine 完全退出
 | 
						// 等待 gc goroutine 完全退出
 | 
				
			||||||
	s.waitGroup.Wait()
 | 
						s.waitGroup.Wait()
 | 
				
			||||||
 | 
					 | 
				
			||||||
	// 锁定并进行最终的资源清理
 | 
						// 锁定并进行最终的资源清理
 | 
				
			||||||
	// 因为 gc 已经退出,所以此时只有 Get/Del 会竞争锁
 | 
						// 因为 gc 已经退出,所以此时只有 Get/Del 会竞争锁
 | 
				
			||||||
	s.mutex.Lock()
 | 
						s.mutex.Lock()
 | 
				
			||||||
	defer s.mutex.Unlock()
 | 
						defer s.mutex.Unlock()
 | 
				
			||||||
 | 
					 | 
				
			||||||
	// 检查是否已被清理,防止重复操作
 | 
						// 检查是否已被清理,防止重复操作
 | 
				
			||||||
	if s.limiter == nil {
 | 
						if s.limiter == nil {
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					 | 
				
			||||||
	// 将所有 session 对象放回对象池
 | 
						// 将所有 session 对象放回对象池
 | 
				
			||||||
	for _, sess := range s.limiter {
 | 
						for _, sess := range s.limiter {
 | 
				
			||||||
		sess.limiter = nil
 | 
							sess.limiter = nil
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user