-
Notifications
You must be signed in to change notification settings - Fork 382
Expand file tree
/
Copy pathcommon.go
More file actions
164 lines (145 loc) · 4.13 KB
/
common.go
File metadata and controls
164 lines (145 loc) · 4.13 KB
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
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* 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 util
import (
"encoding/json"
"net"
"net/http"
"net/url"
"strconv"
"time"
"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
"github.com/nacos-group/nacos-sdk-go/v2/common/logger"
"github.com/nacos-group/nacos-sdk-go/v2/model"
)
func CurrentMillis() int64 {
return time.Now().UnixNano() / 1e6
}
func JsonToService(result string) *model.Service {
var service model.Service
err := json.Unmarshal([]byte(result), &service)
if err != nil {
logger.Errorf("failed to unmarshal json string:%s err:%+v", result, err)
return nil
}
if len(service.Hosts) == 0 {
logger.Warnf("instance list is empty,json string:%s", result)
}
return &service
}
func ToJsonString(object interface{}) string {
js, _ := json.Marshal(object)
return string(js)
}
func GetGroupName(serviceName string, groupName string) string {
return groupName + constant.SERVICE_INFO_SPLITER + serviceName
}
func GetServiceCacheKey(serviceName string, clusters string) string {
if clusters == "" {
return serviceName
}
return serviceName + constant.SERVICE_INFO_SPLITER + clusters
}
func GetConfigCacheKey(dataId string, group string, tenant string) string {
return dataId + constant.CONFIG_INFO_SPLITER + group + constant.CONFIG_INFO_SPLITER + tenant
}
var localIP = ""
var customClientIP = ""
// SetCustomClientIP sets a custom client IP to be used in gRPC communication.
// This will override the auto-detected local IP.
// Useful for containerized environments where the auto-detected IP might be incorrect.
func SetCustomClientIP(ip string) {
customClientIP = ip
if len(customClientIP) > 0 {
logger.Infof("Custom Client IP set to: %s", customClientIP)
}
}
func LocalIP() string {
if customClientIP != "" {
return customClientIP
}
if localIP == "" {
netInterfaces, err := net.Interfaces()
if err != nil {
logger.Errorf("get Interfaces failed,err:%+v", err)
return ""
}
for i := 0; i < len(netInterfaces); i++ {
if ((netInterfaces[i].Flags & net.FlagUp) != 0) && ((netInterfaces[i].Flags & net.FlagLoopback) == 0) {
addrs, err := netInterfaces[i].Addrs()
if err != nil {
logger.Errorf("get InterfaceAddress failed,err:%+v", err)
return ""
}
for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() && ipnet.IP.To4() != nil {
localIP = ipnet.IP.String()
break
}
}
}
}
if len(localIP) > 0 {
logger.Infof("Local IP:%s", localIP)
}
}
return localIP
}
func GetDurationWithDefault(metadata map[string]string, key string, defaultDuration time.Duration) time.Duration {
data, ok := metadata[key]
if ok {
value, err := strconv.ParseInt(data, 10, 64)
if err != nil {
logger.Errorf("key:%s is not a number", key)
return defaultDuration
}
return time.Duration(value)
}
return defaultDuration
}
func GetUrlFormedMap(source map[string]string) (urlEncoded string) {
urlEncoder := url.Values{}
for key, value := range source {
urlEncoder.Add(key, value)
}
urlEncoded = urlEncoder.Encode()
return
}
// get status code by response,default is NA
func GetStatusCode(response *http.Response) string {
var statusCode string
if response != nil {
statusCode = strconv.Itoa(response.StatusCode)
} else {
statusCode = "NA"
}
return statusCode
}
func DeepCopyMap(params map[string]string) map[string]string {
result := make(map[string]string, len(params))
for k, v := range params {
result[k] = v
}
return result
}
func Contains(slice []string, item string) bool {
for _, s := range slice {
if s == item {
return true
}
}
return false
}