From 70f803173a7105ec9829d84428cbd96be5a98e79 Mon Sep 17 00:00:00 2001 From: v2ray Date: Tue, 12 Apr 2016 21:43:13 +0200 Subject: [PATCH] simplify reader/writer interface --- common/io/reader.go | 6 +----- common/io/writer.go | 8 ++------ proxy/shadowsocks/ota.go | 5 +++++ proxy/shadowsocks/shadowsocks.go | 1 + proxy/vmess/inbound/inbound.go | 6 +++--- proxy/vmess/io/writer.go | 6 +++--- proxy/vmess/outbound/outbound.go | 5 +++-- 7 files changed, 18 insertions(+), 19 deletions(-) diff --git a/common/io/reader.go b/common/io/reader.go index d0a438a6..cd652411 100644 --- a/common/io/reader.go +++ b/common/io/reader.go @@ -20,15 +20,11 @@ func ReadFrom(reader io.Reader, buffer *alloc.Buffer) (*alloc.Buffer, error) { // Reader extends io.Reader with alloc.Buffer. type Reader interface { + common.Releasable // Read reads content from underlying reader, and put it into an alloc.Buffer. 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 diff --git a/common/io/writer.go b/common/io/writer.go index 202c24f9..1cefa25d 100644 --- a/common/io/writer.go +++ b/common/io/writer.go @@ -3,21 +3,17 @@ package io import ( "io" - "github.com/v2ray/v2ray-core/common" + "github.com/v2ray/v2ray-core/common" "github.com/v2ray/v2ray-core/common/alloc" ) // Writer extends io.Writer with alloc.Buffer. type Writer interface { + common.Releasable // Write writes an alloc.Buffer into underlying writer. 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/shadowsocks/ota.go b/proxy/shadowsocks/ota.go index e8c7e5e0..c130dbec 100644 --- a/proxy/shadowsocks/ota.go +++ b/proxy/shadowsocks/ota.go @@ -66,6 +66,11 @@ func NewChunkReader(reader io.Reader, auth *Authenticator) *ChunkReader { } } +func (this *ChunkReader) Release() { + this.reader = nil + this.auth = nil +} + func (this *ChunkReader) Read() (*alloc.Buffer, error) { buffer := alloc.NewLargeBuffer() if _, err := io.ReadFull(this.reader, buffer.Value[:2]); err != nil { diff --git a/proxy/shadowsocks/shadowsocks.go b/proxy/shadowsocks/shadowsocks.go index 59c7968b..9f721fb4 100644 --- a/proxy/shadowsocks/shadowsocks.go +++ b/proxy/shadowsocks/shadowsocks.go @@ -234,6 +234,7 @@ func (this *Shadowsocks) handleConnection(conn *hub.TCPConn) { v2io.ReaderToChan(ray.InboundInput(), payloadReader) close(ray.InboundInput()) + payloadReader.Release() writeFinish.Lock() } diff --git a/proxy/vmess/inbound/inbound.go b/proxy/vmess/inbound/inbound.go index 3955cc04..3a8f7c38 100644 --- a/proxy/vmess/inbound/inbound.go +++ b/proxy/vmess/inbound/inbound.go @@ -148,7 +148,7 @@ func (this *VMessInboundHandler) HandleConnection(connection *hub.TCPConn) { defer close(input) defer readFinish.Unlock() bodyReader := session.DecodeRequestBody(reader) - var requestReader v2io.ReleasableReader + var requestReader v2io.Reader if request.Option.IsChunkStream() { requestReader = vmessio.NewAuthChunkReader(bodyReader) } else { @@ -179,12 +179,12 @@ func (this *VMessInboundHandler) HandleConnection(connection *hub.TCPConn) { writer.SetCached(false) go func(finish *sync.Mutex) { - var writer v2io.ReleasableWriter = v2io.NewAdaptiveWriter(bodyWriter) + var writer v2io.Writer = v2io.NewAdaptiveWriter(bodyWriter) if request.Option.IsChunkStream() { writer = vmessio.NewAuthChunkWriter(writer) } v2io.ChanToWriter(writer, output) - writer.Release() + writer.Release() finish.Unlock() }(&writeFinish) writeFinish.Lock() diff --git a/proxy/vmess/io/writer.go b/proxy/vmess/io/writer.go index 2ccfb9eb..1bff745c 100644 --- a/proxy/vmess/io/writer.go +++ b/proxy/vmess/io/writer.go @@ -9,10 +9,10 @@ import ( ) type AuthChunkWriter struct { - writer v2io.ReleasableWriter + writer v2io.Writer } -func NewAuthChunkWriter(writer v2io.ReleasableWriter) *AuthChunkWriter { +func NewAuthChunkWriter(writer v2io.Writer) *AuthChunkWriter { return &AuthChunkWriter{ writer: writer, } @@ -25,7 +25,7 @@ func (this *AuthChunkWriter) Write(buffer *alloc.Buffer) error { func (this *AuthChunkWriter) Release() { this.writer.Release() - this.writer = nil + this.writer = nil } func Authenticate(buffer *alloc.Buffer) { diff --git a/proxy/vmess/outbound/outbound.go b/proxy/vmess/outbound/outbound.go index 6bf96269..c9f6abc0 100644 --- a/proxy/vmess/outbound/outbound.go +++ b/proxy/vmess/outbound/outbound.go @@ -116,12 +116,12 @@ func (this *VMessOutboundHandler) handleRequest(session *raw.ClientSession, conn writer.SetCached(false) if moreChunks { - var streamWriter v2io.ReleasableWriter = v2io.NewAdaptiveWriter(bodyWriter) + var streamWriter v2io.Writer = v2io.NewAdaptiveWriter(bodyWriter) if request.Option.IsChunkStream() { streamWriter = vmessio.NewAuthChunkWriter(streamWriter) } v2io.ChanToWriter(streamWriter, input) - streamWriter.Release() + streamWriter.Release() } return } @@ -150,6 +150,7 @@ func (this *VMessOutboundHandler) handleResponse(session *raw.ClientSession, con } v2io.ReaderToChan(output, bodyReader) + bodyReader.Release() return }