2016-06-14 16:54:08 -04:00
|
|
|
package tcp
|
2016-05-30 18:21:41 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"sync"
|
2016-06-01 19:49:25 -04:00
|
|
|
"sync/atomic"
|
2016-05-30 18:21:41 -04:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2016-06-01 19:49:25 -04:00
|
|
|
type Once struct {
|
|
|
|
m sync.Mutex
|
|
|
|
done uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Once) Do(f func()) {
|
|
|
|
if atomic.LoadUint32(&o.done) == 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
o.m.Lock()
|
|
|
|
defer o.m.Unlock()
|
|
|
|
if o.done == 0 {
|
2016-06-03 18:38:22 -04:00
|
|
|
atomic.StoreUint32(&o.done, 1)
|
2016-06-01 19:49:25 -04:00
|
|
|
f()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Once) Reset() {
|
|
|
|
o.m.Lock()
|
|
|
|
defer o.m.Unlock()
|
|
|
|
atomic.StoreUint32(&o.done, 0)
|
|
|
|
}
|
|
|
|
|
2016-05-30 18:21:41 -04:00
|
|
|
type AwaitingConnection struct {
|
|
|
|
conn net.Conn
|
|
|
|
expire time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *AwaitingConnection) Expired() bool {
|
|
|
|
return this.expire.Before(time.Now())
|
|
|
|
}
|
|
|
|
|
|
|
|
type ConnectionCache struct {
|
|
|
|
sync.Mutex
|
2016-06-01 19:49:25 -04:00
|
|
|
cache map[string][]*AwaitingConnection
|
|
|
|
cleanupOnce Once
|
2016-05-30 18:21:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewConnectionCache() *ConnectionCache {
|
2016-06-01 19:49:25 -04:00
|
|
|
return &ConnectionCache{
|
2016-05-30 18:21:41 -04:00
|
|
|
cache: make(map[string][]*AwaitingConnection),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ConnectionCache) Cleanup() {
|
2016-06-01 19:49:25 -04:00
|
|
|
defer this.cleanupOnce.Reset()
|
|
|
|
|
|
|
|
for len(this.cache) > 0 {
|
2016-05-30 18:21:41 -04:00
|
|
|
time.Sleep(time.Second * 4)
|
|
|
|
this.Lock()
|
|
|
|
for key, value := range this.cache {
|
|
|
|
size := len(value)
|
|
|
|
changed := false
|
|
|
|
for i := 0; i < size; {
|
|
|
|
if value[i].Expired() {
|
|
|
|
value[i].conn.Close()
|
|
|
|
value[i] = value[size-1]
|
|
|
|
size--
|
|
|
|
changed = true
|
|
|
|
} else {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if changed {
|
|
|
|
for i := size; i < len(value); i++ {
|
|
|
|
value[i] = nil
|
|
|
|
}
|
|
|
|
value = value[:size]
|
|
|
|
this.cache[key] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ConnectionCache) Recycle(dest string, conn net.Conn) {
|
|
|
|
this.Lock()
|
|
|
|
defer this.Unlock()
|
|
|
|
|
|
|
|
aconn := &AwaitingConnection{
|
|
|
|
conn: conn,
|
|
|
|
expire: time.Now().Add(time.Second * 4),
|
|
|
|
}
|
|
|
|
|
|
|
|
var list []*AwaitingConnection
|
|
|
|
if v, found := this.cache[dest]; found {
|
|
|
|
v = append(v, aconn)
|
|
|
|
list = v
|
|
|
|
} else {
|
|
|
|
list = []*AwaitingConnection{aconn}
|
|
|
|
}
|
|
|
|
this.cache[dest] = list
|
2016-06-01 19:49:25 -04:00
|
|
|
|
|
|
|
go this.cleanupOnce.Do(this.Cleanup)
|
2016-05-30 18:21:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func FindFirstValid(list []*AwaitingConnection) int {
|
|
|
|
for idx, conn := range list {
|
|
|
|
if !conn.Expired() {
|
|
|
|
return idx
|
|
|
|
}
|
2016-06-01 20:40:46 -04:00
|
|
|
go conn.conn.Close()
|
2016-05-30 18:21:41 -04:00
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ConnectionCache) Get(dest string) net.Conn {
|
|
|
|
this.Lock()
|
|
|
|
defer this.Unlock()
|
|
|
|
|
|
|
|
list, found := this.cache[dest]
|
|
|
|
if !found {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
firstValid := FindFirstValid(list)
|
|
|
|
if firstValid == -1 {
|
|
|
|
delete(this.cache, dest)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
res := list[firstValid].conn
|
|
|
|
list = list[firstValid+1:]
|
|
|
|
this.cache[dest] = list
|
|
|
|
return res
|
|
|
|
}
|