Release all references

pull/109/head
v2ray 2016-03-11 23:51:58 +01:00
parent f5960db27e
commit ff69ac7a0e
9 changed files with 42 additions and 9 deletions

View File

@ -10,6 +10,8 @@ var (
ErrorAlreadyReleased = errors.New("Object already released.")
)
// Releasable interface is for those types that can release its members.
type Releasable interface {
// Release releases all references to accelerate garbage collection.
Release()
}

View File

@ -3,6 +3,7 @@ package io // import "github.com/v2ray/v2ray-core/common/io"
import (
"io"
"github.com/v2ray/v2ray-core/common"
"github.com/v2ray/v2ray-core/common/alloc"
)
@ -23,6 +24,11 @@ type Reader interface {
Read() (*alloc.Buffer, error)
}
type ReleasableReader interface {
Reader
common.Releasable
}
// AdaptiveReader is a Reader that adjusts its reading speed automatically.
type AdaptiveReader struct {
reader io.Reader
@ -57,3 +63,7 @@ func (this *AdaptiveReader) Read() (*alloc.Buffer, error) {
}
return buffer, nil
}
func (this *AdaptiveReader) Release() {
this.reader = nil
}

View File

@ -32,3 +32,7 @@ func (this *AdaptiveWriter) Write(buffer *alloc.Buffer) error {
}
return err
}
func (this *AdaptiveWriter) Release() {
this.writer = nil
}

View File

@ -50,6 +50,11 @@ func (reader *TimeOutReader) SetTimeOut(value int) {
}
}
func (reader *TimeOutReader) Release() {
reader.connection = nil
reader.worker = nil
}
type timedReaderWorker struct {
timeout int
connection net.Conn

View File

@ -57,3 +57,7 @@ func GetUserSettings(level UserLevel) UserSettings {
}
return settings
}
type Account interface {
CryptionKey() []byte
}

View File

@ -118,8 +118,11 @@ func (this *VMessInboundHandler) HandleConnection(connection *hub.TCPConn) {
defer connection.Close()
connReader := v2net.NewTimeOutReader(16, connection)
defer connReader.Release()
reader := v2io.NewBufferedReader(connReader)
defer reader.Release()
session := raw.NewServerSession(this.clients)
request, err := session.DecodeRequestHeader(reader)
@ -145,7 +148,8 @@ func (this *VMessInboundHandler) HandleConnection(connection *hub.TCPConn) {
defer close(input)
defer readFinish.Unlock()
bodyReader := session.DecodeRequestBody(reader)
var requestReader v2io.Reader
var requestReader v2io.ReleasableReader
defer requestReader.Release()
if request.Option.IsChunkStream() {
requestReader = vmessio.NewAuthChunkReader(bodyReader)
} else {
@ -155,6 +159,7 @@ func (this *VMessInboundHandler) HandleConnection(connection *hub.TCPConn) {
}()
writer := v2io.NewBufferedWriter(connection)
defer writer.Release()
response := &proto.ResponseHeader{
Command: this.generateCommand(request),

View File

@ -44,3 +44,7 @@ func (this *AuthChunkReader) Read() (*alloc.Buffer, error) {
buffer.SliceFrom(4)
return buffer, nil
}
func (this *AuthChunkReader) Release() {
this.reader = nil
}

View File

@ -23,6 +23,10 @@ func (this *AuthChunkWriter) Write(buffer *alloc.Buffer) error {
return this.writer.Write(buffer)
}
func (this *AuthChunkWriter) Release() {
this.writer = nil
}
func Authenticate(buffer *alloc.Buffer) {
fnvHash := fnv.New32a()
fnvHash.Write(buffer.Value)

View File

@ -16,7 +16,6 @@ var (
type TCPConn struct {
conn *net.TCPConn
listener *TCPHub
dirty bool
}
func (this *TCPConn) Read(b []byte) (int, error) {
@ -38,8 +37,6 @@ func (this *TCPConn) Close() error {
return ErrorClosedConnection
}
err := this.conn.Close()
this.conn = nil
this.listener = nil
return err
}
@ -48,11 +45,9 @@ func (this *TCPConn) Release() {
return
}
if this.dirty {
this.Close()
return
}
this.listener.recycle(this.conn)
this.Close()
this.conn = nil
this.listener = nil
}
func (this *TCPConn) LocalAddr() net.Addr {