From 81bd0d075efa1c48d557f4f285b1863d78683d48 Mon Sep 17 00:00:00 2001 From: didier amyot Date: Tue, 7 Jul 2015 21:02:04 -0400 Subject: [PATCH] fixed bug --- doorman.go | 6 +++++- doorman_test.go | 5 ++++- example/main.go | 13 +++++++++---- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/doorman.go b/doorman.go index cd399d9..21a75b9 100644 --- a/doorman.go +++ b/doorman.go @@ -39,6 +39,7 @@ type Doorman struct { LastChangeTimestamp int64 // an always increasing int that represent the last time the doorman has beed updated Probabilities []*big.Rat // The probability of each cases. The sum of probabilities needs to be one wg sync.WaitGroup // waitgroup for goroutine safety + hashKey []byte // the decoded id } func New(id string, probabilities []*big.Rat) (*Doorman, error) { @@ -47,6 +48,9 @@ func New(id string, probabilities []*big.Rat) (*Doorman, error) { return nil, err } else if len(bid) != 16 { return nil, errors.New("id must be base64 encoded of a 16 bytes array") + } else { + wab.hashKey = bid + wab.Id = id } wab.Probabilities = probabilities return wab, wab.Validate() @@ -133,7 +137,7 @@ func (w *Doorman) GenerateRandomProbabilityFromInteger(data uint64) *big.Rat { } func (w *Doorman) Hash(data ...[]byte) uint64 { - h := siphash.New(make([]byte, 16)) + h := siphash.New(w.hashKey) for _, datum := range data { h.Write(datum) } diff --git a/doorman_test.go b/doorman_test.go index 40a99d9..609a872 100644 --- a/doorman_test.go +++ b/doorman_test.go @@ -58,7 +58,9 @@ func TestNew(t *testing.T) { t.Error() } goodId := "MTIzNDU2Nzg5MDEyMzQ1Ng==" - if _, err := New(goodId, getProbs("1/4", "1/4")); err == nil { + if dm, err := New(goodId, getProbs("1/4", "1/4")); err == nil { + t.Error() + } else if dm.Id != goodId { t.Error() } } @@ -131,6 +133,7 @@ func TestGenerateRandomProbabilityFromBitSlice(t *testing.T) { func TestHash(t *testing.T) { m := []byte("doormen are great") w := new(Doorman) + w.hashKey = make([]byte, 16) var expt uint64 = 0x3973fc1b3e324215 result := w.Hash(m) diff --git a/example/main.go b/example/main.go index b42f1b5..9fdffe0 100644 --- a/example/main.go +++ b/example/main.go @@ -10,7 +10,6 @@ import ( "strconv" "github.com/didiercrunch/doorman" - "gopkg.in/mgo.v2/bson" ) func getProbs(probs ...float64) []*big.Rat { @@ -21,9 +20,13 @@ func getProbs(probs ...float64) []*big.Rat { return ret } -var wab = doorman.New(bson.ObjectIdHex("558701a2f6fb5e29a4000003"), getProbs(1, 0, 0)) +var wab *doorman.Doorman func init() { + var err error + if wab, err = doorman.New("zST8_FWcPDT2-14wDQAAAQ==", getProbs(1, 0, 0)); err != nil { + panic(err) + } if err := wab.Subscriber("http://localhost:1999"); err != nil { panic(err) } @@ -47,13 +50,16 @@ var tmpl = ` +

the doorman value is {{ .value }}

- this doorman depend solely on the url. If you hit different url you + This doorman depend solely on the url. If you hit different url you will have different doorman results. For example, here is a random url: {{ .nextUrl }}

+
+ ` @@ -84,5 +90,4 @@ func main() { port := getPort() log.Println("serving on port", port) log.Fatal(http.ListenAndServe(":"+port, nil)) - }