1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-02 15:36:41 -05:00

releasable writer

This commit is contained in:
v2ray 2016-03-24 23:36:18 +08:00
parent 760247a4ab
commit e8023f0d92
4 changed files with 14 additions and 5 deletions

View File

@ -3,6 +3,7 @@ package io
import ( import (
"io" "io"
"github.com/v2ray/v2ray-core/common"
"github.com/v2ray/v2ray-core/common/alloc" "github.com/v2ray/v2ray-core/common/alloc"
) )
@ -12,6 +13,11 @@ type Writer interface {
Write(*alloc.Buffer) error Write(*alloc.Buffer) error
} }
type ReleasableWriter interface {
Writer
common.Releasable
}
// AdaptiveWriter is a Writer that writes alloc.Buffer into underlying writer. // AdaptiveWriter is a Writer that writes alloc.Buffer into underlying writer.
type AdaptiveWriter struct { type AdaptiveWriter struct {
writer io.Writer writer io.Writer

View File

@ -179,11 +179,12 @@ func (this *VMessInboundHandler) HandleConnection(connection *hub.TCPConn) {
writer.SetCached(false) writer.SetCached(false)
go func(finish *sync.Mutex) { go func(finish *sync.Mutex) {
var writer v2io.Writer = v2io.NewAdaptiveWriter(bodyWriter) var writer v2io.ReleasableWriter = v2io.NewAdaptiveWriter(bodyWriter)
if request.Option.IsChunkStream() { if request.Option.IsChunkStream() {
writer = vmessio.NewAuthChunkWriter(writer) writer = vmessio.NewAuthChunkWriter(writer)
} }
v2io.ChanToWriter(writer, output) v2io.ChanToWriter(writer, output)
writer.Release()
finish.Unlock() finish.Unlock()
}(&writeFinish) }(&writeFinish)
writeFinish.Lock() writeFinish.Lock()

View File

@ -9,10 +9,10 @@ import (
) )
type AuthChunkWriter struct { type AuthChunkWriter struct {
writer v2io.Writer writer v2io.ReleasableWriter
} }
func NewAuthChunkWriter(writer v2io.Writer) *AuthChunkWriter { func NewAuthChunkWriter(writer v2io.ReleasableWriter) *AuthChunkWriter {
return &AuthChunkWriter{ return &AuthChunkWriter{
writer: writer, writer: writer,
} }
@ -24,7 +24,8 @@ func (this *AuthChunkWriter) Write(buffer *alloc.Buffer) error {
} }
func (this *AuthChunkWriter) Release() { func (this *AuthChunkWriter) Release() {
this.writer = nil this.writer.Release()
this.writer = nil
} }
func Authenticate(buffer *alloc.Buffer) { func Authenticate(buffer *alloc.Buffer) {

View File

@ -116,11 +116,12 @@ func (this *VMessOutboundHandler) handleRequest(session *raw.ClientSession, conn
writer.SetCached(false) writer.SetCached(false)
if moreChunks { if moreChunks {
var streamWriter v2io.Writer = v2io.NewAdaptiveWriter(bodyWriter) var streamWriter v2io.ReleasableWriter = v2io.NewAdaptiveWriter(bodyWriter)
if request.Option.IsChunkStream() { if request.Option.IsChunkStream() {
streamWriter = vmessio.NewAuthChunkWriter(streamWriter) streamWriter = vmessio.NewAuthChunkWriter(streamWriter)
} }
v2io.ChanToWriter(streamWriter, input) v2io.ChanToWriter(streamWriter, input)
streamWriter.Release()
} }
return return
} }