added code

This commit is contained in:
Stephen McQuay 2014-08-03 01:02:16 -07:00
parent ffcf226edf
commit 4b5793ee6d
2 changed files with 62 additions and 0 deletions

43
id.go Normal file
View File

@ -0,0 +1,43 @@
package idg
import (
"crypto/md5"
"fmt"
"io"
"time"
)
// This thing contains a channel that when initialized (see NewIdGenerator)
// will return a bunch of (as best as I can tell) unique md5 hashes.
//
// we use this for naming players, games, etc.
//
// It will consume a single goroutine
type IdGenerator struct {
id chan int64
}
func NewIdGenerator() *IdGenerator {
return &IdGenerator{
id: make(chan int64),
}
}
// Run is called (typically in a gorotine) to allow for queries to be made
// against IdGenerator.id throgh the Hash method.
func (idg *IdGenerator) Run() {
var i int64
for i = 0; ; i++ {
idg.id <- i
}
}
// Hash is the method used by a properly instantiated IdGenerator that gives
// fairly unique strings. They are currently truncated md5 hashes of the time
// plus a unique counter
func (id *IdGenerator) Hash() string {
h := md5.New()
ns := time.Now().UnixNano() + <-id.id
io.WriteString(h, fmt.Sprintf("%d", ns))
return fmt.Sprintf("%x", h.Sum(nil))[:8]
}

19
id_test.go Normal file
View File

@ -0,0 +1,19 @@
package idg
import (
"testing"
)
func TestIDGenerator(t *testing.T) {
cache := make(map[string]bool)
var cur string
gg := NewIdGenerator()
go gg.Run()
for i := 0; i < 10000; i++ {
cur = gg.Hash()
if _, ok := cache[cur]; ok {
t.Errorf("unexpected duplicate key")
}
cache[cur] = true
}
}