1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-11-23 12:02:58 -05:00

pooled session objects

This commit is contained in:
Darien Raymond
2018-09-11 20:15:15 +02:00
parent ed1d713ef4
commit a89ff38fe6
5 changed files with 38 additions and 5 deletions

View File

@@ -7,6 +7,7 @@ import (
"crypto/rand"
"hash/fnv"
"io"
"sync"
"golang.org/x/crypto/chacha20poly1305"
@@ -40,12 +41,16 @@ type ClientSession struct {
responseHeader byte
}
var clientSessionPool = sync.Pool{
New: func() interface{} { return &ClientSession{} },
}
// NewClientSession creates a new ClientSession.
func NewClientSession(idHash protocol.IDHash) *ClientSession {
randomBytes := make([]byte, 33) // 16 + 16 + 1
common.Must2(rand.Read(randomBytes))
session := &ClientSession{}
session := clientSessionPool.Get().(*ClientSession)
copy(session.requestBodyKey[:], randomBytes[:16])
copy(session.requestBodyIV[:], randomBytes[16:32])
session.responseHeader = randomBytes[32]
@@ -56,6 +61,12 @@ func NewClientSession(idHash protocol.IDHash) *ClientSession {
return session
}
func ReleaseClientSession(session *ClientSession) {
session.idHash = nil
session.responseReader = nil
clientSessionPool.Put(session)
}
func (c *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, writer io.Writer) error {
timestamp := protocol.NewTimestampGenerator(protocol.NowTime(), 30)()
account := header.User.Account.(*vmess.InternalAccount)