-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.go
411 lines (356 loc) · 9.89 KB
/
utils.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// Package utils 一些常用工具
package utils
import (
"bytes"
"context"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"fmt"
"hash"
"io"
"io/ioutil"
"os"
"os/exec"
"reflect"
"regexp"
"runtime"
"runtime/debug"
"strconv"
"strings"
"time"
"github.com/Laisky/zap"
jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
)
var (
json = jsoniter.ConfigCompatibleWithStandardLibrary
// JSON effective json
JSON = json
)
const (
defaultCgroupMemLimitPath = "/sys/fs/cgroup/memory/memory.limit_in_bytes"
defaultGCMemRatio = uint64(85)
)
// CtxKeyT type of context key
type CtxKeyT struct{}
// IsHasField check is struct has field
//
// inspired by https://mrwaggel.be/post/golang-reflect-if-initialized-struct-has-member-method-or-fields/
func IsHasField(st interface{}, fieldName string) bool {
valueIface := reflect.ValueOf(st)
// Check if the passed interface is a pointer
if valueIface.Type().Kind() != reflect.Ptr {
// Create a new type of Iface's Type, so we have a pointer to work with
valueIface = reflect.New(reflect.TypeOf(st))
}
// 'dereference' with Elem() and get the field by name
field := valueIface.Elem().FieldByName(fieldName)
return field.IsValid()
}
// IsHasMethod check is struct has method
//
// inspired by https://mrwaggel.be/post/golang-reflect-if-initialized-struct-has-member-method-or-fields/
func IsHasMethod(st interface{}, methodName string) bool {
valueIface := reflect.ValueOf(st)
// Check if the passed interface is a pointer
if valueIface.Type().Kind() != reflect.Ptr {
// Create a new type of Iface, so we have a pointer to work with
valueIface = reflect.New(reflect.TypeOf(st))
}
// Get the method by name
method := valueIface.MethodByName(methodName)
return method.IsValid()
}
// ValidateFileHash validate file content with hashed string
//
// Args:
// * filepath: file path to check
// * hashed: hashed string, like `sha256: xxxx`
func ValidateFileHash(filepath string, hashed string) error {
hs := strings.Split(hashed, ":")
if len(hs) != 2 {
return fmt.Errorf("unknown hashed format, expect is `sha256:xxxx`, but got `%s`", hashed)
}
var hasher hash.Hash
switch hs[0] {
case "sha256":
hasher = sha256.New()
default:
return fmt.Errorf("unknown hasher `%s`", hs[0])
}
fp, err := os.Open(filepath)
if err != nil {
return errors.Wrapf(err, "open file `%s`", filepath)
}
defer fp.Close()
if _, err = io.Copy(hasher, fp); err != nil {
return errors.Wrap(err, "read file content")
}
actualHash := hex.EncodeToString(hasher.Sum(nil))
if hs[1] != actualHash {
return fmt.Errorf("hash `%s` not match expect `%s`", actualHash, hs[1])
}
return nil
}
// GetFuncName return the name of func
func GetFuncName(f interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
}
// FallBack return the fallback when orig got error
// utils.FallBack(func() interface{} { return getIOStatMetric(fs) }, &IOStat{}).(*IOStat)
func FallBack(orig func() interface{}, fallback interface{}) (ret interface{}) {
defer func() {
if recover() != nil {
ret = fallback
}
}()
ret = orig()
return
}
// RegexNamedSubMatch extract key:val map from string by group match
func RegexNamedSubMatch(r *regexp.Regexp, str string, subMatchMap map[string]string) error {
match := r.FindStringSubmatch(str)
names := r.SubexpNames()
if len(names) != len(match) {
return errors.New("the number of args in `regexp` and `str` not matched")
}
for i, name := range r.SubexpNames() {
if i != 0 && name != "" {
subMatchMap[name] = match[i]
}
}
return nil
}
// FlattenMap make embedded map into flatten map
func FlattenMap(data map[string]interface{}, delimiter string) {
for k, vi := range data {
if v2i, ok := vi.(map[string]interface{}); ok {
FlattenMap(v2i, delimiter)
for k3, v3i := range v2i {
data[k+delimiter+k3] = v3i
}
delete(data, k)
}
}
}
// ForceGCBlocking force to run blocking manual gc.
func ForceGCBlocking() {
Logger.Info("force gc")
runtime.GC()
debug.FreeOSMemory()
}
// ForceGCUnBlocking trigger GC unblocking
func ForceGCUnBlocking() {
go func() {
ForceGC()
}()
}
type gcOption struct {
memRatio uint64
memLimitFilePath string
}
// GcOptFunc option for GC utils
type GcOptFunc func(*gcOption) error
// WithGCMemRatio set mem ratio trigger for GC
func WithGCMemRatio(ratio int) GcOptFunc {
return func(opt *gcOption) error {
if ratio <= 0 {
return fmt.Errorf("ratio must > 0, got %d", ratio)
}
if ratio > 100 {
return fmt.Errorf("ratio must <= 0, got %d", ratio)
}
Logger.Debug("set memRatio", zap.Int("ratio", ratio))
opt.memRatio = uint64(ratio)
return nil
}
}
// WithGCMemLimitFilePath set memory limit file
func WithGCMemLimitFilePath(path string) GcOptFunc {
return func(opt *gcOption) error {
if _, err := os.Open(path); err != nil {
return errors.Wrapf(err, "try open path `%s`", path)
}
Logger.Debug("set memLimitFilePath", zap.String("file", path))
opt.memLimitFilePath = path
return nil
}
}
// AutoGC auto trigger GC when memory usage exceeds the custom ration
func AutoGC(ctx context.Context, opts ...GcOptFunc) (err error) {
opt := &gcOption{
memRatio: defaultGCMemRatio,
memLimitFilePath: defaultCgroupMemLimitPath,
}
for _, optf := range opts {
if err = optf(opt); err != nil {
return errors.Wrap(err, "set option")
}
}
var (
fp *os.File
memByte []byte
memLimit uint64
)
if fp, err = os.Open(opt.memLimitFilePath); err != nil {
return errors.Wrapf(err, "open file got error: %+v", opt.memLimitFilePath)
}
defer fp.Close()
if memByte, err = ioutil.ReadAll(fp); err != nil {
return errors.Wrap(err, "read cgroup mem limit file")
}
if err = fp.Close(); err != nil {
Logger.Error("close cgroup mem limit file", zap.Error(err), zap.String("file", opt.memLimitFilePath))
}
if memLimit, err = strconv.ParseUint(string(bytes.TrimSpace(memByte)), 10, 64); err != nil {
return errors.Wrap(err, "parse cgroup memory limit")
}
if memLimit == 0 {
return fmt.Errorf("mem limit should > 0, but got: %d", memLimit)
}
Logger.Info("enable auto gc", zap.Uint64("ratio", opt.memRatio), zap.Uint64("limit", memLimit))
go func(ctx context.Context) {
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
var (
m runtime.MemStats
ratio uint64
)
for {
select {
case <-ticker.C:
case <-ctx.Done():
return
}
runtime.ReadMemStats(&m)
ratio = (m.Alloc * 100) / memLimit
Logger.Debug("mem stat",
zap.Uint64("mem", m.Alloc),
zap.Uint64("limit_mem", memLimit),
zap.Uint64("ratio", ratio),
zap.Uint64("limit_ratio", opt.memRatio),
)
if ratio >= opt.memRatio {
ForceGCBlocking()
}
}
}(ctx)
return nil
}
var (
// ForceGC force to start gc blocking
ForceGC = ForceGCBlocking
// TriggerGC force to start gc unblocking
TriggerGC = ForceGCUnBlocking
)
var defaultTemplateWithMappReg = regexp.MustCompile(`(?sm)\$\{([^}]+)\}`)
// TemplateWithMap replace `${var}` in template string
func TemplateWithMap(tpl string, data map[string]interface{}) string {
return TemplateWithMapAndRegexp(defaultTemplateWithMappReg, tpl, data)
}
// TemplateWithMapAndRegexp replace `${var}` in template string
func TemplateWithMapAndRegexp(tplReg *regexp.Regexp, tpl string, data map[string]interface{}) string {
var (
k, vs string
vi interface{}
)
for _, kg := range tplReg.FindAllStringSubmatch(tpl, -1) {
k = kg[1]
vi = data[k]
switch vi := vi.(type) {
case string:
vs = vi
case []byte:
vs = string(vi)
case int:
vs = strconv.FormatInt(int64(vi), 10)
case int64:
vs = strconv.FormatInt(vi, 10)
case float64:
vs = strconv.FormatFloat(vi, 'f', -1, 64)
}
tpl = strings.ReplaceAll(tpl, "${"+k+"}", vs)
}
return tpl
}
var (
urlMaskingRegexp = regexp.MustCompile(`(\S+:)\S+(@\w+)`)
)
// URLMasking masking password in url
func URLMasking(url, mask string) string {
return urlMaskingRegexp.ReplaceAllString(url, `${1}`+mask+`${2}`)
}
// SetStructFieldsBySlice set field value of structs slice by values slice
func SetStructFieldsBySlice(structs, vals interface{}) (err error) {
sv := reflect.ValueOf(structs)
vv := reflect.ValueOf(vals)
typeCheck := func(name string, v *reflect.Value) error {
switch v.Kind() {
case reflect.Slice:
case reflect.Array:
default:
return fmt.Errorf(name + " must be array/slice")
}
return nil
}
if err = typeCheck("structs", &sv); err != nil {
return err
}
if err = typeCheck("vals", &vv); err != nil {
return err
}
var (
eachGrpValsV reflect.Value
iField, nFields int
)
for i := 0; i < MinInt(sv.Len(), vv.Len()); i++ {
eachGrpValsV = vv.Index(i)
if err = typeCheck("vals."+strconv.FormatInt(int64(i), 10), &eachGrpValsV); err != nil {
return err
}
switch sv.Index(i).Kind() {
case reflect.Ptr:
nFields = sv.Index(i).Elem().NumField()
default:
nFields = sv.Index(i).NumField()
}
for iField = 0; iField < MinInt(eachGrpValsV.Len(), nFields); iField++ {
switch sv.Index(i).Kind() {
case reflect.Ptr:
sv.Index(i).Elem().Field(iField).Set(eachGrpValsV.Index(iField))
default:
sv.Index(i).Field(iField).Set(eachGrpValsV.Index(iField))
}
}
}
return
}
// UniqueStrings remove duplicate string in slice
func UniqueStrings(vs []string) (r []string) {
m := map[string]struct{}{}
var ok bool
for _, v := range vs {
if _, ok = m[v]; !ok {
m[v] = struct{}{}
r = append(r, v)
}
}
return
}
// IsPtr check if t is pointer
func IsPtr(t interface{}) bool {
return reflect.TypeOf(t).Kind() == reflect.Ptr
}
// RunCMD run command script
func RunCMD(ctx context.Context, app string, args ...string) (stdout []byte, err error) {
return exec.CommandContext(ctx, app, args...).Output()
}
// Base64Encode encode bytes to string use base64
func Base64Encode(raw []byte) string {
return base64.URLEncoding.EncodeToString(raw)
}
// Base64Decode decode string to bytes use base64
func Base64Decode(encoded string) ([]byte, error) {
return base64.URLEncoding.DecodeString(encoded)
}