From 90200fbecb45e098209ff7c1f45d03ea0ecde8bf Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Mon, 13 Feb 2017 23:12:27 +0100 Subject: [PATCH] remove signal.Once --- common/signal/once.go | 29 ----------------------------- transport/internet/internal/pool.go | 23 ++++++++++++++--------- 2 files changed, 14 insertions(+), 38 deletions(-) delete mode 100644 common/signal/once.go diff --git a/common/signal/once.go b/common/signal/once.go deleted file mode 100644 index db30b009..00000000 --- a/common/signal/once.go +++ /dev/null @@ -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) -} diff --git a/transport/internet/internal/pool.go b/transport/internet/internal/pool.go index fe8b8371..efa87724 100644 --- a/transport/internet/internal/pool.go +++ b/transport/internet/internal/pool.go @@ -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: + } }