mirror of https://github.com/cbeuw/Cloak
Use a sync.Pool to remove the global random bottleneck in picking a random conn
This commit is contained in:
parent
415523f10a
commit
2d08e88efb
|
|
@ -7,6 +7,7 @@ import (
|
|||
"net"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
|
|
@ -31,6 +32,7 @@ type switchboard struct {
|
|||
conns sync.Map
|
||||
numConns uint32
|
||||
nextConnId uint32
|
||||
randPool sync.Pool
|
||||
|
||||
broken uint32
|
||||
}
|
||||
|
|
@ -48,6 +50,9 @@ func makeSwitchboard(sesh *Session) *switchboard {
|
|||
strategy: strategy,
|
||||
valve: sesh.Valve,
|
||||
nextConnId: 1,
|
||||
randPool: sync.Pool{New: func() interface{} {
|
||||
return rand.New(rand.NewSource(int64(time.Now().Nanosecond())))
|
||||
}},
|
||||
}
|
||||
return sb
|
||||
}
|
||||
|
|
@ -121,7 +126,9 @@ func (sb *switchboard) pickRandConn() (uint32, net.Conn, error) {
|
|||
// so if the r > len(sb.conns) at the point of range call, the last visited element is picked
|
||||
var id uint32
|
||||
var conn net.Conn
|
||||
r := rand.Intn(connCount)
|
||||
randReader := sb.randPool.Get().(*rand.Rand)
|
||||
r := randReader.Intn(connCount)
|
||||
sb.randPool.Put(randReader)
|
||||
var c int
|
||||
sb.conns.Range(func(connIdI, connI interface{}) bool {
|
||||
if r == c {
|
||||
|
|
|
|||
Loading…
Reference in New Issue