rpc加入压缩功能

This commit is contained in:
duanhf2012
2023-07-10 14:22:23 +08:00
parent 962016d476
commit 85a8ec58e5
4 changed files with 154 additions and 12 deletions

69
rpc/compressor.go Normal file
View File

@@ -0,0 +1,69 @@
package rpc
import (
"runtime"
"errors"
"github.com/pierrec/lz4/v4"
"fmt"
)
type ICompressor interface {
CompressBlock(src, dst []byte) (int, error) //dst如果有预申请使用dst内存传入nil时内部申请
UncompressBlock(src []byte, dst []byte) (int, error)//dst如果有预申请使用dst内存传入nil时内部申请
CompressBlockBound(n int) int
UnCompressBlockBound(n int) int
}
var compressor ICompressor
func init(){
SetCompressor(&Lz4Compressor{})
}
func SetCompressor(cp ICompressor){
compressor = cp
}
type Lz4Compressor struct {
}
func (lc *Lz4Compressor) CompressBlock(src, dst []byte) (cnt int, err error) {
defer func() {
if r := recover(); r != nil {
buf := make([]byte, 4096)
l := runtime.Stack(buf, false)
errString := fmt.Sprint(r)
err = errors.New("core dump info[" + errString + "]\n" + string(buf[:l]))
}
}()
var c lz4.Compressor
cnt, err = c.CompressBlock(src, dst)
return
}
func (lc *Lz4Compressor) UncompressBlock(src, dst []byte) (cnt int, err error) {
defer func() {
if r := recover(); r != nil {
buf := make([]byte, 4096)
l := runtime.Stack(buf, false)
errString := fmt.Sprint(r)
err = errors.New("core dump info[" + errString + "]\n" + string(buf[:l]))
}
}()
cnt, err = lz4.UncompressBlock(src, dst)
return
}
func (lc *Lz4Compressor) CompressBlockBound(n int) int{
return lz4.CompressBlockBound(n)
}
func (lc *Lz4Compressor) UnCompressBlockBound(n int) int{
return n*10
}