forked from funny/link
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
把可重用的CodeType移回根目录;BufioCodeType引入sync.Pool优化;加入ThreadSafe替代Session的收发锁
- Loading branch information
Showing
7 changed files
with
270 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package link | ||
|
||
import ( | ||
"bufio" | ||
"io" | ||
"sync" | ||
) | ||
|
||
type BufioCodecType struct { | ||
base CodecType | ||
readBufferSize int | ||
writeBufferSize int | ||
readerPool sync.Pool | ||
writerPool sync.Pool | ||
} | ||
|
||
func Bufio(base CodecType) *BufioCodecType { | ||
return BufioSize(base, 4096, 4096) | ||
} | ||
|
||
func BufioSize(base CodecType, readBufferSize, writeBufferSize int) *BufioCodecType { | ||
return &BufioCodecType{ | ||
base: base, | ||
readBufferSize: readBufferSize, | ||
writeBufferSize: writeBufferSize, | ||
} | ||
} | ||
|
||
func (codecType *BufioCodecType) NewEncoder(w io.Writer) Encoder { | ||
if codecType.writeBufferSize == 0 { | ||
return codecType.base.NewEncoder(w) | ||
} | ||
bw, ok := codecType.writerPool.Get().(*bufio.Writer) | ||
if ok { | ||
bw.Reset(w) | ||
} else { | ||
bw = bufio.NewWriterSize(w, codecType.writeBufferSize) | ||
} | ||
return &bufioEncoder{ | ||
writer: bw, | ||
pool: &codecType.writerPool, | ||
base: codecType.base.NewEncoder(bw), | ||
} | ||
} | ||
|
||
func (codecType *BufioCodecType) NewDecoder(r io.Reader) Decoder { | ||
if codecType.readBufferSize == 0 { | ||
return codecType.base.NewDecoder(r) | ||
} | ||
br, ok := codecType.readerPool.Get().(*bufio.Reader) | ||
if ok { | ||
br.Reset(r) | ||
} else { | ||
br = bufio.NewReaderSize(r, codecType.readBufferSize) | ||
} | ||
return &bufioDecoder{ | ||
reader: br, | ||
pool: &codecType.readerPool, | ||
base: codecType.base.NewDecoder(br), | ||
} | ||
} | ||
|
||
type bufioEncoder struct { | ||
writer *bufio.Writer | ||
pool *sync.Pool | ||
base Encoder | ||
} | ||
|
||
func (encoder *bufioEncoder) Encode(msg interface{}) error { | ||
if err := encoder.base.Encode(msg); err != nil { | ||
return err | ||
} | ||
return encoder.writer.Flush() | ||
} | ||
|
||
func (encoder *bufioEncoder) Dispose() { | ||
if d, ok := encoder.base.(Disposeable); ok { | ||
d.Dispose() | ||
} | ||
encoder.pool.Put(encoder.writer) | ||
} | ||
|
||
type bufioDecoder struct { | ||
reader *bufio.Reader | ||
pool *sync.Pool | ||
base Decoder | ||
} | ||
|
||
func (decoder *bufioDecoder) Decode(msg interface{}) error { | ||
return decoder.base.Decode(msg) | ||
} | ||
|
||
func (decoder *bufioDecoder) Dispose() { | ||
if d, ok := decoder.base.(Disposeable); ok { | ||
d.Dispose() | ||
} | ||
decoder.pool.Put(decoder.reader) | ||
} |
Oops, something went wrong.