2016-03-12 11:52:13 +03:00
|
|
|
package pool
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
)
|
|
|
|
|
|
|
|
type connList struct {
|
|
|
|
cns []*Conn
|
2016-03-12 13:41:02 +03:00
|
|
|
mu sync.Mutex
|
2016-03-12 11:52:13 +03:00
|
|
|
len int32 // atomic
|
|
|
|
size int32
|
|
|
|
}
|
|
|
|
|
|
|
|
func newConnList(size int) *connList {
|
|
|
|
return &connList{
|
2016-03-12 13:41:02 +03:00
|
|
|
cns: make([]*Conn, size),
|
2016-03-12 11:52:13 +03:00
|
|
|
size: int32(size),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *connList) Len() int {
|
|
|
|
return int(atomic.LoadInt32(&l.len))
|
|
|
|
}
|
|
|
|
|
2016-03-12 13:41:02 +03:00
|
|
|
// Reserve reserves place in the list and returns true on success.
|
2016-03-14 14:17:33 +03:00
|
|
|
// The caller must add connection or cancel reservation if it was reserved.
|
2016-03-12 11:52:13 +03:00
|
|
|
func (l *connList) Reserve() bool {
|
|
|
|
len := atomic.AddInt32(&l.len, 1)
|
|
|
|
reserved := len <= l.size
|
|
|
|
if !reserved {
|
|
|
|
atomic.AddInt32(&l.len, -1)
|
|
|
|
}
|
|
|
|
return reserved
|
|
|
|
}
|
|
|
|
|
2016-03-14 14:17:33 +03:00
|
|
|
func (l *connList) CancelReservation() {
|
|
|
|
atomic.AddInt32(&l.len, -1)
|
|
|
|
}
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
// Add adds connection to the list. The caller must reserve place first.
|
|
|
|
func (l *connList) Add(cn *Conn) {
|
2016-03-12 13:41:02 +03:00
|
|
|
l.mu.Lock()
|
|
|
|
for i, c := range l.cns {
|
|
|
|
if c == nil {
|
2016-03-16 17:57:24 +03:00
|
|
|
cn.SetIndex(i)
|
2016-03-12 13:41:02 +03:00
|
|
|
l.cns[i] = cn
|
|
|
|
l.mu.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
panic("not reached")
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
2016-03-14 14:17:33 +03:00
|
|
|
func (l *connList) Replace(cn *Conn) {
|
|
|
|
l.mu.Lock()
|
|
|
|
if l.cns != nil {
|
|
|
|
l.cns[cn.idx] = cn
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
2016-03-14 14:17:33 +03:00
|
|
|
l.mu.Unlock()
|
|
|
|
}
|
2016-03-12 11:52:13 +03:00
|
|
|
|
2016-03-14 14:17:33 +03:00
|
|
|
// Remove closes connection and removes it from the list.
|
|
|
|
func (l *connList) Remove(idx int) {
|
2016-03-12 13:41:02 +03:00
|
|
|
l.mu.Lock()
|
|
|
|
if l.cns != nil {
|
2016-03-14 14:17:33 +03:00
|
|
|
l.cns[idx] = nil
|
2016-03-16 17:57:24 +03:00
|
|
|
atomic.AddInt32(&l.len, -1)
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
2016-03-12 13:41:02 +03:00
|
|
|
l.mu.Unlock()
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
2016-03-16 17:57:24 +03:00
|
|
|
func (l *connList) Reset() []*Conn {
|
2016-03-12 13:41:02 +03:00
|
|
|
l.mu.Lock()
|
2016-03-16 17:57:24 +03:00
|
|
|
|
|
|
|
for _, cn := range l.cns {
|
|
|
|
if cn == nil {
|
2016-03-12 13:41:02 +03:00
|
|
|
continue
|
|
|
|
}
|
2016-03-16 17:57:24 +03:00
|
|
|
cn.SetIndex(-1)
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
2016-03-16 17:57:24 +03:00
|
|
|
|
|
|
|
cns := l.cns
|
2016-03-12 11:52:13 +03:00
|
|
|
l.cns = nil
|
2016-03-14 14:17:33 +03:00
|
|
|
l.len = 0
|
2016-03-16 17:57:24 +03:00
|
|
|
|
2016-03-12 13:41:02 +03:00
|
|
|
l.mu.Unlock()
|
2016-03-16 17:57:24 +03:00
|
|
|
return cns
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|