-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.go
60 lines (51 loc) · 1.55 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
package main
import (
"encoding/binary"
"go.etcd.io/etcd/client/v3"
"google.golang.org/grpc"
"os"
"pome/define"
)
const (
registryPrefix = "/pome-r/"
)
type serviceName string
func (s serviceName) concat(id clientv3.LeaseID) string {
var buf = make([]byte, 8)
binary.BigEndian.PutUint64(buf, uint64(int64(id)))
return registryPrefix + string(s) + string(buf)
}
func (s *serviceName) split(raw []byte) (id int64) {
*s = serviceName(raw[len(registryPrefix) : len(raw)-8])
id = int64(binary.BigEndian.Uint64(raw[len(raw)-8:]))
return
}
func serviceNameFrom(s grpc.ServerStream) string {
fullMethodName, _ := grpc.MethodFromServerStream(s)
var i, j int
for i = len(fullMethodName) - 1; i >= 0 && fullMethodName[i] != '/'; i-- {
}
for j = i; j >= 0 && fullMethodName[j] != '.'; j-- {
}
return string(fullMethodName[j+1 : i])
}
func fullServiceNameFrom(s grpc.ServerStream) (string, string) {
fullMethodName, _ := grpc.MethodFromServerStream(s)
var i, j int
for i = len(fullMethodName) - 1; i >= 0 && fullMethodName[i] != '/'; i-- {
}
for j = i; j >= 0 && fullMethodName[j] != '.'; j-- {
}
return string(fullMethodName[j+1 : i]), string(fullMethodName[i+1:])
}
func localhost() string { // 返回本节点在 service mesh 中的地址,这一步暂时没想到怎么做比较好,通过 环境变量 写入?
return os.Getenv(define.POME_ADDRESS)
}
var serviceNameThis serviceName
func name() serviceName {
if serviceNameThis != "" {
return serviceNameThis
}
serviceNameThis = serviceName(os.Getenv(define.POME_SERVICE_NAME))
return serviceNameThis
}