feature

Paddy 2015-03-16

0:caad72abc05a Go to Latest

feature/flag.go

First pass at an implementation. Implement the calculation to determine whether a string is included in the partition or not, using crc32 to coerce the string to an evenly distributed uint32, and a modulo to turn it into a percentage. Implement a store to keep track of available flags and the level of the partition for each flag. Implement an API to retrieve, create, and modify these available flags.

History
1 package feature
3 import (
4 "hash/crc32"
5 "log"
6 )
8 type Flag struct {
9 ID string
10 Limit int
11 }
13 func (f Flag) calcOffset() int {
14 return calcValue(f.ID)
15 }
17 func (f Flag) Permit(candidate string) bool {
18 if f.Limit >= 100 {
19 return true
20 }
21 if f.Limit <= 0 {
22 return false
23 }
24 return (calcValue(candidate)+f.calcOffset())%100 <= f.Limit
25 }
27 type FlagSet struct {
28 flags map[string]bool
29 }
31 func (set FlagSet) Check(flag string) bool {
32 pass, ok := set.flags[flag]
33 if !ok {
34 log.Println("Checked for flag that wasn't in response:", flag)
35 pass = false
36 }
37 return pass
38 }
40 func calcValue(in string) int {
41 return int(crc32.ChecksumIEEE([]byte(in)) % 100)
42 }
44 /*
46 Client has one function:
48 1. func (c Client) Load(in string) FlagSet -- loads the flagset with "in" as the parameter to gate on
50 // TODO Have the client cache FlagSets in memory by the parameter being gated on; we can reuse FlagSets for a minute or two
52 */