Initial commit
This commit is contained in:
52
trace/trace.go
Normal file
52
trace/trace.go
Normal 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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user