Used the idg.Generator from github

This commit is contained in:
Stephen McQuay 2014-11-24 14:42:11 -08:00
parent 3eb76009ad
commit f86bacbd69
4 changed files with 6 additions and 65 deletions

View File

@ -12,6 +12,7 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/smcquay/idg"
"golang.org/x/net/websocket" "golang.org/x/net/websocket"
) )
@ -28,7 +29,7 @@ func (h JsonHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// Controller is the shepherd of a collection of games. The main package in // Controller is the shepherd of a collection of games. The main package in
// server simply populates one of these and starts an http server. // server simply populates one of these and starts an http server.
type Controller struct { type Controller struct {
Idg *IdGenerator Idg *idg.Generator
Conf Config Conf Config
Games MapLock Games MapLock
Memprofile string Memprofile string
@ -40,7 +41,7 @@ type Controller struct {
// Controller. // Controller.
func NewController(conf Config, mprof, pprof string) *http.ServeMux { func NewController(conf Config, mprof, pprof string) *http.ServeMux {
c := &Controller{ c := &Controller{
Idg: NewIdGenerator(), Idg: idg.NewGenerator(),
Conf: conf, Conf: conf,
Games: MapLock{ Games: MapLock{
M: make(map[string]*Game), M: make(map[string]*Game),

43
id.go
View File

@ -1,43 +0,0 @@
package server
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]
}

View File

@ -1,19 +0,0 @@
package server
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
}
}

View File

@ -5,6 +5,8 @@ import (
"math" "math"
"math/rand" "math/rand"
"github.com/smcquay/idg"
v "bitbucket.org/hackerbots/vector" v "bitbucket.org/hackerbots/vector"
) )
@ -34,7 +36,7 @@ type Robot struct {
ProbeResult *Collision `json:"probe_result"` ProbeResult *Collision `json:"probe_result"`
gameStats *BotStats `json:"-"` gameStats *BotStats `json:"-"`
Delta float64 `json:"-"` Delta float64 `json:"-"`
idg *IdGenerator idg *idg.Generator
} }
// Collision is basically a Point2d. // Collision is basically a Point2d.