From e8023f0d92f44cb52b3cf7c6735c7f42a4ddf763 Mon Sep 17 00:00:00 2001 From: v2ray Date: Thu, 24 Mar 2016 23:36:18 +0800 Subject: [PATCH] releasable writer --- common/io/writer.go | 6 ++++++ proxy/vmess/inbound/inbound.go | 3 ++- proxy/vmess/io/writer.go | 7 ++++--- proxy/vmess/outbound/outbound.go | 3 ++- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/common/io/writer.go b/common/io/writer.go index 007d5efc..202c24f9 100644 --- a/common/io/writer.go +++ b/common/io/writer.go @@ -3,6 +3,7 @@ package io import ( "io" + "github.com/v2ray/v2ray-core/common" "github.com/v2ray/v2ray-core/common/alloc" ) @@ -12,6 +13,11 @@ type Writer interface { Write(*alloc.Buffer) error } +type ReleasableWriter interface { + Writer + common.Releasable +} + // AdaptiveWriter is a Writer that writes alloc.Buffer into underlying writer. type AdaptiveWriter struct { writer io.Writer diff --git a/proxy/vmess/inbound/inbound.go b/proxy/vmess/inbound/inbound.go index 873f2aa0..3955cc04 100644 --- a/proxy/vmess/inbound/inbound.go +++ b/proxy/vmess/inbound/inbound.go @@ -179,11 +179,12 @@ func (this *VMessInboundHandler) HandleConnection(connection *hub.TCPConn) { writer.SetCached(false) go func(finish *sync.Mutex) { - var writer v2io.Writer = v2io.NewAdaptiveWriter(bodyWriter) + var writer v2io.ReleasableWriter = v2io.NewAdaptiveWriter(bodyWriter) if request.Option.IsChunkStream() { writer = vmessio.NewAuthChunkWriter(writer) } v2io.ChanToWriter(writer, output) + writer.Release() finish.Unlock() }(&writeFinish) writeFinish.Lock() diff --git a/proxy/vmess/io/writer.go b/proxy/vmess/io/writer.go index a8c987f3..2ccfb9eb 100644 --- a/proxy/vmess/io/writer.go +++ b/proxy/vmess/io/writer.go @@ -9,10 +9,10 @@ import ( ) type AuthChunkWriter struct { - writer v2io.Writer + writer v2io.ReleasableWriter } -func NewAuthChunkWriter(writer v2io.Writer) *AuthChunkWriter { +func NewAuthChunkWriter(writer v2io.ReleasableWriter) *AuthChunkWriter { return &AuthChunkWriter{ writer: writer, } @@ -24,7 +24,8 @@ func (this *AuthChunkWriter) Write(buffer *alloc.Buffer) error { } func (this *AuthChunkWriter) Release() { - this.writer = nil + this.writer.Release() + this.writer = nil } func Authenticate(buffer *alloc.Buffer) { diff --git a/proxy/vmess/outbound/outbound.go b/proxy/vmess/outbound/outbound.go index 649d106e..6bf96269 100644 --- a/proxy/vmess/outbound/outbound.go +++ b/proxy/vmess/outbound/outbound.go @@ -116,11 +116,12 @@ func (this *VMessOutboundHandler) handleRequest(session *raw.ClientSession, conn writer.SetCached(false) if moreChunks { - var streamWriter v2io.Writer = v2io.NewAdaptiveWriter(bodyWriter) + var streamWriter v2io.ReleasableWriter = v2io.NewAdaptiveWriter(bodyWriter) if request.Option.IsChunkStream() { streamWriter = vmessio.NewAuthChunkWriter(streamWriter) } v2io.ChanToWriter(streamWriter, input) + streamWriter.Release() } return }