-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
110 lines (99 loc) · 2.08 KB
/
common.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
// Copyright 2021 冯立强 fenglq@tingyun.com. All rights reserved.
package tingyun3
import (
"fmt"
"runtime"
"sync"
)
type httpListenAddress struct {
Addr string
tls bool
}
//GetCallerName : 取layer层调用栈函数名
//go:noinline
func GetCallerName(layer int) string {
if _, pc := GetCallerPC(layer + 1); pc != 0 {
return runtime.FuncForPC(pc).Name()
}
return ""
}
type listenSet struct {
lock sync.RWMutex
listens map[string]int
}
func (l *listenSet) init() *listenSet {
l.listens = make(map[string]int)
return l
}
func (l *listenSet) ForEach(handler func(string)) {
l.lock.Lock()
for addr, _ := range l.listens {
handler(addr)
}
l.lock.Unlock()
}
func (l *listenSet) Append(address string) {
l.lock.Lock()
l.listens[address] = 1
l.lock.Unlock()
}
var listens listenSet = listenSet{}
//go:noinline
func GetRootCallerName(layer int) string {
var pc uintptr = 0
for {
if addr, _, _, success := runtime.Caller(layer); success {
pc = addr
layer++
} else {
break
}
}
if pc != 0 {
return runtime.FuncForPC(pc).Name()
}
return ""
}
func printCallers() {
callers := make([]uintptr, 30)
callerCount := runtime.Callers(1, callers)
for i := 0; i < callerCount; i++ {
f := runtime.FuncForPC(callers[i])
fname := f.Name()
file, line := f.FileLine(callers[i])
fmt.Printf("%s(%s:%d)\n", fname, file, line)
}
}
//go:noinline
func MatchCallerName(layer int, funcname string) bool {
for {
if addr, _, _, success := runtime.Caller(layer); success {
if runtime.FuncForPC(addr).Name() == funcname {
return true
}
layer++
} else {
break
}
}
return false
}
// GetCallerPC return caller pc
//go:noinline
func GetCallerPC(layer int) (l int, pc uintptr) {
if pc, _, _, success := runtime.Caller(layer); success {
return layer, pc
}
return 0, 0
}
const (
StorageIndexDatabase = 1 + 8*0
StorageIndexRedis = 1 + 8*1
StorageIndexEcho = 1 + 8*2
StorageIndexMongo = 1 + 8*3
StorageIndexMgo = 2 + 8*3
StorageIndexBeego = 1 + 8*4
StorageIndexIris = 1 + 8*5
StorageIndexGin = 1 + 8*6
StorageGorillaWS = 1 + 8*7
)