/
marshal.go
218 lines (187 loc) · 4.6 KB
/
marshal.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package cache
import (
"context"
"fmt"
"runtime"
"github.com/klauspost/compress/s2"
"github.com/vmihailenco/msgpack/v5"
"golang.org/x/sync/errgroup"
)
const (
compressionThreshold = 64
noCompression = 0x0
s2Compression = 0x1
)
func (self *Cache) Marshal(value any) ([]byte, error) {
return self.marshal(value)
}
func (self *Cache) Unmarshal(b []byte, value any) error {
return self.unmarshal(b, value)
}
func (self *Cache) marshalItems(ctx context.Context, items []Item,
) ([][]byte, error) {
g := self.marshalGroup(ctx)
bytes := make([][]byte, len(items))
var err error
for i := range items {
if g.Canceled() {
err = g.Cause()
break
}
g.GoMarshal(&items[i], func(b []byte) { bytes[i] = b })
}
const errMsg = "failed marshal items: %w"
if err2 := g.Wait(); err2 != nil {
return nil, fmt.Errorf(errMsg, err2)
} else if err != nil {
return nil, fmt.Errorf(errMsg, err)
}
return bytes, nil
}
func (self *Cache) unmarshalItems(ctx context.Context, bytes [][]byte,
items []Item,
) (err error) {
g := self.unmarshalGroup(ctx)
for i := range items {
if g.Canceled() {
err = g.Cause()
break
}
if err = g.GoUnmarshal(bytes[i], items[i].Value); err != nil {
break
}
}
const errMsg = "failed unmarshal items: %w"
if err2 := g.Wait(); err2 != nil {
err = fmt.Errorf(errMsg, err2)
} else if err != nil {
err = fmt.Errorf(errMsg, err)
}
return
}
// --------------------------------------------------
func (self *Cache) marshalGroup(ctx context.Context) marshalGroup {
g, ctx := errgroup.WithContext(ctx)
if self.valueProcs < 1 {
g.SetLimit(runtime.GOMAXPROCS(0))
} else {
g.SetLimit(self.valueProcs)
}
return marshalGroup{ctx: ctx, g: g, cache: self}
}
func (self *Cache) unmarshalGroup(ctx context.Context) marshalGroup {
g, ctx := errgroup.WithContext(ctx)
return marshalGroup{ctx: ctx, g: g, cache: self}
}
type marshalGroup struct {
ctx context.Context
g *errgroup.Group
cache *Cache
}
func (self *marshalGroup) GoMarshal(item *Item, fn func([]byte)) {
self.g.Go(func() error {
b, err := item.marshal(self.ctx, self.marshal)
if err != nil {
return err
}
fn(b)
return nil
})
}
func (self *marshalGroup) marshal(v any) ([]byte, error) {
if err := self.cache.marshalers.Acquire(self.ctx, 1); err != nil {
return nil, fmt.Errorf("failed acquire marshalers: %w", err)
}
defer self.cache.marshalers.Release(1)
b, err := self.cache.marshal(v)
if err != nil {
return nil, err
}
return b, nil
}
func (self *marshalGroup) GoUnmarshal(b []byte, v any) error {
if err := self.cache.marshalers.Acquire(self.ctx, 1); err != nil {
return fmt.Errorf("failed acquire marshalers: %w", err)
}
self.g.Go(func() error {
defer self.cache.marshalers.Release(1)
if err := self.cache.unmarshal(b, v); err != nil {
return err
}
return nil
})
return nil
}
func (self *marshalGroup) Ctx() context.Context {
return self.ctx
}
func (self *marshalGroup) Canceled() bool {
return self.ctx.Err() != nil
}
func (self *marshalGroup) Cause() error {
return context.Cause(self.ctx) //nolint:wrapcheck // we need the error as is
}
func (self *marshalGroup) Wait() error {
return self.g.Wait() //nolint:wrapcheck // caller will wrap the error
}
// --------------------------------------------------
func marshal(value any) ([]byte, error) {
switch value := value.(type) {
case nil:
return nil, nil
case []byte:
return value, nil
case string:
return []byte(value), nil
}
b, err := msgpack.Marshal(value)
if err != nil {
return nil, fmt.Errorf("marshal: msgpack error: %w", err)
}
return compress(b), nil
}
func unmarshal(b []byte, value any) error {
if len(b) == 0 {
return nil
}
switch value := value.(type) {
case nil:
return nil
case *[]byte:
clone := make([]byte, len(b))
copy(clone, b)
*value = clone
return nil
case *string:
*value = string(b)
return nil
}
switch c := b[len(b)-1]; c {
case noCompression:
b = b[:len(b)-1]
case s2Compression:
b = b[:len(b)-1]
if decoded, err := s2.Decode(nil, b); err != nil {
return fmt.Errorf("unmarshal: decompress error: %w", err)
} else {
b = decoded
}
default:
return fmt.Errorf("unmarshal: unknown compression method: %x", c)
}
if err := msgpack.Unmarshal(b, value); err != nil {
return fmt.Errorf("unmarshal msgpack error: %w", err)
}
return nil
}
func compress(data []byte) []byte {
if len(data) < compressionThreshold {
b := make([]byte, len(data)+1)
copy(b, data)
b[len(data)] = noCompression
return b
}
b := make([]byte, s2.MaxEncodedLen(len(data))+1)
b = s2.Encode(b, data)
return append(b, s2Compression) //nolint:makezero // b is a sub-slice
}