github.com/cloudwego/kitex@v0.9.0/pkg/remote/codec/grpc/grpc_compress.go (about) 1 /* 2 * Copyright 2023 CloudWeGo Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package grpc 18 19 import ( 20 "bytes" 21 "context" 22 "encoding/binary" 23 "errors" 24 "io" 25 26 "github.com/cloudwego/kitex/pkg/rpcinfo" 27 28 "github.com/bytedance/gopkg/lang/mcache" 29 30 "github.com/cloudwego/kitex/pkg/remote/codec/protobuf/encoding" 31 32 "github.com/cloudwego/kitex/pkg/remote" 33 ) 34 35 func getSendCompressor(ctx context.Context) (encoding.Compressor, error) { 36 ri := rpcinfo.GetRPCInfo(ctx) 37 return encoding.FindCompressor(remote.GetSendCompressor(ri)) 38 } 39 40 func decodeGRPCFrame(ctx context.Context, in remote.ByteBuffer) ([]byte, error) { 41 ri := rpcinfo.GetRPCInfo(ctx) 42 compressor, err := encoding.FindCompressor(remote.GetRecvCompressor(ri)) 43 if err != nil { 44 return nil, err 45 } 46 hdr, err := in.Next(5) 47 if err != nil { 48 return nil, err 49 } 50 compressFlag := hdr[0] 51 dLen := int(binary.BigEndian.Uint32(hdr[1:])) 52 d, err := in.Next(dLen) 53 if err != nil { 54 return nil, err 55 } 56 if compressFlag == 1 { 57 if compressor == nil { 58 return nil, errors.New("kitex compression algorithm not found") 59 } 60 return decompress(compressor, d) 61 } 62 return d, nil 63 } 64 65 func compress(compressor encoding.Compressor, data []byte) ([]byte, error) { 66 defer mcache.Free(data) 67 cbuf := &bytes.Buffer{} 68 z, err := compressor.Compress(cbuf) 69 if err != nil { 70 return nil, err 71 } 72 if _, err = z.Write(data); err != nil { 73 return nil, err 74 } 75 if err = z.Close(); err != nil { 76 return nil, err 77 } 78 return cbuf.Bytes(), nil 79 } 80 81 func decompress(compressor encoding.Compressor, data []byte) ([]byte, error) { 82 dcReader, er := compressor.Decompress(bytes.NewReader(data)) 83 if er != nil { 84 return nil, er 85 } 86 var buf bytes.Buffer 87 _, err := io.Copy(&buf, dcReader) 88 if err != nil { 89 return nil, err 90 } 91 return buf.Bytes(), nil 92 }