当前仓库属于关闭状态,部分功能使用受限,详情请查阅 仓库状态说明
1 Star 1 Fork 0

仓库镜像/go-redis-redis
关闭

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
conn_list.go 1.41 KB
一键复制 编辑 原始数据 按行查看 历史
Vladimir Mihailenco 提交于 2016-03-12 12:41 . Optimize pool.Remove.
package pool
import (
"sync"
"sync/atomic"
)
type connList struct {
cns []*Conn
mu sync.Mutex
len int32 // atomic
size int32
}
func newConnList(size int) *connList {
return &connList{
cns: make([]*Conn, size),
size: int32(size),
}
}
func (l *connList) Len() int {
return int(atomic.LoadInt32(&l.len))
}
// Reserve reserves place in the list and returns true on success.
// The caller must add or remove connection if place was reserved.
func (l *connList) Reserve() bool {
len := atomic.AddInt32(&l.len, 1)
reserved := len <= l.size
if !reserved {
atomic.AddInt32(&l.len, -1)
}
return reserved
}
// Add adds connection to the list. The caller must reserve place first.
func (l *connList) Add(cn *Conn) {
l.mu.Lock()
for i, c := range l.cns {
if c == nil {
cn.idx = i
l.cns[i] = cn
l.mu.Unlock()
return
}
}
panic("not reached")
}
// Remove closes connection and removes it from the list.
func (l *connList) Remove(cn *Conn) error {
atomic.AddInt32(&l.len, -1)
if cn == nil { // free reserved place
return nil
}
l.mu.Lock()
if l.cns != nil {
l.cns[cn.idx] = nil
cn.idx = -1
}
l.mu.Unlock()
return nil
}
func (l *connList) Close() error {
var retErr error
l.mu.Lock()
for _, c := range l.cns {
if c == nil {
continue
}
if err := c.Close(); err != nil && retErr == nil {
retErr = err
}
}
l.cns = nil
atomic.StoreInt32(&l.len, 0)
l.mu.Unlock()
return retErr
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/github-image/go-redis-redis.git
git@gitee.com:github-image/go-redis-redis.git
github-image
go-redis-redis
go-redis-redis
v3.5.0

搜索帮助

0d507c66 1850385 C8b1a773 1850385