-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelper.go
93 lines (76 loc) · 1.96 KB
/
helper.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
package gmicro
import (
"context"
"crypto/md5"
"encoding/hex"
"errors"
"math/rand"
"net"
"strconv"
"strings"
"time"
"github.com/google/uuid"
"google.golang.org/grpc/peer"
)
var (
// ErrInvokeGRPCClientIP get grpc request client ip fail.
ErrInvokeGRPCClientIP = errors.New("invoke from context failed")
// ErrPeerAddressNil gRPC peer address is nil.
ErrPeerAddressNil = errors.New("peer address is nil")
)
func init() {
rand.Seed(time.Now().UnixNano())
}
// GetGRPCClientIP get client ip address from context
func GetGRPCClientIP(ctx context.Context) (string, error) {
pr, ok := peer.FromContext(ctx)
if !ok {
return "", ErrInvokeGRPCClientIP
}
if pr.Addr == net.Addr(nil) {
return "", ErrPeerAddressNil
}
addSlice := strings.Split(pr.Addr.String(), ":")
return addSlice[0], nil
}
// RndUUID realizes unique uuid based on time ns and random number
// There is no duplication of uuid on a single machine
// If you want to generate non-duplicate uuid on a distributed architecture
// Just add some custom strings in front of rndStr
// Return format: eba1e8cd-0460-4910-49c6-44bdf3cf024d
func RndUUID() string {
s := RndUUIDMd5()
return strings.Join([]string{
s[:8], s[8:12], s[12:16], s[16:20], s[20:],
}, "-")
}
// RndUUIDMd5 make an uuid
func RndUUIDMd5() string {
ns := time.Now().UnixNano()
rndStr := strings.Join([]string{
strconv.FormatInt(ns, 10), strconv.FormatInt(RandInt64(1000, 9999), 10),
}, "")
return Md5(rndStr)
}
// RandInt64 crete a num [m,n]
func RandInt64(min, max int64) int64 {
if min >= max || min == 0 || max == 0 {
return max
}
return rand.Int63n(max-min) + min
}
// Md5 md5 func
func Md5(str string) string {
h := md5.New()
h.Write([]byte(str))
return hex.EncodeToString(h.Sum(nil))
}
// Uuid uuid of version4
// eg:eba1e8cd0460491049c644bdf3cf024d
func Uuid() string {
u, err := uuid.NewRandom()
if err != nil {
return strings.Replace(RndUUID(), "-", "", -1)
}
return strings.Replace(u.String(), "-", "", -1)
}