removed stuttering in name
This commit is contained in:
parent
4b5793ee6d
commit
43aedf5db2
16
id.go
16
id.go
@ -7,35 +7,35 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// This thing contains a channel that when initialized (see NewIdGenerator)
|
// This thing contains a channel that when initialized (see NewGenerator)
|
||||||
// will return a bunch of (as best as I can tell) unique md5 hashes.
|
// will return a bunch of (as best as I can tell) unique md5 hashes.
|
||||||
//
|
//
|
||||||
// we use this for naming players, games, etc.
|
// we use this for naming players, games, etc.
|
||||||
//
|
//
|
||||||
// It will consume a single goroutine
|
// It will consume a single goroutine
|
||||||
type IdGenerator struct {
|
type Generator struct {
|
||||||
id chan int64
|
id chan int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewIdGenerator() *IdGenerator {
|
func NewGenerator() *Generator {
|
||||||
return &IdGenerator{
|
return &Generator{
|
||||||
id: make(chan int64),
|
id: make(chan int64),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run is called (typically in a gorotine) to allow for queries to be made
|
// Run is called (typically in a gorotine) to allow for queries to be made
|
||||||
// against IdGenerator.id throgh the Hash method.
|
// against Generator.id throgh the Hash method.
|
||||||
func (idg *IdGenerator) Run() {
|
func (idg *Generator) Run() {
|
||||||
var i int64
|
var i int64
|
||||||
for i = 0; ; i++ {
|
for i = 0; ; i++ {
|
||||||
idg.id <- i
|
idg.id <- i
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hash is the method used by a properly instantiated IdGenerator that gives
|
// Hash is the method used by a properly instantiated Generator that gives
|
||||||
// fairly unique strings. They are currently truncated md5 hashes of the time
|
// fairly unique strings. They are currently truncated md5 hashes of the time
|
||||||
// plus a unique counter
|
// plus a unique counter
|
||||||
func (id *IdGenerator) Hash() string {
|
func (id *Generator) Hash() string {
|
||||||
h := md5.New()
|
h := md5.New()
|
||||||
ns := time.Now().UnixNano() + <-id.id
|
ns := time.Now().UnixNano() + <-id.id
|
||||||
io.WriteString(h, fmt.Sprintf("%d", ns))
|
io.WriteString(h, fmt.Sprintf("%d", ns))
|
||||||
|
@ -4,10 +4,10 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestIDGenerator(t *testing.T) {
|
func TestGenerator(t *testing.T) {
|
||||||
cache := make(map[string]bool)
|
cache := make(map[string]bool)
|
||||||
var cur string
|
var cur string
|
||||||
gg := NewIdGenerator()
|
gg := NewGenerator()
|
||||||
go gg.Run()
|
go gg.Run()
|
||||||
for i := 0; i < 10000; i++ {
|
for i := 0; i < 10000; i++ {
|
||||||
cur = gg.Hash()
|
cur = gg.Hash()
|
||||||
|
Loading…
Reference in New Issue
Block a user