mirror of https://github.com/v2ray/v2ray-core
Release all references
parent
f5960db27e
commit
ff69ac7a0e
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -32,3 +32,7 @@ func (this *AdaptiveWriter) Write(buffer *alloc.Buffer) error {
|
|||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (this *AdaptiveWriter) Release() {
|
||||
this.writer = nil
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -57,3 +57,7 @@ func GetUserSettings(level UserLevel) UserSettings {
|
|||
}
|
||||
return settings
|
||||
}
|
||||
|
||||
type Account interface {
|
||||
CryptionKey() []byte
|
||||
}
|
||||
|
|
|
@ -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),
|
||||
|
|
|
@ -44,3 +44,7 @@ func (this *AuthChunkReader) Read() (*alloc.Buffer, error) {
|
|||
buffer.SliceFrom(4)
|
||||
return buffer, nil
|
||||
}
|
||||
|
||||
func (this *AuthChunkReader) Release() {
|
||||
this.reader = nil
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue