From c68da6a0e89cd5dcf167f32838c03ccfc96c9e82 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Tue, 27 Dec 2016 21:33:34 +0100 Subject: [PATCH] unified release --- common/bufio/reader.go | 6 ++++-- common/crypto/io.go | 8 ++++---- common/log/internal/log_entry.go | 11 +++++------ common/net/timed_io.go | 6 ++++-- proxy/shadowsocks/ota.go | 9 +++++---- proxy/vmess/vmess.go | 12 +++++------- 6 files changed, 27 insertions(+), 25 deletions(-) diff --git a/common/bufio/reader.go b/common/bufio/reader.go index c9e430f0c..182323a37 100644 --- a/common/bufio/reader.go +++ b/common/bufio/reader.go @@ -25,8 +25,10 @@ func NewReader(rawReader io.Reader) *BufferedReader { // Release implements Releasable.Release(). func (v *BufferedReader) Release() { - v.buffer.Release() - v.buffer = nil + if v.buffer != nil { + v.buffer.Release() + v.buffer = nil + } common.Release(v.reader) } diff --git a/common/crypto/io.go b/common/crypto/io.go index 98ed242bf..1f7bc7420 100644 --- a/common/crypto/io.go +++ b/common/crypto/io.go @@ -31,8 +31,8 @@ func (v *CryptionReader) Read(data []byte) (int, error) { } func (v *CryptionReader) Release() { - v.reader = nil - v.stream = nil + common.Release(v.reader) + common.Release(v.stream) } type CryptionWriter struct { @@ -56,6 +56,6 @@ func (v *CryptionWriter) Write(data []byte) (int, error) { } func (v *CryptionWriter) Release() { - v.writer = nil - v.stream = nil + common.Release(v.writer) + common.Release(v.stream) } diff --git a/common/log/internal/log_entry.go b/common/log/internal/log_entry.go index c9db04088..6f46bb6c2 100644 --- a/common/log/internal/log_entry.go +++ b/common/log/internal/log_entry.go @@ -19,10 +19,9 @@ type ErrorLog struct { } func (v *ErrorLog) Release() { - for index := range v.Values { - v.Values[index] = nil + for _, val := range v.Values { + common.Release(val) } - v.Values = nil } func (v *ErrorLog) String() string { @@ -37,9 +36,9 @@ type AccessLog struct { } func (v *AccessLog) Release() { - v.From = nil - v.To = nil - v.Reason = nil + common.Release(v.From) + common.Release(v.To) + common.Release(v.Reason) } func (v *AccessLog) String() string { diff --git a/common/net/timed_io.go b/common/net/timed_io.go index a56127e17..1ceca42e0 100644 --- a/common/net/timed_io.go +++ b/common/net/timed_io.go @@ -4,6 +4,8 @@ import ( "io" "net" "time" + + "v2ray.com/core/common" ) var ( @@ -51,8 +53,8 @@ func (reader *TimeOutReader) SetTimeOut(value uint32) { } func (reader *TimeOutReader) Release() { - reader.connection = nil - reader.worker = nil + common.Release(reader.connection) + common.Release(reader.worker) } type timedReaderWorker struct { diff --git a/proxy/shadowsocks/ota.go b/proxy/shadowsocks/ota.go index 6794cf218..26c202af3 100644 --- a/proxy/shadowsocks/ota.go +++ b/proxy/shadowsocks/ota.go @@ -6,6 +6,7 @@ import ( "crypto/sha1" "io" + "v2ray.com/core/common" "v2ray.com/core/common/buf" "v2ray.com/core/common/errors" "v2ray.com/core/common/serial" @@ -70,8 +71,8 @@ func NewChunkReader(reader io.Reader, auth *Authenticator) *ChunkReader { } func (v *ChunkReader) Release() { - v.reader = nil - v.auth = nil + common.Release(v.reader) + common.Release(v.auth) } func (v *ChunkReader) Read() (*buf.Buffer, error) { @@ -124,8 +125,8 @@ func NewChunkWriter(writer io.Writer, auth *Authenticator) *ChunkWriter { } func (v *ChunkWriter) Release() { - v.writer = nil - v.auth = nil + common.Release(v.writer) + common.Release(v.auth) } func (v *ChunkWriter) Write(payload *buf.Buffer) error { diff --git a/proxy/vmess/vmess.go b/proxy/vmess/vmess.go index cfeaaa4f4..68832d83a 100644 --- a/proxy/vmess/vmess.go +++ b/proxy/vmess/vmess.go @@ -69,11 +69,6 @@ func (v *TimedUserValidator) Release() { } v.running = false - v.validUsers = nil - v.userHash = nil - v.ids = nil - v.hasher = nil - v.cancel = nil } func (v *TimedUserValidator) generateNewHashes(nowSec protocol.Timestamp, idx int, entry *idEntry) { @@ -89,10 +84,8 @@ func (v *TimedUserValidator) generateNewHashes(nowSec protocol.Timestamp, idx in idHash.Sum(hashValueRemoval[:0]) idHash.Reset() - v.Lock() v.userHash[hashValue] = &indexTimePair{idx, entry.lastSec} delete(v.userHash, hashValueRemoval) - v.Unlock() entry.lastSec++ entry.lastSecRemoval++ @@ -107,9 +100,11 @@ func (v *TimedUserValidator) updateUserHash(interval time.Duration) { select { case now := <-time.After(interval): nowSec := protocol.Timestamp(now.Unix() + cacheDurationSec) + v.Lock() for _, entry := range v.ids { v.generateNewHashes(nowSec, entry.userIdx, entry) } + v.Unlock() case <-v.cancel.WaitForCancel(): return } @@ -117,6 +112,9 @@ func (v *TimedUserValidator) updateUserHash(interval time.Duration) { } func (v *TimedUserValidator) Add(user *protocol.User) error { + v.Lock() + defer v.Unlock() + idx := len(v.validUsers) v.validUsers = append(v.validUsers, user) rawAccount, err := user.GetTypedAccount()