Initial commit

This commit is contained in:
Cruise Zhao
2020-04-06 19:57:19 +08:00
commit 2fc86fd0a1
42 changed files with 4056 additions and 0 deletions

52
trace/trace.go Normal file
View File

@@ -0,0 +1,52 @@
package trace
import "time"
var t *Trace
type Trace struct {
queue chan int64
tick <-chan time.Time
ts int64
serverId int64
}
func NewTrace(serverId int64) {
if t != nil {
return
}
t = &Trace{
queue: make(chan int64, 1000),
tick: time.Tick(time.Second),
serverId: serverId % 65536,
}
t.ts = t.serverId<<47 | time.Now().Unix()<<16
go func() {
var i int64 = 0
for {
t.queue <- i % 65536
i++
}
}()
go func() {
for now := range t.tick {
t.ts = t.serverId<<47 | now.Unix()<<16
}
}()
}
func GenId() int64 {
if t == nil {
return 0
}
select {
case seq := <-t.queue:
return seq | t.ts
default:
return 0
}
}