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