fixed bug
This commit is contained in:
1 parent
0c8e5ed294
commit
81bd0d075e
3 files changed
+18
-6
No files matched your search
+5
-1
@@ -39,6 +39,7 @@ type Doorman struct {
|
|||||||
LastChangeTimestamp int64 // an always increasing int that represent the last time the doorman has beed updated
|
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
|
Probabilities []*big.Rat // The probability of each cases. The sum of probabilities needs to be one
|
||||||
wg sync.WaitGroup // waitgroup for goroutine safety
|
wg sync.WaitGroup // waitgroup for goroutine safety
|
||||||
|
hashKey []byte // the decoded id
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(id string, probabilities []*big.Rat) (*Doorman, error) {
|
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
|
return nil, err
|
||||||
} else if len(bid) != 16 {
|
} else if len(bid) != 16 {
|
||||||
return nil, errors.New("id must be base64 encoded of a 16 bytes array")
|
return nil, errors.New("id must be base64 encoded of a 16 bytes array")
|
||||||
|
} else {
|
||||||
|
wab.hashKey = bid
|
||||||
|
wab.Id = id
|
||||||
}
|
}
|
||||||
wab.Probabilities = probabilities
|
wab.Probabilities = probabilities
|
||||||
return wab, wab.Validate()
|
return wab, wab.Validate()
|
||||||
@@ -133,7 +137,7 @@ func (w *Doorman) GenerateRandomProbabilityFromInteger(data uint64) *big.Rat {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (w *Doorman) Hash(data ...[]byte) uint64 {
|
func (w *Doorman) Hash(data ...[]byte) uint64 {
|
||||||
h := siphash.New(make([]byte, 16))
|
h := siphash.New(w.hashKey)
|
||||||
for _, datum := range data {
|
for _, datum := range data {
|
||||||
h.Write(datum)
|
h.Write(datum)
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-1
@@ -58,7 +58,9 @@ func TestNew(t *testing.T) {
|
|||||||
t.Error()
|
t.Error()
|
||||||
}
|
}
|
||||||
goodId := "MTIzNDU2Nzg5MDEyMzQ1Ng=="
|
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()
|
t.Error()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -131,6 +133,7 @@ func TestGenerateRandomProbabilityFromBitSlice(t *testing.T) {
|
|||||||
func TestHash(t *testing.T) {
|
func TestHash(t *testing.T) {
|
||||||
m := []byte("doormen are great")
|
m := []byte("doormen are great")
|
||||||
w := new(Doorman)
|
w := new(Doorman)
|
||||||
|
w.hashKey = make([]byte, 16)
|
||||||
var expt uint64 = 0x3973fc1b3e324215
|
var expt uint64 = 0x3973fc1b3e324215
|
||||||
result := w.Hash(m)
|
result := w.Hash(m)
|
||||||
|
|
||||||
|
|||||||
+9
-4
@@ -10,7 +10,6 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/didiercrunch/doorman"
|
"github.com/didiercrunch/doorman"
|
||||||
"gopkg.in/mgo.v2/bson"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func getProbs(probs ...float64) []*big.Rat {
|
func getProbs(probs ...float64) []*big.Rat {
|
||||||
@@ -21,9 +20,13 @@ func getProbs(probs ...float64) []*big.Rat {
|
|||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
var wab = doorman.New(bson.ObjectIdHex("558701a2f6fb5e29a4000003"), getProbs(1, 0, 0))
|
var wab *doorman.Doorman
|
||||||
|
|
||||||
func init() {
|
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 {
|
if err := wab.Subscriber("http://localhost:1999"); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@@ -47,13 +50,16 @@ var tmpl = `
|
|||||||
<html>
|
<html>
|
||||||
<head></head>
|
<head></head>
|
||||||
<body style="background:{{ .color }};width:100%;height:100%">
|
<body style="background:{{ .color }};width:100%;height:100%">
|
||||||
|
<div style="width: 62.5rem;">
|
||||||
<h1 style="text-align:center;">the doorman value is {{ .value }}</h1>
|
<h1 style="text-align:center;">the doorman value is {{ .value }}</h1>
|
||||||
<p style="text-align:center;">
|
<p style="text-align:center;">
|
||||||
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
|
will have different doorman results. For example, here is a random
|
||||||
url: <a href="{{ .nextUrl }}" style="color: black;"> {{ .nextUrl }} </a>
|
url: <a href="{{ .nextUrl }}" style="color: black;"> {{ .nextUrl }} </a>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
`
|
`
|
||||||
@@ -84,5 +90,4 @@ func main() {
|
|||||||
port := getPort()
|
port := getPort()
|
||||||
log.Println("serving on port", port)
|
log.Println("serving on port", port)
|
||||||
log.Fatal(http.ListenAndServe(":"+port, nil))
|
log.Fatal(http.ListenAndServe(":"+port, nil))
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user