mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-22 18:17:52 -05:00
remove lock on bytes reader and writer
This commit is contained in:
parent
f195f15536
commit
ea33b7691b
@ -1,9 +1,6 @@
|
|||||||
package buf
|
package buf
|
||||||
|
|
||||||
import (
|
import "io"
|
||||||
"io"
|
|
||||||
"sync"
|
|
||||||
)
|
|
||||||
|
|
||||||
// BytesToBufferReader is a Reader that adjusts its reading speed automatically.
|
// BytesToBufferReader is a Reader that adjusts its reading speed automatically.
|
||||||
type BytesToBufferReader struct {
|
type BytesToBufferReader struct {
|
||||||
@ -52,7 +49,6 @@ func (v *BytesToBufferReader) Release() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type BufferToBytesReader struct {
|
type BufferToBytesReader struct {
|
||||||
sync.Mutex
|
|
||||||
stream Reader
|
stream Reader
|
||||||
current *Buffer
|
current *Buffer
|
||||||
eof bool
|
eof bool
|
||||||
@ -74,8 +70,6 @@ func (v *BufferToBytesReader) Read(b []byte) (int, error) {
|
|||||||
return 0, io.EOF
|
return 0, io.EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
v.Lock()
|
|
||||||
defer v.Unlock()
|
|
||||||
if v.current == nil {
|
if v.current == nil {
|
||||||
v.Fill()
|
v.Fill()
|
||||||
if v.eof {
|
if v.eof {
|
||||||
@ -92,11 +86,7 @@ func (v *BufferToBytesReader) Read(b []byte) (int, error) {
|
|||||||
|
|
||||||
// Release implements Releasable.Release().
|
// Release implements Releasable.Release().
|
||||||
func (v *BufferToBytesReader) Release() {
|
func (v *BufferToBytesReader) Release() {
|
||||||
v.Lock()
|
|
||||||
defer v.Unlock()
|
|
||||||
|
|
||||||
v.eof = true
|
v.eof = true
|
||||||
v.current.Release()
|
v.current.Release()
|
||||||
v.current = nil
|
v.current = nil
|
||||||
v.stream = nil
|
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,6 @@
|
|||||||
package buf
|
package buf
|
||||||
|
|
||||||
import (
|
import "io"
|
||||||
"io"
|
|
||||||
"sync"
|
|
||||||
)
|
|
||||||
|
|
||||||
// BufferToBytesWriter is a Writer that writes alloc.Buffer into underlying writer.
|
// BufferToBytesWriter is a Writer that writes alloc.Buffer into underlying writer.
|
||||||
type BufferToBytesWriter struct {
|
type BufferToBytesWriter struct {
|
||||||
@ -32,17 +29,10 @@ func (v *BufferToBytesWriter) Release() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type BytesToBufferWriter struct {
|
type BytesToBufferWriter struct {
|
||||||
sync.Mutex
|
|
||||||
writer Writer
|
writer Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *BytesToBufferWriter) Write(payload []byte) (int, error) {
|
func (v *BytesToBufferWriter) Write(payload []byte) (int, error) {
|
||||||
v.Lock()
|
|
||||||
defer v.Unlock()
|
|
||||||
if v.writer == nil {
|
|
||||||
return 0, io.ErrClosedPipe
|
|
||||||
}
|
|
||||||
|
|
||||||
bytesWritten := 0
|
bytesWritten := 0
|
||||||
size := len(payload)
|
size := len(payload)
|
||||||
for size > 0 {
|
for size > 0 {
|
||||||
@ -62,8 +52,5 @@ func (v *BytesToBufferWriter) Write(payload []byte) (int, error) {
|
|||||||
|
|
||||||
// Release implements Releasable.Release()
|
// Release implements Releasable.Release()
|
||||||
func (v *BytesToBufferWriter) Release() {
|
func (v *BytesToBufferWriter) Release() {
|
||||||
v.Lock()
|
|
||||||
v.writer.Release()
|
v.writer.Release()
|
||||||
v.writer = nil
|
|
||||||
v.Unlock()
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user