gearman-go/client/id.go

38 lines
544 B
Go
Raw Normal View History

package client
import (
"strconv"
"sync/atomic"
2013-08-30 12:36:57 +08:00
"time"
)
2013-08-30 11:41:18 +08:00
var (
IdGen IdGenerator
)
func init() {
IdGen = NewAutoIncId()
}
type IdGenerator interface {
Id() string
}
// AutoIncId
type autoincId struct {
value int64
}
func (ai *autoincId) Id() string {
next := atomic.AddInt64(&ai.value, 1)
return strconv.FormatInt(next, 10)
}
func NewAutoIncId() IdGenerator {
// we'll consider the nano fraction of a second at startup unique
// and count up from there.
return &autoincId{
value: int64(time.Now().Nanosecond()) << 32,
}
}