remove signal.Once

pull/432/head
Darien Raymond 8 years ago
parent 6d446f57f7
commit 90200fbecb
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169

@ -1,29 +0,0 @@
package signal
import (
"sync"
"sync/atomic"
)
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 {
atomic.StoreUint32(&o.done, 1)
f()
}
}
func (o *Once) Reset() {
o.m.Lock()
defer o.m.Unlock()
atomic.StoreUint32(&o.done, 0)
}

@ -4,8 +4,6 @@ import (
"net"
"sync"
"time"
"v2ray.com/core/common/signal"
)
// ConnectionRecyler is the interface for recycling connections.
@ -32,15 +30,18 @@ func (ec *ExpiringConnection) Expired() bool {
// Pool is a connection pool.
type Pool struct {
sync.Mutex
connsByDest map[ConnectionID][]*ExpiringConnection
cleanupOnce signal.Once
connsByDest map[ConnectionID][]*ExpiringConnection
cleanupToken chan bool
}
// NewConnectionPool creates a new Pool.
func NewConnectionPool() *Pool {
return &Pool{
connsByDest: make(map[ConnectionID][]*ExpiringConnection),
p := &Pool{
connsByDest: make(map[ConnectionID][]*ExpiringConnection),
cleanupToken: make(chan bool, 1),
}
p.cleanupToken <- true
return p
}
// Get returns a connection with matching connection ID. Nil if not found.
@ -73,7 +74,9 @@ func (p *Pool) Get(id ConnectionID) net.Conn {
}
func (p *Pool) cleanup() {
defer p.cleanupOnce.Reset()
defer func() {
p.cleanupToken <- true
}()
for len(p.connsByDest) > 0 {
time.Sleep(time.Second * 5)
@ -117,7 +120,9 @@ func (p *Pool) Put(id ConnectionID, conn net.Conn) {
}
p.connsByDest[id] = list
p.cleanupOnce.Do(func() {
select {
case <-p.cleanupToken:
go p.cleanup()
})
default:
}
}

Loading…
Cancel
Save