From 538b0720d55ede1cdf9505acfc5a2e46970eae5c Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Tue, 27 Dec 2016 21:06:55 +0100 Subject: [PATCH] simplified Release() --- common/buf/reader.go | 3 ++- common/buf/writer.go | 3 ++- common/bufio/reader.go | 5 ++--- common/bufio/writer.go | 4 +--- common/common.go | 7 +++++++ 5 files changed, 14 insertions(+), 8 deletions(-) diff --git a/common/buf/reader.go b/common/buf/reader.go index 15805339c..04b6cda40 100644 --- a/common/buf/reader.go +++ b/common/buf/reader.go @@ -1,6 +1,7 @@ package buf import "io" +import "v2ray.com/core/common" // BytesToBufferReader is a Reader that adjusts its reading speed automatically. type BytesToBufferReader struct { @@ -45,7 +46,7 @@ func (v *BytesToBufferReader) Read() (*Buffer, error) { // Release implements Releasable.Release(). func (v *BytesToBufferReader) Release() { - v.reader = nil + common.Release(v.reader) } type BufferToBytesReader struct { diff --git a/common/buf/writer.go b/common/buf/writer.go index b762a7e63..e89722214 100644 --- a/common/buf/writer.go +++ b/common/buf/writer.go @@ -1,6 +1,7 @@ package buf import "io" +import "v2ray.com/core/common" // BufferToBytesWriter is a Writer that writes alloc.Buffer into underlying writer. type BufferToBytesWriter struct { @@ -25,7 +26,7 @@ func (v *BufferToBytesWriter) Write(buffer *Buffer) error { // Release implements Releasable.Release(). func (v *BufferToBytesWriter) Release() { - v.writer = nil + common.Release(v.writer) } type BytesToBufferWriter struct { diff --git a/common/bufio/reader.go b/common/bufio/reader.go index 7957438da..c9e430f0c 100644 --- a/common/bufio/reader.go +++ b/common/bufio/reader.go @@ -27,9 +27,8 @@ func NewReader(rawReader io.Reader) *BufferedReader { func (v *BufferedReader) Release() { v.buffer.Release() v.buffer = nil - if releasable, ok := v.reader.(common.Releasable); ok { - releasable.Release() - } + + common.Release(v.reader) } // Cached returns true if the internal cache is effective. diff --git a/common/bufio/writer.go b/common/bufio/writer.go index c61255ac6..742fb5f72 100644 --- a/common/bufio/writer.go +++ b/common/bufio/writer.go @@ -91,7 +91,5 @@ func (v *BufferedWriter) Release() { v.buffer.Release() v.buffer = nil - if releasable, ok := v.writer.(common.Releasable); ok { - releasable.Release() - } + common.Release(v.writer) } diff --git a/common/common.go b/common/common.go index d0b70ead8..551bdb965 100644 --- a/common/common.go +++ b/common/common.go @@ -18,3 +18,10 @@ type Releasable interface { // Release releases all references to accelerate garbage collection. Release() } + +// Release tries to release the given object. +func Release(v interface{}) { + if releasable, ok := v.(Releasable); ok { + releasable.Release() + } +}