/
utils.go
executable file
·218 lines (194 loc) · 5.05 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
package udp
import (
"bytes"
"encoding/binary"
"fmt"
"os"
"runtime"
"strconv"
"strings"
"sync"
"time"
)
func int64ToBytes(n int64) ([]byte, error) {
buf := bytes.NewBuffer([]byte{})
err := binary.Write(buf, binary.BigEndian, n)
return buf.Bytes(), err
}
func intToBytes(n int) ([]byte, error) {
return int64ToBytes(int64(n))
}
func bytesToInt(bys []byte) (int, error) {
i, err := bytesToInt64(bys)
return int(i), err
}
func bytesToInt64(bys []byte) (int64, error) {
buf := bytes.NewBuffer(bys)
var data int64
err := binary.Read(buf, binary.BigEndian, &data)
return data, err
}
func formatName(str string) string {
ln := len(str)
if ln > 0 && ln <= 7 {
// 补齐位
for i := 0; i < 7-ln; i++ {
str += " "
}
}
return str
}
// LogClose 是否关闭日志
var LogClose bool = true
var std = newStd()
// CloseLog 关闭日志
func CloseLog() {
LogClose = false
}
type logger struct {
outFile bool
outFileWriter *os.File
}
func newStd() *logger {
return &logger{}
}
func SetLogFile(name string) {
std.outFile = true
std.outFileWriter, _ = os.OpenFile(name+time.Now().Format("-20060102")+".log",
os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
}
type Level int
var LevelMap = map[Level]string{
1: "[Info] ",
4: "[Error] ",
}
func (l *logger) Log(level Level, args string, times int) {
var buffer bytes.Buffer
buffer.WriteString(time.Now().Format("2006-01-02 15:04:05.000 "))
buffer.WriteString(LevelMap[level])
_, file, line, _ := runtime.Caller(times)
fileList := strings.Split(file, "/")
// 最多显示两级路径
if len(fileList) > 3 {
fileList = fileList[len(fileList)-3 : len(fileList)]
}
buffer.WriteString(strings.Join(fileList, "/"))
buffer.WriteString(":")
buffer.WriteString(strconv.Itoa(line))
buffer.WriteString(" \t| ")
buffer.WriteString(args)
buffer.WriteString("\n")
out := buffer.Bytes()
if LogClose {
_, _ = buffer.WriteTo(os.Stdout)
}
if l.outFile {
_, _ = l.outFileWriter.Write(out)
}
}
func Info(args ...interface{}) {
std.Log(1, fmt.Sprint(args...), 2)
}
func InfoF(format string, args ...interface{}) {
std.Log(1, fmt.Sprintf(format, args...), 2)
}
func Error(args ...interface{}) {
std.Log(4, fmt.Sprint(args...), 2)
}
func ErrorF(format string, args ...interface{}) {
std.Log(4, fmt.Sprintf(format, args...), 2)
}
type IdWorker struct {
startTime int64
workerIdBits uint
datacenterIdBits uint
maxWorkerId int64
maxDatacenterId int64
sequenceBits uint
workerIdLeftShift uint
datacenterIdLeftShift uint
timestampLeftShift uint
sequenceMask int64
workerId int64
datacenterId int64
sequence int64
lastTimestamp int64
signMask int64
idLock *sync.Mutex
}
func (idw *IdWorker) InitIdWorker(workerId, datacenterId int64) error {
var baseValue int64 = -1
idw.startTime = 1463834116272
idw.workerIdBits = 5
idw.datacenterIdBits = 5
idw.maxWorkerId = baseValue ^ (baseValue << idw.workerIdBits)
idw.maxDatacenterId = baseValue ^ (baseValue << idw.datacenterIdBits)
idw.sequenceBits = 12
idw.workerIdLeftShift = idw.sequenceBits
idw.datacenterIdLeftShift = idw.workerIdBits + idw.workerIdLeftShift
idw.timestampLeftShift = idw.datacenterIdBits + idw.datacenterIdLeftShift
idw.sequenceMask = baseValue ^ (baseValue << idw.sequenceBits)
idw.sequence = 0
idw.lastTimestamp = -1
idw.signMask = ^baseValue + 1
idw.idLock = &sync.Mutex{}
if idw.workerId < 0 || idw.workerId > idw.maxWorkerId {
return fmt.Errorf("workerId[%v] is less than 0 or greater than maxWorkerId[%v].",
workerId, datacenterId)
}
if idw.datacenterId < 0 || idw.datacenterId > idw.maxDatacenterId {
return fmt.Errorf("datacenterId[%d] is less than 0 or greater than maxDatacenterId[%d].",
workerId, datacenterId)
}
idw.workerId = workerId
idw.datacenterId = datacenterId
return nil
}
// NextId 返回一个唯一的 INT64 ID
func (idw *IdWorker) NextId() (int64, error) {
idw.idLock.Lock()
timestamp := time.Now().UnixNano()
if timestamp < idw.lastTimestamp {
return -1, fmt.Errorf(fmt.Sprintf("Clock moved backwards. Refusing to generate id for %d milliseconds",
idw.lastTimestamp-timestamp))
}
if timestamp == idw.lastTimestamp {
idw.sequence = (idw.sequence + 1) & idw.sequenceMask
if idw.sequence == 0 {
timestamp = idw.tilNextMillis()
idw.sequence = 0
}
} else {
idw.sequence = 0
}
idw.lastTimestamp = timestamp
idw.idLock.Unlock()
id := ((timestamp - idw.startTime) << idw.timestampLeftShift) |
(idw.datacenterId << idw.datacenterIdLeftShift) |
(idw.workerId << idw.workerIdLeftShift) |
idw.sequence
if id < 0 {
id = -id
}
return id, nil
}
// tilNextMillis
func (idw *IdWorker) tilNextMillis() int64 {
timestamp := time.Now().UnixNano()
if timestamp <= idw.lastTimestamp {
timestamp = time.Now().UnixNano() / int64(time.Millisecond)
}
return timestamp
}
func ID64() (int64, error) {
currWorker := &IdWorker{}
err := currWorker.InitIdWorker(1000, 2)
if err != nil {
return 0, err
}
return currWorker.NextId()
}
func id() int64 {
id, _ := ID64()
return id
}