From cb2658f2bf7562a6f8b2c1604992394d5907c741 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Fri, 14 Sep 2018 16:51:46 +0200 Subject: [PATCH] Revert "pooled session objects" This reverts commit a89ff38fe61c89b8151da9d400729fade729361e. --- proxy/vmess/encoding/client.go | 13 +------------ proxy/vmess/encoding/encoding_test.go | 7 ------- proxy/vmess/encoding/server.go | 19 ++++--------------- proxy/vmess/inbound/inbound.go | 2 -- proxy/vmess/outbound/outbound.go | 2 -- 5 files changed, 5 insertions(+), 38 deletions(-) diff --git a/proxy/vmess/encoding/client.go b/proxy/vmess/encoding/client.go index 4ae7dac57..f9a58a2ce 100644 --- a/proxy/vmess/encoding/client.go +++ b/proxy/vmess/encoding/client.go @@ -5,7 +5,6 @@ import ( "crypto/rand" "hash/fnv" "io" - "sync" "golang.org/x/crypto/chacha20poly1305" @@ -39,16 +38,12 @@ 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 := clientSessionPool.Get().(*ClientSession) + session := &ClientSession{} copy(session.requestBodyKey[:], randomBytes[:16]) copy(session.requestBodyIV[:], randomBytes[16:32]) session.responseHeader = randomBytes[32] @@ -59,12 +54,6 @@ 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) diff --git a/proxy/vmess/encoding/encoding_test.go b/proxy/vmess/encoding/encoding_test.go index 2aa83ce21..b7cc10495 100644 --- a/proxy/vmess/encoding/encoding_test.go +++ b/proxy/vmess/encoding/encoding_test.go @@ -44,8 +44,6 @@ func TestRequestSerialization(t *testing.T) { buffer := buf.New() client := NewClientSession(protocol.DefaultIDHash) - defer ReleaseClientSession(client) - common.Must(client.EncodeRequestHeader(expectedRequest, buffer)) buffer2 := buf.New() @@ -59,7 +57,6 @@ func TestRequestSerialization(t *testing.T) { defer common.Close(userValidator) server := NewServerSession(userValidator, sessionHistory) - defer ReleaseServerSession(server) actualRequest, err := server.DecodeRequestHeader(buffer) assert(err, IsNil) @@ -100,8 +97,6 @@ func TestInvalidRequest(t *testing.T) { buffer := buf.New() client := NewClientSession(protocol.DefaultIDHash) - defer ReleaseClientSession(client) - common.Must(client.EncodeRequestHeader(expectedRequest, buffer)) buffer2 := buf.New() @@ -115,7 +110,6 @@ func TestInvalidRequest(t *testing.T) { defer common.Close(userValidator) server := NewServerSession(userValidator, sessionHistory) - defer ReleaseServerSession(server) _, err := server.DecodeRequestHeader(buffer) assert(err, IsNotNil) } @@ -156,7 +150,6 @@ func TestMuxRequest(t *testing.T) { defer common.Close(userValidator) server := NewServerSession(userValidator, sessionHistory) - defer ReleaseServerSession(server) actualRequest, err := server.DecodeRequestHeader(buffer) assert(err, IsNil) diff --git a/proxy/vmess/encoding/server.go b/proxy/vmess/encoding/server.go index 55442b7ca..66665adc6 100644 --- a/proxy/vmess/encoding/server.go +++ b/proxy/vmess/encoding/server.go @@ -99,24 +99,13 @@ type ServerSession struct { responseHeader byte } -var serverSessionPool = sync.Pool{ - New: func() interface{} { return &ServerSession{} }, -} - // NewServerSession creates a new ServerSession, using the given UserValidator. // The ServerSession instance doesn't take ownership of the validator. func NewServerSession(validator *vmess.TimedUserValidator, sessionHistory *SessionHistory) *ServerSession { - session := serverSessionPool.Get().(*ServerSession) - session.userValidator = validator - session.sessionHistory = sessionHistory - return session -} - -func ReleaseServerSession(session *ServerSession) { - session.responseWriter = nil - session.userValidator = nil - session.sessionHistory = nil - serverSessionPool.Put(session) + return &ServerSession{ + userValidator: validator, + sessionHistory: sessionHistory, + } } func parseSecurityType(b byte) protocol.SecurityType { diff --git a/proxy/vmess/inbound/inbound.go b/proxy/vmess/inbound/inbound.go index 88b6288d1..dd0f40a22 100644 --- a/proxy/vmess/inbound/inbound.go +++ b/proxy/vmess/inbound/inbound.go @@ -221,8 +221,6 @@ func (h *Handler) Process(ctx context.Context, network net.Network, connection i reader := &buf.BufferedReader{Reader: buf.NewReader(connection)} svrSession := encoding.NewServerSession(h.clients, h.sessionHistory) - defer encoding.ReleaseServerSession(svrSession) - request, err := svrSession.DecodeRequestHeader(reader) if err != nil { diff --git a/proxy/vmess/outbound/outbound.go b/proxy/vmess/outbound/outbound.go index 690c00b99..bfce2c287 100644 --- a/proxy/vmess/outbound/outbound.go +++ b/proxy/vmess/outbound/outbound.go @@ -106,8 +106,6 @@ func (v *Handler) Process(ctx context.Context, link *core.Link, dialer proxy.Dia output := link.Writer session := encoding.NewClientSession(protocol.DefaultIDHash) - defer encoding.ReleaseClientSession(session) - sessionPolicy := v.v.PolicyManager().ForLevel(request.User.Level) ctx, cancel := context.WithCancel(ctx)