Skip to content

Commit

Permalink
合并上下文和非上下文接管
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong committed May 16, 2024
1 parent a89f238 commit e99b99e
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 70 deletions.
45 changes: 11 additions & 34 deletions deflate/compression_decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,6 @@ import (

var tailBytes = []byte{0x00, 0x00, 0xff, 0xff, 0x01, 0x00, 0x00, 0xff, 0xff}

// 无上下文-解压缩
func newDecompressNoContextTakeover(r io.Reader) io.ReadCloser {
fr, _ := flateReaderPool.Get().(io.ReadCloser)
fr.(flate.Resetter).Reset(io.MultiReader(r, bytes.NewReader(tailBytes)), nil)
return &flateReadWrapper{fr}
}

// 无上下文-解压缩
func DecompressNoContextTakeover(payload *[]byte) (*[]byte, error) {
pr := bytes.NewReader(*payload)
r := newDecompressNoContextTakeover(pr)

// 从池里面拿buf, 这里的2是经验值,解压缩之后是2倍的大小
decodeBuf := bytespool.GetBytes(len(*payload)*2 + enum.MaxFrameHeaderSize)
// 包装下
out := bytes.NewBuffer((*decodeBuf)[:0])
// 解压缩
if _, err := io.Copy(out, r); err != nil {
return nil, err
}
// 拿到解压缩之后的buf
outBytes := out.Bytes()
// 如果解压缩之后的buf和从池里面拿的buf不一样,就把从池里面拿的buf放回去
if unsafe.SliceData(*decodeBuf) != unsafe.SliceData(outBytes) {
bytespool.PutBytes(decodeBuf)
}

r.Close()
return &outBytes, nil
}

// 上下文-解压缩
type DeCompressContextTakeover struct {
dict historyDict
Expand All @@ -71,9 +40,14 @@ func NewDecompressContextTakeover(bit uint8) (*DeCompressContextTakeover, error)
}

// 解压缩
// d有值时,上下文接管的情况调用
// d为nil时, 上下文不接管的情况下调用,利用了go,对象为空,调用函数不会panic的特性
func (d *DeCompressContextTakeover) Decompress(payload *[]byte, maxMessage int64) (outBytes2 *[]byte, err error) {
// 获取dict
dict := d.dict.GetData()
var dict []byte
if d != nil {
dict = d.dict.GetData()
}

// 拿到解码器
rc, _ := flateReaderPool.Get().(io.Reader)
Expand Down Expand Up @@ -107,8 +81,11 @@ func (d *DeCompressContextTakeover) Decompress(payload *[]byte, maxMessage int64
if unsafe.SliceData(*decodeBuf) != unsafe.SliceData(outBytes) {
bytespool.PutBytes(decodeBuf)
}
// 写入dict
d.dict.Write(out.Bytes())

if d != nil {
// 写入dict
d.dict.Write(out.Bytes())
}
// 返回解压缩之后的buf
return &outBytes, nil
}
34 changes: 0 additions & 34 deletions deflate/compression_decode_no_context.go

This file was deleted.

1 change: 1 addition & 0 deletions deflate/compression_decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func TestDecompressNoContextTakeover(t *testing.T) {
type args struct {
payload []byte
}

tests := []struct {
name string
args args
Expand Down
3 changes: 2 additions & 1 deletion deflate/compression_encode_no_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ func Test_compressNoContextTakeover(t *testing.T) {
return
}

gotDecode, err := DecompressNoContextTakeover(gotEncodeBuf)
var decCtx *DeCompressContextTakeover
gotDecode, err := decCtx.Decompress(gotEncodeBuf, 0)
if (err != nil) != tt.wantErr {
t.Errorf("decompressNoContextTakeover() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down
3 changes: 2 additions & 1 deletion deflate/compression_encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ func TestEnCompressContextTakeover_Compress(t *testing.T) {
return
}

decodePayload, err := DecompressNoContextTakeover(gotEncodePayload)
var decCtx *DeCompressContextTakeover
decodePayload, err := decCtx.Decompress(gotEncodePayload, 0)
if (err != nil) != tt.wantErr {
t.Errorf("CompressContextTakeover.Compress() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down

0 comments on commit e99b99e

Please sign in to comment.